Class: KubeMQ::Transport::ReconnectManager Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kubemq/transport/reconnect_manager.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manages automatic reconnection with exponential backoff and jitter.

Spawns a background thread that repeatedly invokes the reconnect procedure until the connection succeeds or the maximum number of attempts is reached. Backoff delay is computed as:

delay = min(base_interval * multiplier^(attempt-1), max_delay) +/- jitter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy:, state_machine:, reconnect_proc:, on_reconnected: nil) ⇒ ReconnectManager

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ReconnectManager.

Parameters:

  • policy (ReconnectPolicy)

    backoff and retry settings

  • state_machine (ConnectionStateMachine)

    tracks connection lifecycle

  • reconnect_proc (Proc)

    callable that performs the actual reconnection

  • on_reconnected (Proc, nil) (defaults to: nil)

    optional callback invoked after successful reconnection



25
26
27
28
29
30
31
32
33
34
# File 'lib/kubemq/transport/reconnect_manager.rb', line 25

def initialize(policy:, state_machine:, reconnect_proc:, on_reconnected: nil)
  @policy = policy
  @state_machine = state_machine
  @reconnect_proc = reconnect_proc
  @on_reconnected = on_reconnected
  @attempt = 0
  @mutex = Mutex.new
  @thread = nil
  @stop_flag = false
end

Instance Attribute Details

#attemptInteger (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns current reconnect attempt counter (0 before first attempt).

Returns:

  • (Integer)

    current reconnect attempt counter (0 before first attempt)



19
20
21
# File 'lib/kubemq/transport/reconnect_manager.rb', line 19

def attempt
  @attempt
end

Instance Method Details

#startvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Starts the background reconnect loop. No-op if already running.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kubemq/transport/reconnect_manager.rb', line 39

def start
  @mutex.synchronize do
    return if @thread&.alive?

    @stop_flag = false
    @attempt = 0
    @thread = Thread.new { reconnect_loop }
    @thread.name = 'kubemq-reconnect'
    @thread.abort_on_exception = false
  end
end

#stopvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Signals the reconnect loop to stop and waits up to 5 seconds for the background thread to finish.



55
56
57
58
# File 'lib/kubemq/transport/reconnect_manager.rb', line 55

def stop
  @mutex.synchronize { @stop_flag = true }
  @thread&.join(5)
end