-
Notifications
You must be signed in to change notification settings - Fork 6
Realtime settings updates #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marksmith
wants to merge
22
commits into
main
Choose a base branch
from
realtime-settings-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
020b3da
Fix unit for configUpdatedAt is seconds
marksmith dca80c6
Add POSTGRES_PORT environment variable override
marksmith ee99854
Add MYSQL_PORT environment variable override
marksmith 9e497c8
Return Boolean from update settings methods
marksmith 63348d1
Add updater to update settings
marksmith a8c5bc5
Normalize instance variable order
marksmith 7a8e9f7
Add realtime settings updates
marksmith 9842e0b
Remove unused stub methods
marksmith 34b06ba
Fix tests
marksmith b9e2e93
Add tests
marksmith 6f9bdd3
Optimize tests
marksmith 94795b2
Probe realtime endpoint at startup
marksmith cd322ff
Fix tests
marksmith 8d6bcc3
Fix assert_logged and refute_logged
marksmith fb03e7d
Add tests
marksmith ddef592
Fix call #settings_updated with event
marksmith 607c146
Use configured realtime endpoint
marksmith b7534cc
Add realtime updates feature flag
marksmith 06886e1
Avoid logging raw chunks
marksmith 34e57b5
Rename Updater to ExclusiveUpdater
marksmith c6b6d14
Make settings_updated private
marksmith 38322c0
Fix test description typo endpont to endpoint
marksmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "net/http" | ||
| require "uri" | ||
| require "json" | ||
|
|
||
| module Aikido::Zen | ||
| class APIStream | ||
| def initialize( | ||
| config: Aikido::Zen.config, | ||
| min_backoff: 5, | ||
| max_backoff: 60, | ||
| backoff_reset: 30, | ||
| open_timeout: 5, | ||
| write_timeout: open_timeout, | ||
| read_timeout: 70 | ||
| ) | ||
| @config = config | ||
| @min_backoff = min_backoff | ||
| @max_backoff = max_backoff | ||
| @backoff_reset = backoff_reset | ||
| @open_timeout = open_timeout | ||
| @write_timeout = write_timeout | ||
| @read_timeout = read_timeout | ||
|
|
||
| @running = Concurrent::AtomicBoolean.new | ||
| @executor = nil | ||
|
|
||
| @host = @config.realtime_endpoint.host | ||
| @port = @config.realtime_endpoint.port | ||
| @use_ssl = @config.realtime_endpoint.scheme == "https" | ||
| @token = @config.api_token | ||
|
|
||
| @handlers = Concurrent::Array.new | ||
| end | ||
|
|
||
| # @return [Boolean] whether we could connect to the realtime endpoint | ||
| def can_connect? | ||
| http = Net::HTTP.new(@host, @port) | ||
|
marksmith marked this conversation as resolved.
|
||
| http.use_ssl = @use_ssl | ||
| http.open_timeout = 5 | ||
| http.write_timeout = 5 | ||
| http.read_timeout = 5 | ||
| http.max_retries = 0 | ||
|
|
||
| request = Net::HTTP::Get.new("/config") | ||
| request["Authorization"] = @token | ||
|
|
||
| begin | ||
| http.request(request) | ||
|
|
||
| return true | ||
| rescue Timeout::Error, SocketError, IOError, SystemCallError, OpenSSL::OpenSSLError => err | ||
| @config.logger.debug("Error probing realtime endpoint: #{err.class}: #{err.message}") | ||
| rescue => err | ||
| @config.logger.error("Error probing realtime endpoint: #{err.class}: #{err.message}") | ||
| end | ||
|
|
||
| false | ||
| end | ||
|
|
||
| def running? | ||
| @running.true? | ||
| end | ||
| alias_method :started?, :running? | ||
|
|
||
| def start! | ||
| return false unless @running.make_true | ||
|
|
||
| @executor = Concurrent::SingleThreadExecutor.new | ||
|
|
||
| @executor.post do | ||
| backoff = @min_backoff | ||
|
|
||
| while running? | ||
| time_before = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) | ||
|
|
||
| begin | ||
| work | ||
| rescue Timeout::Error, SocketError, IOError, SystemCallError, OpenSSL::OpenSSLError => err | ||
| @config.logger.debug("Error in API stream: #{err.class}: #{err.message}") | ||
| rescue => err | ||
| @config.logger.error("Error in API stream: #{err.class}: #{err.message}") | ||
| end | ||
|
|
||
| break unless running? | ||
|
|
||
| time_after = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) | ||
|
|
||
| backoff = if time_after - time_before > @backoff_reset | ||
| @min_backoff | ||
| else | ||
| [backoff * 2, @max_backoff].min | ||
| end | ||
|
|
||
| jitter = rand * backoff / 2 | ||
|
|
||
| @config.logger.debug("API stream reconnecting in %d seconds" % (backoff + jitter).ceil) | ||
|
|
||
| sleep(backoff + jitter) | ||
| end | ||
| end | ||
|
|
||
| true | ||
| end | ||
|
|
||
| def stop! | ||
| return false unless @running.make_false | ||
|
|
||
| @executor.shutdown | ||
| @executor.wait_for_termination(@read_timeout) | ||
|
marksmith marked this conversation as resolved.
|
||
|
|
||
| true | ||
| end | ||
|
|
||
| def handle(type, &block) | ||
| raise ArgumentError, "block required" unless block | ||
|
|
||
| @handlers << proc do |event| | ||
| block.call(event) if type === event[:type] | ||
| end | ||
| end | ||
|
|
||
| private def work | ||
|
marksmith marked this conversation as resolved.
marksmith marked this conversation as resolved.
|
||
| http = Net::HTTP.new(@host, @port) | ||
| http.use_ssl = @use_ssl | ||
| http.open_timeout = @open_timeout | ||
| http.write_timeout = @write_timeout | ||
| http.read_timeout = @read_timeout | ||
| http.max_retries = 0 | ||
|
|
||
| request = Net::HTTP::Get.new("/api/runtime/stream") | ||
| request["Authorization"] = @token | ||
| request["Accept"] = "text/event-stream" | ||
| request["Cache-Control"] = "no-cache" | ||
|
|
||
| @config.logger.debug("API stream connecting") | ||
| http.start | ||
| @config.logger.debug("API stream connected") | ||
|
|
||
| begin | ||
| http.request(request) do |response| | ||
|
marksmith marked this conversation as resolved.
|
||
| case response.code.to_i | ||
| when 200 | ||
| # empty | ||
|
marksmith marked this conversation as resolved.
|
||
| when 401, 403 | ||
| @running.make_false | ||
| return nil | ||
| else | ||
| return nil | ||
| end | ||
|
|
||
| buffer = +"" | ||
|
|
||
| response.read_body do |chunk| | ||
| return nil unless running? | ||
|
|
||
| @config.logger.debug("API stream received chunk of #{chunk.bytesize} bytes") | ||
|
|
||
| buffer << chunk | ||
|
|
||
| while (index = buffer.index("\n\n")) | ||
|
marksmith marked this conversation as resolved.
|
||
| event_str = buffer.slice!(0..index + 1) | ||
| buffer = buffer.lstrip | ||
|
|
||
| event = {} | ||
|
|
||
| begin | ||
| event_str.each_line do |line| | ||
| case line | ||
| when /^event:\s*(.+)/ | ||
| event[:type] = $1.strip | ||
| when /^data:\s*(.+)/ | ||
| event[:data] = JSON.parse($1.strip) | ||
| end | ||
| end | ||
| rescue => err | ||
| @config.logger.error("Error in API stream: #{err.class}: #{err.message}") | ||
| next | ||
| end | ||
|
|
||
| @handlers.each do |handler| | ||
| handler.call(event) | ||
| rescue => err | ||
| @config.logger.error("Error in API stream: #{err.class}: #{err.message}") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| ensure | ||
| @config.logger.debug("API stream disconnecting") | ||
| http.finish | ||
| @config.logger.debug("API stream disconnected") | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.