Class: Memcached::Connection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- Memcached::Connection
- Defined in:
- lib/remcached/client.rb
Direct Known Subclasses
Constant Summary collapse
- RECONNECT_DELAY =
10
- RECONNECT_JITTER =
5
- RECEIVE_TIMEOUT =
15
- KEEPALIVE_INTERVAL =
5
Class Method Summary collapse
Instance Method Summary collapse
- #connected? ⇒ Boolean
- #connection_completed ⇒ Object
- #keepalive ⇒ Object
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
- #reconnect ⇒ Object
- #send_packet(pkt) ⇒ Object
- #unbind ⇒ Object
Class Method Details
.connect(host, port = 11211, &connect_callback) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/remcached/client.rb', line 5 def self.connect(host, port=11211, &connect_callback) df = EventMachine::DefaultDeferrable.new df.callback &connect_callback EventMachine.connect(host, port, self) do |me| me.instance_eval { @host, @port = host, port @connect_deferrable = df } end end |
Instance Method Details
#connected? ⇒ Boolean
17 18 19 |
# File 'lib/remcached/client.rb', line 17 def connected? @connected end |
#connection_completed ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/remcached/client.rb', line 34 def connection_completed @connected = true @connect_deferrable.succeed(self) @last_receive = Time.now @keepalive_timer = EventMachine::PeriodicTimer.new(1, &method(:keepalive)) end |
#keepalive ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/remcached/client.rb', line 54 def keepalive if @last_receive + RECEIVE_TIMEOUT <= Time.now p :timeout close_connection elsif @last_receive + KEEPALIVE_INTERVAL <= Time.now send_keepalive end end |
#post_init ⇒ Object
27 28 29 30 31 32 |
# File 'lib/remcached/client.rb', line 27 def post_init @recv_buf = "" @recv_state = :header @connected = false @keepalive_timer = nil end |
#receive_data(data) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/remcached/client.rb', line 67 def receive_data(data) @recv_buf += data @last_receive = Time.now done = false while not done if @recv_state == :header && @recv_buf.length >= 24 @received = Response.parse_header(@recv_buf[0..23]) @recv_buf = @recv_buf[24..-1] @recv_state = :body elsif @recv_state == :body && @recv_buf.length >= @received[:total_body_length] @recv_buf = @received.parse_body(@recv_buf) receive_packet(@received) @recv_state = :header else done = true end end end |
#reconnect ⇒ Object
21 22 23 24 25 |
# File 'lib/remcached/client.rb', line 21 def reconnect @connect_deferrable = EventMachine::DefaultDeferrable.new super @host, @port @connect_deferrable end |
#send_packet(pkt) ⇒ Object
63 64 65 |
# File 'lib/remcached/client.rb', line 63 def send_packet(pkt) send_data pkt.to_s end |
#unbind ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/remcached/client.rb', line 44 def unbind @keepalive_timer.cancel if @keepalive_timer @connected = false EventMachine::Timer.new(RECONNECT_DELAY + rand(RECONNECT_JITTER), method(:reconnect)) end |