net/server_event.lua

changeset 3237
721a83a3beb9
parent 3149
5aca2f01c0f9
child 3387
ebb8d0f9a177
equal deleted inserted replaced
3236:4727b2b0e069 3237:721a83a3beb9
18 local SCRIPT_AUTHOR = "blastbeat" 18 local SCRIPT_AUTHOR = "blastbeat"
19 local LAST_MODIFIED = "2009/11/20" 19 local LAST_MODIFIED = "2009/11/20"
20 20
21 local cfg = { 21 local cfg = {
22 MAX_CONNECTIONS = 100000, -- max per server connections (use "ulimit -n" on *nix) 22 MAX_CONNECTIONS = 100000, -- max per server connections (use "ulimit -n" on *nix)
23 MAX_HANDSHAKE_ATTEMPS = 1000, -- attempts to finish ssl handshake 23 MAX_HANDSHAKE_ATTEMPTS= 1000, -- attempts to finish ssl handshake
24 HANDSHAKE_TIMEOUT = 60, -- timout in seconds per handshake attempt 24 HANDSHAKE_TIMEOUT = 60, -- timeout in seconds per handshake attempt
25 MAX_READ_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes allowed to read from sockets 25 MAX_READ_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes allowed to read from sockets
26 MAX_SEND_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes size of write buffer (for writing on sockets) 26 MAX_SEND_LENGTH = 1024 * 1024 * 1024 * 1024, -- max bytes size of write buffer (for writing on sockets)
27 ACCEPT_DELAY = 10, -- seconds to wait until the next attempt of a full server to accept 27 ACCEPT_DELAY = 10, -- seconds to wait until the next attempt of a full server to accept
28 READ_TIMEOUT = 60 * 60 * 6, -- timeout in seconds for read data from socket 28 READ_TIMEOUT = 60 * 60 * 6, -- timeout in seconds for read data from socket
29 WRITE_TIMEOUT = 180, -- timeout in seconds for write data on socket 29 WRITE_TIMEOUT = 180, -- timeout in seconds for write data on socket
134 return true 134 return true
135 end 135 end
136 136
137 function interface_mt:_start_connection(plainssl) -- should be called from addclient 137 function interface_mt:_start_connection(plainssl) -- should be called from addclient
138 local callback = function( event ) 138 local callback = function( event )
139 if EV_TIMEOUT == event then -- timout during connection 139 if EV_TIMEOUT == event then -- timeout during connection
140 self.fatalerror = "connection timeout" 140 self.fatalerror = "connection timeout"
141 self:ontimeout() -- call timeout listener 141 self:ontimeout() -- call timeout listener
142 self:_close() 142 self:_close()
143 debug( "new connection failed. id:", self.id, "error:", self.fatalerror ) 143 debug( "new connection failed. id:", self.id, "error:", self.fatalerror )
144 else 144 else
194 self.conn:settimeout( 0 ) -- set non blocking 194 self.conn:settimeout( 0 ) -- set non blocking
195 local handshakecallback = coroutine_wrap( 195 local handshakecallback = coroutine_wrap(
196 function( event ) 196 function( event )
197 local _, err 197 local _, err
198 local attempt = 0 198 local attempt = 0
199 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPS 199 local maxattempt = cfg.MAX_HANDSHAKE_ATTEMPTS
200 while attempt < maxattempt do -- no endless loop 200 while attempt < maxattempt do -- no endless loop
201 attempt = attempt + 1 201 attempt = attempt + 1
202 debug( "ssl handshake of client with id:"..tostring(self).."attemp:"..attempt ) 202 debug( "ssl handshake of client with id:"..tostring(self)..", attempt:"..attempt )
203 if attempt > maxattempt then 203 if attempt > maxattempt then
204 self.fatalerror = "max handshake attemps exceeded" 204 self.fatalerror = "max handshake attempts exceeded"
205 elseif EV_TIMEOUT == event then 205 elseif EV_TIMEOUT == event then
206 self.fatalerror = "timeout during handshake" 206 self.fatalerror = "timeout during handshake"
207 else 207 else
208 _, err = self.conn:dohandshake( ) 208 _, err = self.conn:dohandshake( )
209 if not err then 209 if not err then
568 interface:_close() 568 interface:_close()
569 interface.eventwritetimeout = nil 569 interface.eventwritetimeout = nil
570 return -1; 570 return -1;
571 end 571 end
572 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event 572 interface.eventwritetimeout = addevent( base, nil, EV_TIMEOUT, callback, cfg.WRITE_TIMEOUT ) -- reg a new timeout event
573 debug( "wantread during write attemp, reg it in readcallback but dont know what really happens next..." ) 573 debug( "wantread during write attempt, reg it in readcallback but dont know what really happens next..." )
574 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent 574 -- hopefully this works with luasec; its simply not possible to use 2 different write events on a socket in luaevent
575 return -1 575 return -1
576 end 576 end
577 return EV_WRITE, cfg.WRITE_TIMEOUT 577 return EV_WRITE, cfg.WRITE_TIMEOUT
578 else -- connection was closed during writing or fatal error 578 else -- connection was closed during writing or fatal error
629 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT, 629 interface.eventreadtimeout = addevent( base, nil, EV_TIMEOUT,
630 function( ) 630 function( )
631 interface:_close() 631 interface:_close()
632 end, cfg.READ_TIMEOUT 632 end, cfg.READ_TIMEOUT
633 ) 633 )
634 debug( "wantwrite during read attemp, reg it in writecallback but dont know what really happens next..." ) 634 debug( "wantwrite during read attempt, reg it in writecallback but dont know what really happens next..." )
635 -- to be honest i dont know what happens next, if it is allowed to first read, the write etc... 635 -- to be honest i dont know what happens next, if it is allowed to first read, the write etc...
636 else -- connection was closed or fatal error 636 else -- connection was closed or fatal error
637 interface.fatalerror = err 637 interface.fatalerror = err
638 debug( "connection failed in read event:", interface.fatalerror ) 638 debug( "connection failed in read event:", interface.fatalerror )
639 interface:_close() 639 interface:_close()
691 local client, err = server:accept() -- try to accept; TODO: check err 691 local client, err = server:accept() -- try to accept; TODO: check err
692 while client do 692 while client do
693 if interface._connections >= cfg.MAX_CONNECTIONS then 693 if interface._connections >= cfg.MAX_CONNECTIONS then
694 client:close( ) -- refuse connection 694 client:close( ) -- refuse connection
695 debug( "maximal connections reached, refuse client connection; accept delay:", delay ) 695 debug( "maximal connections reached, refuse client connection; accept delay:", delay )
696 return EV_TIMEOUT, delay -- delay for next accept attemp 696 return EV_TIMEOUT, delay -- delay for next accept attempt
697 end 697 end
698 local client_ip, client_port = client:getpeername( ) 698 local client_ip, client_port = client:getpeername( )
699 interface._connections = interface._connections + 1 -- increase connection count 699 interface._connections = interface._connections + 1 -- increase connection count
700 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, nil, sslctx ) 700 local clientinterface = handleclient( client, client_ip, client_port, interface, pattern, listener, nil, sslctx )
701 --vdebug( "client id:", clientinterface, "startssl:", startssl ) 701 --vdebug( "client id:", clientinterface, "startssl:", startssl )

mercurial