lib/remcached/client.rb in remcached-0.3.1 vs lib/remcached/client.rb in remcached-0.4.0

- old
+ new

@@ -26,31 +26,49 @@ def post_init @recv_buf = "" @recv_state = :header @connected = false + @keepalive_timer = nil end def connection_completed @connected = true @connect_deferrable.succeed(self) + + @last_receive = Time.now + @keepalive_timer = EventMachine::PeriodicTimer.new(1, &method(:keepalive)) end RECONNECT_DELAY = 10 RECONNECT_JITTER = 5 def unbind + @keepalive_timer.cancel if @keepalive_timer + @connected = false EventMachine::Timer.new(RECONNECT_DELAY + rand(RECONNECT_JITTER), method(:reconnect)) end + RECEIVE_TIMEOUT = 15 + KEEPALIVE_INTERVAL = 5 + def keepalive + if @last_receive + RECEIVE_TIMEOUT <= Time.now + p :timeout + close_connection + elsif @last_receive + KEEPALIVE_INTERVAL <= Time.now + send_keepalive + end + end + def send_packet(pkt) send_data pkt.to_s end def receive_data(data) @recv_buf += data + @last_receive = Time.now done = false while not done if @recv_state == :header && @recv_buf.length >= 24 @@ -125,9 +143,13 @@ end rescue Exception => e $stderr.puts "#{e.class}: #{e}\n" + e.backtrace.join("\n") end end + end + + def send_keepalive + send_request Request::NoOp.new end # Callback will be called multiple times def stats(contents={}, &callback) send_request Request::Stats.new(contents) do |result|