Class: Smplkit::LoggerRegistrationBuffer

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

Overview

Thread-safe batch buffer for logger discovery.

Instance Method Summary collapse

Constructor Details

#initializeLoggerRegistrationBuffer

Returns a new instance of LoggerRegistrationBuffer.



204
205
206
207
208
# File 'lib/smplkit/buffers.rb', line 204

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

Instance Method Details

#add(source) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/smplkit/buffers.rb', line 210

def add(source)
  @lock.synchronize do
    next if @seen.key?(source.name)

    @seen[source.name] = source.resolved_level
    item = { "id" => source.name, "resolved_level" => source.resolved_level&.to_s }
    item["level"] = source.level&.to_s if source.level
    item["service"] = source.service if source.service
    item["environment"] = source.environment if source.environment
    @pending << item
  end
end

#drainObject



223
224
225
226
227
228
229
# File 'lib/smplkit/buffers.rb', line 223

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

#pending_countObject



231
232
233
# File 'lib/smplkit/buffers.rb', line 231

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