@@ -16,6 +16,7 @@ using MbedTLS: SSLContext, SSLConfig
16
16
using ConcurrentUtilities: ConcurrentUtilities, Lockable, lock
17
17
using .. IOExtras, .. Streams, .. Messages, .. Parsers, .. Connections, .. Exceptions
18
18
import .. access_threaded, .. SOCKET_TYPE_TLS, .. @logfmt_str
19
+ using .. Accept: acceptmany
19
20
20
21
TRUE (x) = true
21
22
getinet (host:: String , port:: Integer ) = Sockets. InetAddr (parse (IPAddr, host), port)
@@ -366,47 +367,6 @@ function listen!(f, listener::Listener;
366
367
return Server (listener, on_shutdown, conns, tsk)
367
368
end
368
369
369
- using Base: iolock_begin, iolock_end, uv_error, preserve_handle, unpreserve_handle,
370
- StatusClosing, StatusClosed, StatusActive, UV_EAGAIN, UV_ECONNABORTED
371
- using Sockets: accept_nonblock
372
-
373
- function acceptmany (server; MAXSIZE= Sockets. BACKLOG_DEFAULT)
374
- result = Vector {TCPSocket} ()
375
- sizehint! (result, MAXSIZE)
376
- iolock_begin ()
377
- if server. status != StatusActive && server. status != StatusClosing && server. status != StatusClosed
378
- throw (ArgumentError (" server not connected, make sure \" listen\" has been called" ))
379
- end
380
- while isopen (server)
381
- client = TCPSocket ()
382
- err = Sockets. accept_nonblock (server, client)
383
- while err == 0 && length (result) < MAXSIZE # Don't try to fill more than half the buffer
384
- push! (result, client)
385
- client = TCPSocket ()
386
- err = Sockets. accept_nonblock (server, client)
387
- end
388
- if length (result) > 0
389
- iolock_end ()
390
- return result
391
- end
392
- if err != UV_EAGAIN
393
- uv_error (" accept" , err)
394
- end
395
- preserve_handle (server)
396
- lock (server. cond)
397
- iolock_end ()
398
- try
399
- wait (server. cond)
400
- finally
401
- unlock (server. cond)
402
- unpreserve_handle (server)
403
- end
404
- iolock_begin ()
405
- end
406
- uv_error (" accept" , UV_ECONNABORTED)
407
- nothing
408
- end
409
-
410
370
""" "
411
371
Main server loop.
412
372
Accepts new tcp connections and spawns async tasks to handle them."
@@ -421,8 +381,10 @@ function listenloop(f, listener, conns, tcpisvalid,
421
381
while isopen (listener)
422
382
try
423
383
for io in acceptmany (listener. server)
384
+ # I would prefer this inside the async, so we can loop and accept again,
385
+ # but https://github.com/JuliaWeb/HTTP.jl/pull/647/files says it's bad for performance
386
+ max_connections < typemax (Int) && Base. acquire (sem)
424
387
@async begin
425
- max_connections < typemax (Int) && Base. acquire (sem)
426
388
local conn = nothing
427
389
isssl = ! isnothing (listener. ssl)
428
390
try
0 commit comments