Class: ProcessorPool

Inherits:
Object
  • Object
show all
Defined in:
lib/wattics-api-client/processor_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(agent, agent_thread_group, maximum_parallel_senders = 0) ⇒ ProcessorPool

Returns a new instance of ProcessorPool.



2
3
4
5
6
7
8
# File 'lib/wattics-api-client/processor_pool.rb', line 2

def initialize(agent, agent_thread_group, maximum_parallel_senders = 0)
  maximum_parallel_senders > 0 ? @max_processors = maximum_parallel_senders.freeze : @max_processors = (2 * Concurrent.processor_count).freeze
  @agent = agent
  @processors = {}
  @processor_thread_group = agent_thread_group
  @mutex = Mutex.new
end

Instance Method Details

#get_processor(channel_id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/wattics-api-client/processor_pool.rb', line 10

def get_processor(channel_id)
  @mutex.synchronize do
    processor = @processors[channel_id]
    return processor unless processor.nil?
    if @processors.size < @max_processors
      @processors[channel_id] = spawn_new_processor
      @processors[channel_id]
    else
      rebind_processor_to_channel_id(channel_id)
    end
  end
end