Class: Qusion::ChannelPool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/qusion/channel_pool.rb

Overview

ChannelPool maintains a pool of AMQP channel objects that can be reused. The motivation behind this is that if you were to use MQ.new to create a new channel for every request, your AMQP broker could be swamped trying to maintain a bunch of channels that you’re only using once.

To use the channel pool, just replace MQ.new in your code with Qusion.channel

# Instead of this:
MQ.new.queue("my-worker-queue")
# Do this:
Qusion.channel.queue("my-worker-queue")

By default, ChannelPool maintains a pool of 5 channels. This can be adjusted with ChannelPool.pool_size=() or Qusion.channel_pool_size() The optimal pool size is not yet known, but I suspect you might need a larger value if using Thin in production, and a smaller value otherwise.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



43
44
45
# File 'lib/qusion/channel_pool.rb', line 43

def pool
  @pool
end

Class Method Details

.pool_sizeObject



32
33
34
# File 'lib/qusion/channel_pool.rb', line 32

def pool_size
  @pool_size ||= 5
end

.pool_size=(new_pool_size) ⇒ Object



27
28
29
30
# File 'lib/qusion/channel_pool.rb', line 27

def pool_size=(new_pool_size)
  reset
  @pool_size = new_pool_size
end

.resetObject



36
37
38
39
# File 'lib/qusion/channel_pool.rb', line 36

def reset
  @pool_size = nil
  instance.reset
end

Instance Method Details

#channelObject



45
46
47
48
49
# File 'lib/qusion/channel_pool.rb', line 45

def channel
  @i ||= 1
  @i = (@i + 1) % pool_size
  pool[@i]
end

#pool_sizeObject



59
60
61
# File 'lib/qusion/channel_pool.rb', line 59

def pool_size
  self.class.pool_size
end

#resetObject



55
56
57
# File 'lib/qusion/channel_pool.rb', line 55

def reset
  @pool = nil
end