Class: MessagePack::RPC::SessionPool
- Inherits:
-
Object
- Object
- MessagePack::RPC::SessionPool
- Includes:
- LoopUtil
- Defined in:
- lib/msgpack/rpc/session_pool.rb
Overview
SessionPool is usable for connection pooling. You can get pooled Session using get_session method. Note that SessionPool includes LoopUtil.
Direct Known Subclasses
Instance Attribute Summary
Attributes included from LoopUtil
Instance Method Summary collapse
- #close ⇒ Object
-
#get_session(arg1, arg2 = nil) ⇒ Object
(also: #get_session_addr)
1.
-
#initialize(arg1 = nil, arg2 = nil) ⇒ SessionPool
constructor
1.
Methods included from LoopUtil
#run, #start_timer, #stop, #submit
Constructor Details
#initialize(arg1 = nil, arg2 = nil) ⇒ SessionPool
-
initialize(builder, loop = Loop.new)
-
initialize(loop = Loop.new)
Creates an SessionPool.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/msgpack/rpc/session_pool.rb', line 30 def initialize(arg1=nil, arg2=nil) if arg1.respond_to?(:build_transport) # 1. builder = arg1 loop = arg2 || Loop.new else # 2. builder = TCPTransport.new loop = arg1 || Loop.new end @builder = builder @loop = loop @pool = {} @timer = Timer.new(1, true, &method(:step_timeout)) loop.attach(@timer) end |
Instance Method Details
#close ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/msgpack/rpc/session_pool.rb', line 72 def close @pool.reject! {|addr, s| s.close true } @timer.detach if @timer.attached? nil end |
#get_session(arg1, arg2 = nil) ⇒ Object Also known as: get_session_addr
-
get_session(address)
-
get_session(host, port)
Returns pooled Session. If there are no pooled Session for the specified address, this method creates the Session and pools it.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/msgpack/rpc/session_pool.rb', line 55 def get_session(arg1, arg2=nil) if arg2.nil? # 1. addr = arg1 else # 2. host = arg1 port = arg2 addr = Address.new(host, port) end @pool[addr] ||= Session.new(@builder, addr, @loop) end |