Class: RobotLab::Streaming::SequenceCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/streaming/sequence_counter.rb

Overview

Monotonic sequence counter for event ordering

Provides globally unique, strictly increasing sequence numbers for event ordering across streaming contexts.

Thread-safe via Mutex.

Instance Method Summary collapse

Constructor Details

#initialize(start: 0) ⇒ SequenceCounter

Creates a new SequenceCounter.

Parameters:

  • start (Integer) (defaults to: 0)

    the starting value (default: 0)



16
17
18
19
# File 'lib/robot_lab/streaming/sequence_counter.rb', line 16

def initialize(start: 0)
  @value = start
  @mutex = Mutex.new
end

Instance Method Details

#currentInteger

Get the current value without incrementing

Returns:

  • (Integer)


35
36
37
# File 'lib/robot_lab/streaming/sequence_counter.rb', line 35

def current
  @mutex.synchronize { @value }
end

#nextInteger

Get the next sequence number

Returns:

  • (Integer)


25
26
27
28
29
# File 'lib/robot_lab/streaming/sequence_counter.rb', line 25

def next
  @mutex.synchronize do
    @value += 1
  end
end

#reset(value = 0) ⇒ Object

Reset to a specific value

Parameters:

  • value (Integer) (defaults to: 0)


43
44
45
# File 'lib/robot_lab/streaming/sequence_counter.rb', line 43

def reset(value = 0)
  @mutex.synchronize { @value = value }
end