Software /
code /
prosody
Comparison
net/server_select.lua @ 7098:5286e79c6829
Merge 0.9->0.10
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 22 Jan 2016 14:49:05 +0100 |
parent | 7066:7d5a1fb9bf9e |
parent | 7097:5a71aaa2c542 |
child | 7099:8e64e7b82928 |
child | 7323:a1570219b865 |
comparison
equal
deleted
inserted
replaced
7090:7aa37d70944b | 7098:5286e79c6829 |
---|---|
87 local _sendlist | 87 local _sendlist |
88 local _socketlist | 88 local _socketlist |
89 local _closelist | 89 local _closelist |
90 local _readtimes | 90 local _readtimes |
91 local _writetimes | 91 local _writetimes |
92 local _fullservers | |
92 | 93 |
93 --// simple data types //-- | 94 --// simple data types //-- |
94 | 95 |
95 local _ | 96 local _ |
96 local _readlistlen | 97 local _readlistlen |
101 local _readtraffic | 102 local _readtraffic |
102 | 103 |
103 local _selecttimeout | 104 local _selecttimeout |
104 local _sleeptime | 105 local _sleeptime |
105 local _tcpbacklog | 106 local _tcpbacklog |
107 local _accepretry | |
106 | 108 |
107 local _starttime | 109 local _starttime |
108 local _currenttime | 110 local _currenttime |
109 | 111 |
110 local _maxsendlen | 112 local _maxsendlen |
129 _timerlist = { } -- array of timer functions | 131 _timerlist = { } -- array of timer functions |
130 _socketlist = { } -- key = socket, value = wrapped socket (handlers) | 132 _socketlist = { } -- key = socket, value = wrapped socket (handlers) |
131 _readtimes = { } -- key = handler, value = timestamp of last data reading | 133 _readtimes = { } -- key = handler, value = timestamp of last data reading |
132 _writetimes = { } -- key = handler, value = timestamp of last data writing/sending | 134 _writetimes = { } -- key = handler, value = timestamp of last data writing/sending |
133 _closelist = { } -- handlers to close | 135 _closelist = { } -- handlers to close |
136 _fullservers = { } -- servers in a paused state while there are too many clients | |
134 | 137 |
135 _readlistlen = 0 -- length of readlist | 138 _readlistlen = 0 -- length of readlist |
136 _sendlistlen = 0 -- length of sendlist | 139 _sendlistlen = 0 -- length of sendlist |
137 _timerlistlen = 0 -- lenght of timerlist | 140 _timerlistlen = 0 -- lenght of timerlist |
138 | 141 |
140 _readtraffic = 0 | 143 _readtraffic = 0 |
141 | 144 |
142 _selecttimeout = 1 -- timeout of socket.select | 145 _selecttimeout = 1 -- timeout of socket.select |
143 _sleeptime = 0 -- time to wait at the end of every loop | 146 _sleeptime = 0 -- time to wait at the end of every loop |
144 _tcpbacklog = 128 -- some kind of hint to the OS | 147 _tcpbacklog = 128 -- some kind of hint to the OS |
148 _accepretry = 10 -- seconds to wait until the next attempt of a full server to accept | |
145 | 149 |
146 _maxsendlen = 51000 * 1024 -- max len of send buffer | 150 _maxsendlen = 51000 * 1024 -- max len of send buffer |
147 _maxreadlen = 25000 * 1024 -- max len of read buffer | 151 _maxreadlen = 25000 * 1024 -- max len of read buffer |
148 | 152 |
149 _checkinterval = 30 -- interval in secs to check idle clients | 153 _checkinterval = 30 -- interval in secs to check idle clients |
208 _socketlist[ socket ] = nil | 212 _socketlist[ socket ] = nil |
209 socket:close( ) | 213 socket:close( ) |
210 socket = nil; | 214 socket = nil; |
211 end | 215 end |
212 handler.paused = true; | 216 handler.paused = true; |
217 out_put("server.lua: server [", ip, "]:", serverport, " paused") | |
213 end | 218 end |
214 end | 219 end |
215 handler.resume = function( ) | 220 handler.resume = function( ) |
216 if handler.paused then | 221 if handler.paused then |
217 if not socket then | 222 if not socket then |
218 socket = socket_bind( ip, serverport, _tcpbacklog ); | 223 socket = socket_bind( ip, serverport, _tcpbacklog ); |
219 socket:settimeout( 0 ) | 224 socket:settimeout( 0 ) |
220 end | 225 end |
221 _readlistlen = addsocket(_readlist, socket, _readlistlen) | 226 _readlistlen = addsocket(_readlist, socket, _readlistlen) |
222 _socketlist[ socket ] = handler | 227 _socketlist[ socket ] = handler |
228 _fullservers[ handler ] = nil | |
223 handler.paused = false; | 229 handler.paused = false; |
230 out_put("server.lua: server [", ip, "]:", serverport, " resumed") | |
224 end | 231 end |
225 end | 232 end |
226 handler.ip = function( ) | 233 handler.ip = function( ) |
227 return ip | 234 return ip |
228 end | 235 end |
233 return socket | 240 return socket |
234 end | 241 end |
235 handler.readbuffer = function( ) | 242 handler.readbuffer = function( ) |
236 if _readlistlen >= _maxselectlen or _sendlistlen >= _maxselectlen then | 243 if _readlistlen >= _maxselectlen or _sendlistlen >= _maxselectlen then |
237 handler.pause( ) | 244 handler.pause( ) |
245 _fullservers[ handler ] = _currenttime | |
238 out_put( "server.lua: refused new client connection: server full" ) | 246 out_put( "server.lua: refused new client connection: server full" ) |
239 return false | 247 return false |
240 end | 248 end |
241 local client, err = accept( socket ) -- try to accept | 249 local client, err = accept( socket ) -- try to accept |
242 if client then | 250 if client then |
251 return dispatch( handler ); | 259 return dispatch( handler ); |
252 end | 260 end |
253 return; | 261 return; |
254 elseif err then -- maybe timeout or something else | 262 elseif err then -- maybe timeout or something else |
255 out_put( "server.lua: error with new client connection: ", tostring(err) ) | 263 out_put( "server.lua: error with new client connection: ", tostring(err) ) |
264 handler.pause( ) | |
265 _fullservers[ handler ] = _currenttime | |
256 return false | 266 return false |
257 end | 267 end |
258 end | 268 end |
259 return handler | 269 return handler |
260 end | 270 end |
263 | 273 |
264 if socket:getfd() >= _maxfd then | 274 if socket:getfd() >= _maxfd then |
265 out_error("server.lua: Disallowed FD number: "..socket:getfd()) -- PROTIP: Switch to libevent | 275 out_error("server.lua: Disallowed FD number: "..socket:getfd()) -- PROTIP: Switch to libevent |
266 socket:close( ) -- Should we send some kind of error here? | 276 socket:close( ) -- Should we send some kind of error here? |
267 if server then | 277 if server then |
278 _fullservers[ server ] = _currenttime | |
268 server.pause( ) | 279 server.pause( ) |
269 end | 280 end |
270 return nil, nil, "fd-too-large" | 281 return nil, nil, "fd-too-large" |
271 end | 282 end |
272 socket:settimeout( 0 ) | 283 socket:settimeout( 0 ) |
804 send_timeout = _sendtimeout; | 815 send_timeout = _sendtimeout; |
805 read_timeout = _readtimeout; | 816 read_timeout = _readtimeout; |
806 max_connections = _maxselectlen; | 817 max_connections = _maxselectlen; |
807 max_ssl_handshake_roundtrips = _maxsslhandshake; | 818 max_ssl_handshake_roundtrips = _maxsslhandshake; |
808 highest_allowed_fd = _maxfd; | 819 highest_allowed_fd = _maxfd; |
820 accept_retry_interval = _accepretry; | |
809 } | 821 } |
810 end | 822 end |
811 | 823 |
812 changesettings = function( new ) | 824 changesettings = function( new ) |
813 if type( new ) ~= "table" then | 825 if type( new ) ~= "table" then |
819 _maxreadlen = tonumber( new.max_receive_buffer_size ) or _maxreadlen | 831 _maxreadlen = tonumber( new.max_receive_buffer_size ) or _maxreadlen |
820 _checkinterval = tonumber( new.select_idle_check_interval ) or _checkinterval | 832 _checkinterval = tonumber( new.select_idle_check_interval ) or _checkinterval |
821 _tcpbacklog = tonumber( new.tcp_backlog ) or _tcpbacklog | 833 _tcpbacklog = tonumber( new.tcp_backlog ) or _tcpbacklog |
822 _sendtimeout = tonumber( new.send_timeout ) or _sendtimeout | 834 _sendtimeout = tonumber( new.send_timeout ) or _sendtimeout |
823 _readtimeout = tonumber( new.read_timeout ) or _readtimeout | 835 _readtimeout = tonumber( new.read_timeout ) or _readtimeout |
836 _accepretry = tonumber( new.accept_retry_interval ) or _accepretry | |
824 _maxselectlen = new.max_connections or _maxselectlen | 837 _maxselectlen = new.max_connections or _maxselectlen |
825 _maxsslhandshake = new.max_ssl_handshake_roundtrips or _maxsslhandshake | 838 _maxsslhandshake = new.max_ssl_handshake_roundtrips or _maxsslhandshake |
826 _maxfd = new.highest_allowed_fd or _maxfd | 839 _maxfd = new.highest_allowed_fd or _maxfd |
827 return true | 840 return true |
828 end | 841 end |
907 if t then next_timer_time = math_min(next_timer_time, t); end | 920 if t then next_timer_time = math_min(next_timer_time, t); end |
908 end | 921 end |
909 _timer = _currenttime | 922 _timer = _currenttime |
910 else | 923 else |
911 next_timer_time = next_timer_time - (_currenttime - _timer); | 924 next_timer_time = next_timer_time - (_currenttime - _timer); |
925 end | |
926 | |
927 for server, paused_time in pairs( _fullservers ) do | |
928 if _currenttime - paused_time > _accepretry then | |
929 _fullservers[ server ] = nil; | |
930 server.resume(); | |
931 end | |
912 end | 932 end |
913 | 933 |
914 -- wait some time (0 by default) | 934 -- wait some time (0 by default) |
915 socket_sleep( _sleeptime ) | 935 socket_sleep( _sleeptime ) |
916 until quitting; | 936 until quitting; |