-
Notifications
You must be signed in to change notification settings - Fork 1
fix: Allow non blocking all the way down to the instance level #27
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -75,7 +75,7 @@ def use(klass, *args, &block) | |||||
| def run(nats_url:, service_opts:, instances: 1, blocking: true) | ||||||
| logger.info 'Booting NATS API server...' | ||||||
| workers = Concurrent::Array.new | ||||||
| pool = spawn_instances(nats_url, service_opts, instances, workers) | ||||||
| pool = spawn_instances(nats_url, service_opts, instances, workers, blocking) | ||||||
| logger.info 'Setting up signal trap...' | ||||||
| trap_signals(workers, pool) | ||||||
| return pool unless blocking | ||||||
|
|
@@ -90,16 +90,16 @@ def run(nats_url:, service_opts:, instances: 1, blocking: true) | |||||
| # @param url [String] The URL of the NATS server. | ||||||
| # @param opts [Hash] Options for the NATS service. | ||||||
| # @param count [Integer] The number of instances to spawn. | ||||||
| # @param workers [Array] The array to store worker instances. | ||||||
| # @param blocking [Boolean] If false, does not block current thread after starting the server. | ||||||
| # | ||||||
| # @return [Concurrent::FixedThreadPool] The thread pool managing the worker threads. | ||||||
| def spawn_instances(url, opts, count, workers) | ||||||
| def spawn_instances(url, opts, count, workers, blocking) | ||||||
| pool = Concurrent::FixedThreadPool.new(count) | ||||||
| @instance_args = opts.delete(:instance_args) || nil | ||||||
| logger.info "Building #{count} workers with options: #{opts.inspect}, instance_args: #{@instance_args}" | ||||||
| count.times do | ||||||
| eps = endpoints.dup | ||||||
| gps = groups.dup | ||||||
| pool.post { build_worker(url, opts, eps, gps, workers) } | ||||||
| pool.post { build_worker(url, opts, workers, blocking) } | ||||||
| end | ||||||
| pool | ||||||
| end | ||||||
|
|
@@ -108,15 +108,16 @@ def spawn_instances(url, opts, count, workers) | |||||
| # | ||||||
| # @param url [String] The URL of the NATS server. | ||||||
| # @param opts [Hash] Options for the NATS service. | ||||||
| # @param eps [Array<Hash>] The list of endpoints to add. | ||||||
| # @param gps [Hash] The groups to add. | ||||||
| # @param workers [Array] The array to store worker instances. | ||||||
| # @param blocking [Boolean] If true, blocks the current thread until the worker is set up. | ||||||
| # | ||||||
| # @return [void] | ||||||
| def build_worker(url, opts, eps, gps, workers) | ||||||
| def build_worker(url, opts, workers, blocking) | ||||||
| worker = @instance_args ? new(*@instance_args) : new | ||||||
| workers << worker | ||||||
| worker.setup_worker(url, opts, eps, gps) | ||||||
| return worker.setup_worker!(nats_url: url, service_opts: opts) if blocking | ||||||
|
|
||||||
| worker.setup_worker(nats_url: url, service_opts: opts) | ||||||
| end | ||||||
|
|
||||||
| # Shuts down the NATS API server gracefully. | ||||||
|
|
@@ -174,20 +175,28 @@ def logger = self.class.logger | |||||
|
|
||||||
| # Sets up a worker thread for the NATS API server. | ||||||
| # This method connects to the NATS server, adds the service, groups, and endpoints, | ||||||
| # and keeps the worker thread alive. | ||||||
| # | ||||||
| # @param url [String] The URL of the NATS server. | ||||||
| # @param opts [Hash] Options for the NATS service. | ||||||
| # @param eps [Array<Hash>] The list of endpoints to add. | ||||||
| # @param gps [Hash] The groups to add. | ||||||
| # | ||||||
| # @return [void] | ||||||
| def setup_worker(url, opts, eps, gps) | ||||||
| def setup_worker(nats_url: 'nats://localhost:4222', service_opts: {}) | ||||||
| @thread = Thread.current | ||||||
| @client = NATS.connect url | ||||||
| @service = @client.services.add(**opts) | ||||||
| @client = NATS.connect nats_url | ||||||
| @service = @client.services.add(build_service_opts(service_opts:)) | ||||||
|
||||||
| @service = @client.services.add(build_service_opts(service_opts:)) | |
| @service = @client.services.add(build_service_opts(service_opts: service_opts)) |
bougyman marked this conversation as resolved.
Show resolved
Hide resolved
bougyman marked this conversation as resolved.
Show resolved
Hide resolved
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conditional logic is inverted. When
blockingis true, the method callssetup_worker!which blocks withsleep, but then immediately returns, preventing the sleep from executing. This should beunless blockingor the logic should be restructured.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it got this wrong. Not sure why it thinks the sleep is prevented from executing