Skip to content

Commit

Permalink
inactivity timeouts and inactivity error callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Feb 5, 2010
1 parent b4d1001 commit 678fbae
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
20 changes: 19 additions & 1 deletion README.md
Expand Up @@ -127,7 +127,7 @@ rejected or a connection timeout occurs:
end
end

proxy_connect_error |remote|
proxy_connect_error do |remote|
puts "error connecting to #{remote}"
end

Expand All @@ -137,6 +137,24 @@ representing the number of seconds to wait before a connection is
established. Hard connection rejections always trigger the callback, even
when no `:connect_timeout` is provided.

Inactivity Timeouts
-------------------

Inactivity timeouts work like connect timeouts but are triggered after
the configured amount of time elapses without receiving the first byte
of data from an already connected server:

proxy do |data|
{ :remote => 'localhost:1234', :inactivity_timeout => 10.0 }
end

proxy_inactivity_error do |remote|
puts "#{remote} did not send any data for 10 seconds"
end

If no `:inactivity_timeout` is provided, the `proxy_inactivity_error`
callback is never triggered.

Contribute
----------

Expand Down
13 changes: 13 additions & 0 deletions lib/proxymachine.rb
Expand Up @@ -78,13 +78,22 @@ def self.connect_error_callback
@@connect_error_callback
end

def self.set_inactivity_error_callback(&block)
@@inactivity_error_callback = block
end

def self.inactivity_error_callback
@@inactivity_error_callback
end

def self.run(name, host, port)
@@totalcounter = 0
@@maxcounter = 0
@@counter = 0
@@name = name
@@listen = "#{host}:#{port}"
@@connect_error_callback ||= proc { |remote| }
@@inactivity_error_callback = proc { |remote| }
self.update_procline
EM.epoll

Expand Down Expand Up @@ -120,4 +129,8 @@ def proxy(&block)
def proxy_connect_error(&block)
ProxyMachine.set_connect_error_callback(&block)
end

def proxy_inactivity_error(&block)
ProxyMachine.set_inactivity_error_callback(&block)
end
end
10 changes: 10 additions & 0 deletions lib/proxymachine/client_connection.rb
Expand Up @@ -14,6 +14,7 @@ def post_init
@tries = 0
@connected = false
@connect_timeout = nil
@inactivity_timeout = nil
ProxyMachine.incr
end

Expand Down Expand Up @@ -52,6 +53,8 @@ def establish_remote_server
if reply = commands[:reply]
send_data(reply)
end
@connect_timeout = commands[:connect_timeout]
@inactivity_timeout = commands[:inactivity_timeout]
connect_to_server
elsif close = commands[:close]
if close == true
Expand All @@ -73,6 +76,7 @@ def connect_to_server
host, port = @remote
@server_side = ServerConnection.request(host, port, self)
@server_side.pending_connect_timeout = @connect_timeout
@server_side.comm_inactivity_timeout = @inactivity_timeout
end

# Called by the server side immediately after the server connection was
Expand Down Expand Up @@ -100,6 +104,12 @@ def server_connection_failed
end
end

def server_inactivity_timeout
@server_side = nil
close_connection
ProxyMachine.inactivity_error_callback.call(@remote.join(':'))
end

def unbind
@server_side.close_connection_after_writing if @server_side
ProxyMachine.decr
Expand Down
13 changes: 10 additions & 3 deletions lib/proxymachine/server_connection.rb
Expand Up @@ -7,20 +7,27 @@ def self.request(host, port, client_side)
def initialize(conn)
@client_side = conn
@connected = false
@data_received = false
end

def post_init
proxy_incoming_to(@client_side, 10240)
def receive_data(data)
fail "receive_data called after raw proxy enabled" if @data_received
@data_received = true
@client_side.send_data(data)
proxy_incoming_to @client_side
end

def connection_completed
@connected = true
@connected = Time.now
@client_side.server_connection_success
end

def unbind
if !@connected
@client_side.server_connection_failed
elsif !@data_received && (Time.now - @connected) >= comm_inactivity_timeout
# EM aborted the connection due to an inactivity timeout
@client_side.server_inactivity_timeout
else
@client_side.close_connection_after_writing
end
Expand Down

0 comments on commit 678fbae

Please sign in to comment.