Skip to content

Commit e9ef3d1

Browse files
committed
Don't boot async subscribers in publisher mode.
1 parent 47b1942 commit e9ef3d1

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

.github/workflows/rspec.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
# bundle exec rubocop
3636
- name: Run rspec
3737
run: |
38-
bundle exec rspec
38+
ES_HOSTING_MODE=listener bundle exec rspec

lib/event_source/configure.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_relative "configure/servers"
55
require_relative "configure/contracts"
66
require_relative "configure/operations"
7+
require_relative "configure/mode"
78
require_relative "configure/config"
89

910
module EventSource

lib/event_source/configure/config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def server_key=(value)
2626
@server_key = value&.to_sym
2727
end
2828

29+
def mode
30+
@mode ||= ::EventSource::Configure::Mode.parse(ENV['ES_HOSTING_MODE'])
31+
end
32+
2933
attr_writer :async_api_schemas
3034

3135
def servers

lib/event_source/configure/mode.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module EventSource
4+
module Configure
5+
# Server boot mode. Used for performance and configuration settings.
6+
class Mode
7+
8+
attr_reader :value
9+
10+
def initialize(val)
11+
@value = val
12+
end
13+
14+
def listener?
15+
@value == :listener
16+
end
17+
18+
def publisher?
19+
@value == :publisher
20+
end
21+
22+
def self.publisher
23+
self.new(:publisher)
24+
end
25+
26+
def self.parse(mode_string)
27+
return publisher if mode_string.blank?
28+
mode_sym = mode_string.to_sym
29+
raise ::EventSource::Error::InvalidModeError, "\"#{mode_string}\" is an invalid mode. Must be empty, null, \"publisher\", or \"listener\"." if ![:publisher, :listener].include?(mode_sym)
30+
self.new(mode_string.to_sym)
31+
end
32+
end
33+
end
34+
end

lib/event_source/error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Error < StandardError
2121
ConstantNotDefined = Class.new(Error)
2222
ContractNotFound = Class.new(Error)
2323
EventNameUndefined = Class.new(Error)
24+
InvalidModeError = Class.new(Error)
2425
FileAccessError = Class.new(Error)
2526
InvalidChannelsResourceError = Class.new(Error)
2627
PublisherAlreadyRegisteredError = Class.new(Error)

lib/event_source/protocols/amqp/bunny_queue_proxy.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ def subscribe(subscriber_klass, bindings)
7373

7474
@channel_proxy.subject.prefetch(prefetch)
7575

76+
# Do not spawn consumers in the 'publisher' mode
77+
unless ::EventSource.config.mode.listener?
78+
logger.debug "In publisher mode, not booting subscription"
79+
return
80+
end
81+
7682
if options[:block]
7783
spawn_thread(options) { add_consumer(subscriber_klass, options) }
7884
else

0 commit comments

Comments
 (0)