Class: Smplkit::Management::FlagRegistrationBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/smplkit/management/buffer.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. drain is unconditional and used only by tests/teardown.

Instance Method Summary collapse

Constructor Details

#initializeFlagRegistrationBuffer

Returns a new instance of FlagRegistrationBuffer.



50
51
52
53
54
# File 'lib/smplkit/management/buffer.rb', line 50

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

Instance Method Details

#add(declaration) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/smplkit/management/buffer.rb', line 56

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



72
73
74
75
76
77
# File 'lib/smplkit/management/buffer.rb', line 72

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

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

#drainObject



79
80
81
82
83
84
85
# File 'lib/smplkit/management/buffer.rb', line 79

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

#peekObject



68
69
70
# File 'lib/smplkit/management/buffer.rb', line 68

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

#pending_countObject



87
88
89
# File 'lib/smplkit/management/buffer.rb', line 87

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