@@ -10,7 +10,7 @@ Methods are provided for `eof`, `readavailable`,
10
10
This allows the `Connection` object to act as a proxy for the
11
11
`TCPSocket` or `SSLContext` that it wraps.
12
12
13
- The [`POOL `](@ref) is used to manage connection pooling. Connections
13
+ [`POOLS `](@ref) are used to manage connection pooling. Connections
14
14
are identified by their host, port, whether they require
15
15
ssl verification, and whether they are a client or server connection.
16
16
If a subsequent request matches these properties of a previous connection
@@ -39,7 +39,7 @@ using .ConnectionPools
39
39
"""
40
40
Connection
41
41
42
- A `TCPSocket` or `SSLContext ` connection to a HTTP `host` and `port`.
42
+ A `Sockets. TCPSocket`, `MbedTLS.SSLContext` or `OpenSSL.SSLStream ` connection to a HTTP `host` and `port`.
43
43
44
44
Fields:
45
45
- `host::String`
@@ -49,7 +49,7 @@ Fields:
49
49
- `peerip`, remote IP adress (used for debug/log messages).
50
50
- `peerport`, remote TCP port number (used for debug/log messages).
51
51
- `localport`, local TCP port number (used for debug messages).
52
- - `io::T`, the `TCPSocket` or `SSLContext .
52
+ - `io::T`, the `Sockets. TCPSocket`, `MbedTLS.SSLContext` or `OpenSSL.SSLStream` .
53
53
- `clientconnection::Bool`, whether the Connection was created from client code (as opposed to server code)
54
54
- `buffer::IOBuffer`, left over bytes read from the connection after
55
55
the end of a response header (or chunksize). These bytes are usually
@@ -58,15 +58,15 @@ Fields:
58
58
- `readable`, whether the Connection object is readable
59
59
- `writable`, whether the Connection object is writable
60
60
"""
61
- mutable struct Connection <: IO
61
+ mutable struct Connection{IO_t <: IO } <: IO
62
62
host:: String
63
63
port:: String
64
64
idle_timeout:: Int
65
65
require_ssl_verification:: Bool
66
66
peerip:: IPAddr # for debugging/logging
67
67
peerport:: UInt16 # for debugging/logging
68
68
localport:: UInt16 # debug only
69
- io:: IO
69
+ io:: IO_t
70
70
clientconnection:: Bool
71
71
buffer:: IOBuffer
72
72
timestamp:: Float64
@@ -89,8 +89,8 @@ connectionkey(x::Connection) = (typeof(x.io), x.host, x.port, x.require_ssl_veri
89
89
90
90
Connection (host:: AbstractString , port:: AbstractString ,
91
91
idle_timeout:: Int ,
92
- require_ssl_verification:: Bool , io:: IO , client= true ) =
93
- Connection (host, port, idle_timeout,
92
+ require_ssl_verification:: Bool , io:: T , client= true ) where {T} =
93
+ Connection {T} (host, port, idle_timeout,
94
94
require_ssl_verification,
95
95
safe_getpeername (io)... , localport (io),
96
96
io, client, PipeBuffer (), time (), false , false , IOBuffer (), nothing )
@@ -325,22 +325,35 @@ function purge(c::Connection)
325
325
@ensure bytesavailable (c) == 0
326
326
end
327
327
328
+ const TCP_POOL = Pool (Connection{Sockets. TCPSocket})
329
+ const MbedTLS_SSL_POOL = Pool (Connection{MbedTLS. SSLContext})
330
+ const OpenSSL_SSL_POOL = Pool (Connection{OpenSSL. SSLStream})
328
331
"""
329
- closeall()
332
+ POOLS
330
333
331
- Close all connections in`pool` .
334
+ A dict of global connection pools keeping track of active connections, split by their IO type .
332
335
"""
333
- function closeall ()
334
- ConnectionPools. reset! (POOL)
335
- return
336
+ const POOLS = Dict {DataType,Pool} (
337
+ Sockets. TCPSocket => TCP_POOL,
338
+ MbedTLS. SSLContext => MbedTLS_SSL_POOL,
339
+ OpenSSL. SSLStream => OpenSSL_SSL_POOL,
340
+ )
341
+ getpool (:: Type{Sockets.TCPSocket} ) = TCP_POOL
342
+ getpool (:: Type{MbedTLS.SSLContext} ) = MbedTLS_SSL_POOL
343
+ getpool (:: Type{OpenSSL.SSLStream} ) = OpenSSL_SSL_POOL
344
+ # Fallback for custom connection io types
345
+ # to opt out from locking, define your own `Pool` and add a `getpool` method for your IO type
346
+ const POOLS_LOCK = Threads. ReentrantLock ()
347
+ function getpool (:: Type{T} ) where {T}
348
+ Base. @lock POOLS_LOCK get! (() -> Pool (Connection{T}), POOLS, T):: Pool{Connection{T}}
336
349
end
337
350
338
351
"""
339
- POOL
352
+ closeall()
340
353
341
- Global connection pool keeping track of active connections .
354
+ Close all connections in `POOLS` .
342
355
"""
343
- const POOL = Pool (Connection )
356
+ closeall () = foreach (ConnectionPools . reset!, values (POOLS) )
344
357
345
358
"""
346
359
newconnection(type, host, port) -> Connection
@@ -355,9 +368,9 @@ function newconnection(::Type{T},
355
368
forcenew:: Bool = false ,
356
369
idle_timeout= typemax (Int),
357
370
require_ssl_verification:: Bool = NetworkOptions. verify_host (host, " SSL" ),
358
- kw... ):: Connection where {T <: IO }
371
+ kw... ) where {T <: IO }
359
372
return acquire (
360
- POOL ,
373
+ getpool (T) ,
361
374
(T, host, port, require_ssl_verification, true );
362
375
max_concurrent_connections= Int (connection_limit),
363
376
forcenew= forcenew,
@@ -370,8 +383,8 @@ function newconnection(::Type{T},
370
383
end
371
384
end
372
385
373
- releaseconnection (c:: Connection , reuse) =
374
- release (POOL , connectionkey (c), c; return_for_reuse= reuse)
386
+ releaseconnection (c:: Connection{T} , reuse) where {T} =
387
+ release (getpool (T) , connectionkey (c), c; return_for_reuse= reuse)
375
388
376
389
function keepalive! (tcp)
377
390
@debugv 2 " setting keepalive on tcp socket"
@@ -524,7 +537,7 @@ function sslupgrade(::Type{IOType}, c::Connection,
524
537
host:: AbstractString ;
525
538
require_ssl_verification:: Bool = NetworkOptions. verify_host (host, " SSL" ),
526
539
readtimeout:: Int = 0 ,
527
- kw... ):: Connection where {IOType}
540
+ kw... ):: Connection{IOType} where {IOType}
528
541
# initiate the upgrade to SSL
529
542
# if the upgrade fails, an error will be thrown and the original c will be closed
530
543
# in ConnectionRequest
@@ -538,9 +551,9 @@ function sslupgrade(::Type{IOType}, c::Connection,
538
551
# success, now we turn it into a new Connection
539
552
conn = Connection (host, " " , 0 , require_ssl_verification, tls)
540
553
# release the "old" one, but don't allow reuse since we're hijacking the socket
541
- release (POOL , connectionkey (c), c; return_for_reuse= false )
554
+ release (getpool (IOType) , connectionkey (c), c; return_for_reuse= false )
542
555
# and return the new one
543
- return acquire (POOL , connectionkey (conn), conn)
556
+ return acquire (getpool (IOType) , connectionkey (conn), conn)
544
557
end
545
558
546
559
function Base. show (io:: IO , c:: Connection )
0 commit comments