A Ruby gem for streaming data from the Bluesky/ATProto firehose
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

throw error when trying to run two streams in one process

Because EventMachine uses a single special "reactor thread" to run things and handle events, and there can be only one such thread at a time, you can't run two completely separate such setups in one process. You could run the second Stream inside the existing reactor thread, but this can lead to some unexpected behavior, like Stream#start method exiting immediately when you expect it to block (since the current thread isn't blocked to run the reactor, but simply passes the proc to the other thread), or different Streams blocking each other if processing events takes a bit longer than expected (since the processing of both would happen on the same thread, unless work is explicitly done in the background).

If you want to do it anyway and you know what you're doing, you know what to do.

+17 -1
+9
lib/skyfall/errors.rb
··· 5 5 class UnsupportedError < StandardError 6 6 end 7 7 8 + class ReactorActiveError < StandardError 9 + def initialize 10 + super( 11 + "An EventMachine reactor thread is already running, but it seems to have been launched by another Stream. " + 12 + "Skyfall doesn't currently support running two different Stream instances in a single process." 13 + ) 14 + end 15 + end 16 + 8 17 class SubscriptionError < StandardError 9 18 attr_reader :error_type, :error_message 10 19
+8 -1
lib/skyfall/stream.rb
··· 33 33 url = build_websocket_url 34 34 35 35 @handlers[:connecting]&.call(url) 36 - @engines_on = true 37 36 38 37 @reconnect_timer&.cancel 39 38 @reconnect_timer = nil 39 + 40 + raise ReactorActiveError if existing_reactor? 41 + 42 + @engines_on = true 40 43 41 44 EM.run do 42 45 EventMachine.error_handler do |e| ··· 81 84 end 82 85 end 83 86 end 87 + end 88 + 89 + def existing_reactor? 90 + EM.reactor_running? && !@engines_on 84 91 end 85 92 86 93 def handle_message(msg)