Class: Smplkit::Management::ContextRegistrationBuffer
- Inherits:
-
Object
- Object
- Smplkit::Management::ContextRegistrationBuffer
- Defined in:
- lib/smplkit/management/buffer.rb
Overview
Thread-safe batch buffer for context registration.
Instance Method Summary collapse
- #drain ⇒ Object
-
#initialize ⇒ ContextRegistrationBuffer
constructor
A new instance of ContextRegistrationBuffer.
- #observe(contexts) ⇒ Object
- #pending_count ⇒ Object
Constructor Details
#initialize ⇒ ContextRegistrationBuffer
Returns a new instance of ContextRegistrationBuffer.
12 13 14 15 16 |
# File 'lib/smplkit/management/buffer.rb', line 12 def initialize @seen = {} @pending = [] @lock = Mutex.new end |
Instance Method Details
#drain ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/smplkit/management/buffer.rb', line 31 def drain @lock.synchronize do batch = @pending @pending = [] batch end end |
#observe(contexts) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/smplkit/management/buffer.rb', line 18 def observe(contexts) @lock.synchronize do contexts.each do |ctx| cache_key = [ctx.type, ctx.key] next if @seen.key?(cache_key) @seen.shift if @seen.size >= CONTEXT_REGISTRATION_LRU_SIZE @seen[cache_key] = ctx.attributes @pending << { "type" => ctx.type, "key" => ctx.key, "attributes" => ctx.attributes.dup } end end end |
#pending_count ⇒ Object
39 40 41 |
# File 'lib/smplkit/management/buffer.rb', line 39 def pending_count @lock.synchronize { @pending.length } end |