Software / code / prosody
Comparison
net/server_select.lua @ 5338:3df649ec43ea
net.server_select: Optimization, clean bufferqueue when it makes sense.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 01 Mar 2013 11:41:26 +0100 |
| parent | 5337:c22dd451487f |
| child | 5385:d663483036db |
comparison
equal
deleted
inserted
replaced
| 5337:c22dd451487f | 5338:3df649ec43ea |
|---|---|
| 7 | 7 |
| 8 -- // wrapping luadch stuff // -- | 8 -- // wrapping luadch stuff // -- |
| 9 | 9 |
| 10 local use = function( what ) | 10 local use = function( what ) |
| 11 return _G[ what ] | 11 return _G[ what ] |
| 12 end | |
| 13 local clean = function( tbl ) | |
| 14 for i, k in pairs( tbl ) do | |
| 15 tbl[ i ] = nil | |
| 16 end | |
| 17 end | 12 end |
| 18 | 13 |
| 19 local log, table_concat = require ("util.logger").init("socket"), table.concat; | 14 local log, table_concat = require ("util.logger").init("socket"), table.concat; |
| 20 local out_put = function (...) return log("debug", table_concat{...}); end | 15 local out_put = function (...) return log("debug", table_concat{...}); end |
| 21 local out_error = function (...) return log("warn", table_concat{...}); end | 16 local out_error = function (...) return log("warn", table_concat{...}); end |
| 115 | 110 |
| 116 local _checkinterval | 111 local _checkinterval |
| 117 local _sendtimeout | 112 local _sendtimeout |
| 118 local _readtimeout | 113 local _readtimeout |
| 119 | 114 |
| 120 local _cleanqueue | |
| 121 | |
| 122 local _timer | 115 local _timer |
| 123 | 116 |
| 124 local _maxselectlen | 117 local _maxselectlen |
| 125 local _maxfd | 118 local _maxfd |
| 126 | 119 |
| 151 _maxreadlen = 25000 * 1024 -- max len of read buffer | 144 _maxreadlen = 25000 * 1024 -- max len of read buffer |
| 152 | 145 |
| 153 _checkinterval = 1200000 -- interval in secs to check idle clients | 146 _checkinterval = 1200000 -- interval in secs to check idle clients |
| 154 _sendtimeout = 60000 -- allowed send idle time in secs | 147 _sendtimeout = 60000 -- allowed send idle time in secs |
| 155 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs | 148 _readtimeout = 6 * 60 * 60 -- allowed read idle time in secs |
| 156 | |
| 157 _cleanqueue = false -- clean bufferqueue after using | |
| 158 | 149 |
| 159 _maxfd = luasocket._SETSIZE or 1024 -- We should ignore this on Windows. Perhaps by simply setting it to math.huge or something. | 150 _maxfd = luasocket._SETSIZE or 1024 -- We should ignore this on Windows. Perhaps by simply setting it to math.huge or something. |
| 160 _maxselectlen = luasocket._SETSIZE or 1024 -- But this still applies on Windows | 151 _maxselectlen = luasocket._SETSIZE or 1024 -- But this still applies on Windows |
| 161 | 152 |
| 162 _maxsslhandshake = 30 -- max handshake round-trips | 153 _maxsslhandshake = 30 -- max handshake round-trips |
| 347 return false, "setoption not implemented"; | 338 return false, "setoption not implemented"; |
| 348 end | 339 end |
| 349 handler.force_close = function ( self, err ) | 340 handler.force_close = function ( self, err ) |
| 350 if bufferqueuelen ~= 0 then | 341 if bufferqueuelen ~= 0 then |
| 351 out_put("server.lua: discarding unwritten data for ", tostring(ip), ":", tostring(clientport)) | 342 out_put("server.lua: discarding unwritten data for ", tostring(ip), ":", tostring(clientport)) |
| 352 for i = bufferqueuelen, 1, -1 do | |
| 353 bufferqueue[i] = nil; | |
| 354 end | |
| 355 bufferqueuelen = 0; | 343 bufferqueuelen = 0; |
| 356 end | 344 end |
| 357 return self:close(err); | 345 return self:close(err); |
| 358 end | 346 end |
| 359 handler.close = function( self, err ) | 347 handler.close = function( self, err ) |
| 511 buffer = table_concat( bufferqueue, "", 1, bufferqueuelen ) | 499 buffer = table_concat( bufferqueue, "", 1, bufferqueuelen ) |
| 512 succ, err, byte = send( socket, buffer, 1, bufferlen ) | 500 succ, err, byte = send( socket, buffer, 1, bufferlen ) |
| 513 count = ( succ or byte or 0 ) * STAT_UNIT | 501 count = ( succ or byte or 0 ) * STAT_UNIT |
| 514 sendtraffic = sendtraffic + count | 502 sendtraffic = sendtraffic + count |
| 515 _sendtraffic = _sendtraffic + count | 503 _sendtraffic = _sendtraffic + count |
| 516 _ = _cleanqueue and clean( bufferqueue ) | 504 for i = bufferqueuelen,1,-1 do |
| 505 bufferqueue[ i ] = nil | |
| 506 end | |
| 517 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) ) | 507 --out_put( "server.lua: sended '", buffer, "', bytes: ", tostring(succ), ", error: ", tostring(err), ", part: ", tostring(byte), ", to: ", tostring(ip), ":", tostring(clientport) ) |
| 518 else | 508 else |
| 519 succ, err, count = false, "unexpected close", 0; | 509 succ, err, count = false, "unexpected close", 0; |
| 520 end | 510 end |
| 521 if succ then -- sending succesful | 511 if succ then -- sending succesful |
| 777 _socketlist = { } | 767 _socketlist = { } |
| 778 --mem_free( ) | 768 --mem_free( ) |
| 779 end | 769 end |
| 780 | 770 |
| 781 getsettings = function( ) | 771 getsettings = function( ) |
| 782 return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, _cleanqueue, _maxselectlen, _maxsslhandshake, _maxfd | 772 return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, nil, _maxselectlen, _maxsslhandshake, _maxfd |
| 783 end | 773 end |
| 784 | 774 |
| 785 changesettings = function( new ) | 775 changesettings = function( new ) |
| 786 if type( new ) ~= "table" then | 776 if type( new ) ~= "table" then |
| 787 return nil, "invalid settings table" | 777 return nil, "invalid settings table" |
| 791 _maxsendlen = tonumber( new.max_send_buffer_size ) or _maxsendlen | 781 _maxsendlen = tonumber( new.max_send_buffer_size ) or _maxsendlen |
| 792 _maxreadlen = tonumber( new.max_receive_buffer_size ) or _maxreadlen | 782 _maxreadlen = tonumber( new.max_receive_buffer_size ) or _maxreadlen |
| 793 _checkinterval = tonumber( new.select_idle_check_interval ) or _checkinterval | 783 _checkinterval = tonumber( new.select_idle_check_interval ) or _checkinterval |
| 794 _sendtimeout = tonumber( new.send_timeout ) or _sendtimeout | 784 _sendtimeout = tonumber( new.send_timeout ) or _sendtimeout |
| 795 _readtimeout = tonumber( new.read_timeout ) or _readtimeout | 785 _readtimeout = tonumber( new.read_timeout ) or _readtimeout |
| 796 _cleanqueue = new.select_clean_queue | |
| 797 _maxselectlen = new.max_connections or _maxselectlen | 786 _maxselectlen = new.max_connections or _maxselectlen |
| 798 _maxsslhandshake = new.max_ssl_handshake_roundtrips or _maxsslhandshake | 787 _maxsslhandshake = new.max_ssl_handshake_roundtrips or _maxsslhandshake |
| 799 _maxfd = new.highest_allowed_fd or _maxfd | 788 _maxfd = new.highest_allowed_fd or _maxfd |
| 800 return true | 789 return true |
| 801 end | 790 end |
| 844 end | 833 end |
| 845 end | 834 end |
| 846 for handler, err in pairs( _closelist ) do | 835 for handler, err in pairs( _closelist ) do |
| 847 handler.disconnect( )( handler, err ) | 836 handler.disconnect( )( handler, err ) |
| 848 handler:force_close() -- forced disconnect | 837 handler:force_close() -- forced disconnect |
| 849 end | 838 _closelist[ handler ] = nil; |
| 850 clean( _closelist ) | 839 end |
| 851 _currenttime = luasocket_gettime( ) | 840 _currenttime = luasocket_gettime( ) |
| 852 if _currenttime - _timer >= math_min(next_timer_time, 1) then | 841 if _currenttime - _timer >= math_min(next_timer_time, 1) then |
| 853 next_timer_time = math_huge; | 842 next_timer_time = math_huge; |
| 854 for i = 1, _timerlistlen do | 843 for i = 1, _timerlistlen do |
| 855 local t = _timerlist[ i ]( _currenttime ) -- fire timers | 844 local t = _timerlist[ i ]( _currenttime ) -- fire timers |