Class: Sqreen::Kit::Signals::BatchCollector

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/sqreen/kit/signals/batch_collector.rb

Defined Under Namespace

Classes: ProcessingLoop, QueueWithTimeout

Constant Summary collapse

EXIT_SENTINEL =
Object.new.freeze
DEFAULT_MAX_DELAY_S =
45
DEFAULT_FLUSH_SIZE =
30
DEFAULT_MAX_BATCH_SIZE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_sig_client, opts = {}) ⇒ BatchCollector

Returns a new instance of BatchCollector.

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 26

def initialize(auth_sig_client, opts = {})
  @auth_sig_client = auth_sig_client
  @flush_size = opts[:flush_size] || DEFAULT_FLUSH_SIZE
  @max_batch_size = opts[:max_batch_size] || DEFAULT_MAX_BATCH_SIZE
  @max_delay_s = opts[:max_delay_s] || DEFAULT_MAX_DELAY_S
  @queue = QueueWithTimeout.new
  @thread = nil

  if max_batch_size < flush_size # rubocop:disable Style/GuardClause
    raise ArgumentError, 'max batch size < flush size'
  end
end

Instance Attribute Details

#auth_sig_clientObject (readonly)

Returns the value of attribute auth_sig_client.



19
20
21
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 19

def auth_sig_client
  @auth_sig_client
end

#flush_sizeObject (readonly)

Returns the value of attribute flush_size.



19
20
21
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 19

def flush_size
  @flush_size
end

#max_batch_sizeObject (readonly)

Returns the value of attribute max_batch_size.



19
20
21
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 19

def max_batch_size
  @max_batch_size
end

#max_delay_sObject (readonly)

Returns the value of attribute max_delay_s.



19
20
21
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 19

def max_delay_s
  @max_delay_s
end

#queueObject (readonly)

Returns the value of attribute queue.



19
20
21
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 19

def queue
  @queue
end

Instance Method Details

#<<(signal_or_trace) ⇒ Object



39
40
41
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 39

def <<(signal_or_trace)
  @queue << signal_or_trace
end

#closeObject



55
56
57
58
59
60
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 55

def close
  return if @thread.nil?

  @queue << EXIT_SENTINEL
  @thread.join
end

#running?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 50

def running?
  return false if thread.nil?
  @thread.alive?
end

#startObject



43
44
45
46
47
48
# File 'lib/sqreen/kit/signals/batch_collector.rb', line 43

def start
  @processing_loop = ProcessingLoop.new(self)
  @thread = Thread.new do
    @processing_loop.run
  end
end