Class: Smplkit::FlagRegistrationBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/buffers.rb

Overview

Thread-safe batch buffer for flag declarations.

Use peek + commit(ids) for the send path so a failed POST leaves declarations queued for the next attempt; the legacy drain is unconditional and used only by tests.

Instance Method Summary collapse

Constructor Details

#initializeFlagRegistrationBuffer

Returns a new instance of FlagRegistrationBuffer.



70
71
72
73
74
# File 'lib/smplkit/buffers.rb', line 70

def initialize
  @seen = {}
  @pending = []
  @lock = Mutex.new
end

Instance Method Details

#add(declaration) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/smplkit/buffers.rb', line 76

def add(declaration)
  @lock.synchronize do
    next if @seen.key?(declaration.id)

    @seen[declaration.id] = true
    item = { "id" => declaration.id, "type" => declaration.type, "default" => declaration.default }
    item["service"] = declaration.service if declaration.service
    item["environment"] = declaration.environment if declaration.environment
    @pending << item
  end
end

#commit(ids) ⇒ Object



92
93
94
95
96
97
# File 'lib/smplkit/buffers.rb', line 92

def commit(ids)
  return if ids.nil? || ids.empty?

  committed = ids.to_set
  @lock.synchronize { @pending.reject! { |item| committed.include?(item["id"]) } }
end

#drainObject



99
100
101
102
103
104
105
# File 'lib/smplkit/buffers.rb', line 99

def drain
  @lock.synchronize do
    batch = @pending
    @pending = []
    batch
  end
end

#peekObject



88
89
90
# File 'lib/smplkit/buffers.rb', line 88

def peek
  @lock.synchronize { @pending.dup }
end

#pending_countObject



107
108
109
# File 'lib/smplkit/buffers.rb', line 107

def pending_count
  @lock.synchronize { @pending.length }
end