Class: NNQ::Engine::Reconnect
- Inherits:
-
Object
- Object
- NNQ::Engine::Reconnect
- Defined in:
- lib/nnq/engine/reconnect.rb
Overview
Schedules reconnect attempts with exponential back-off.
Runs a background task that loops until a connection is established or the engine is closed. Caller is non-blocking: #connect returns immediately and the actual dial happens inside the task.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(engine, endpoint, options) ⇒ Reconnect
constructor
A new instance of Reconnect.
- #run(parent_task, delay: nil) ⇒ Object
Constructor Details
#initialize(engine, endpoint, options) ⇒ Reconnect
Returns a new instance of Reconnect.
23 24 25 26 27 |
# File 'lib/nnq/engine/reconnect.rb', line 23 def initialize(engine, endpoint, ) @engine = engine @endpoint = endpoint @options = end |
Class Method Details
.schedule(endpoint, options, parent_task, engine, delay: nil) ⇒ Object
18 19 20 |
# File 'lib/nnq/engine/reconnect.rb', line 18 def self.schedule(endpoint, , parent_task, engine, delay: nil) new(engine, endpoint, ).run(parent_task, delay: delay) end |
Instance Method Details
#run(parent_task, delay: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/nnq/engine/reconnect.rb', line 30 def run(parent_task, delay: nil) delay, max_delay = init_delay(delay) parent_task.async(transient: true, annotation: "nnq reconnect #{@endpoint}") do loop do break if @engine.closed? sleep quantized_wait(delay) if delay > 0 break if @engine.closed? begin @engine.transport_for(@endpoint).connect(@endpoint, @engine, **@engine.dial_opts_for(@endpoint)) break rescue *CONNECTION_FAILED, *CONNECTION_LOST => e delay = next_delay(delay, max_delay) @engine.emit_monitor_event(:connect_retried, endpoint: @endpoint, detail: { interval: delay, error: e }) end end rescue Async::Stop end end |