Class: OnyxCord::Internal::EventExecutor::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/onyxcord/internal/event_executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, queue_size: nil) ⇒ Pool

Returns a new instance of Pool.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/onyxcord/internal/event_executor.rb', line 25

def initialize(size:, queue_size: nil)
  raise ArgumentError, 'Pool size must be greater than zero' unless size.positive?

  @size = size
  @queue = queue_size ? SizedQueue.new(queue_size) : Queue.new
  @closed = false
  @workers = []

  start_workers
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



23
24
25
# File 'lib/onyxcord/internal/event_executor.rb', line 23

def queue
  @queue
end

Instance Method Details

#post(&block) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/onyxcord/internal/event_executor.rb', line 36

def post(&block)
  raise ArgumentError, 'EventExecutor::Pool#post requires a block' unless block
  raise 'Event executor has been shut down' if @closed

  if @queue.respond_to?(:max) && @queue.size >= @queue.max
    OnyxCord::LOGGER.warn('Event executor queue is full — dropping event') if defined?(OnyxCord::LOGGER)
    return
  end

  @queue << block
end

#queue_sizeObject



48
49
50
# File 'lib/onyxcord/internal/event_executor.rb', line 48

def queue_size
  @queue.size
end

#shutdownObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/onyxcord/internal/event_executor.rb', line 56

def shutdown
  return if @closed

  @closed = true
  @size.times { @queue << STOP }
  @workers.each do |w|
    w.join unless w == Thread.current
  rescue Interrupt, SystemExit
    raise
  rescue StandardError
    nil
  end
end

#threadsObject



52
53
54
# File 'lib/onyxcord/internal/event_executor.rb', line 52

def threads
  @workers
end