Class: Smplkit::Management::ContextRegistrationBuffer

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

Overview

Thread-safe batch buffer for context registration.

Instance Method Summary collapse

Constructor Details

#initializeContextRegistrationBuffer

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

#drainObject



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_countObject



39
40
41
# File 'lib/smplkit/management/buffer.rb', line 39

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