Skip to content

Commit

Permalink
Update ConcurrentUtilities
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrobinson251 committed Nov 1, 2023
1 parent a05f346 commit 2810ecc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ version = "0.7.1"

[[deps.ConcurrentUtilities]]
deps = ["Serialization", "Sockets"]
git-tree-sha1 = "584a4f8d88293ad5eaa5de6dff3e5066b9d47b14"
repo-rev = "npr-pool-size"
git-tree-sha1 = "8cfa272e8bdedfa88b6aefbbca7c19f1befac519"
repo-rev = "main"
repo-url = "https://github.com/JuliaServices/ConcurrentUtilities.jl.git"
uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
version = "2.3.0"
Expand Down
26 changes: 13 additions & 13 deletions src/Connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ end
const CPool{T} = ConcurrentUtilities.Pool{ConnectionKeyType, Connection{T}}

"""
HTTP.Pool(max::Int=HTTP.default_connection_limit[])
HTTP.Pool(limit::Int=HTTP.default_connection_limit[])
Connection pool for managing the reuse of HTTP connections.
`max` controls the maximum number of concurrent connections allowed
`limit` controls the maximum number of concurrent connections allowed
and defaults to the `HTTP.default_connection_limit` value.
A pool can be passed to any of the `HTTP.request` methods via the `pool` keyword argument.
Expand All @@ -364,17 +364,17 @@ struct Pool
mbedtls::CPool{MbedTLS.SSLContext}
openssl::CPool{OpenSSL.SSLStream}
other::IdDict{Type, CPool}
max::Int
limit::Int
end

function Pool(max::Union{Int, Nothing}=nothing)
max = something(max, default_connection_limit[])
function Pool(limit::Union{Int, Nothing}=nothing)
limit = something(limit, default_connection_limit[])
return Pool(ReentrantLock(),
CPool{Sockets.TCPSocket}(max),
CPool{MbedTLS.SSLContext}(max),
CPool{OpenSSL.SSLStream}(max),
CPool{Sockets.TCPSocket}(limit),
CPool{MbedTLS.SSLContext}(limit),
CPool{OpenSSL.SSLStream}(limit),
IdDict{Type, CPool}(),
max,
limit,
)
end

Expand Down Expand Up @@ -428,9 +428,9 @@ Metrics for the given connection pool:
"""
function Metrics(cpool::CPool)
return Metrics(
limit=ConcurrentUtilities.Pools.max(cpool),
in_use=ConcurrentUtilities.Pools.permits(cpool),
in_pool=ConcurrentUtilities.Pools.depth(cpool),
limit=ConcurrentUtilities.Pools.limit(cpool),
in_use=ConcurrentUtilities.Pools.in_use(cpool),
in_pool=ConcurrentUtilities.Pools.in_pool(cpool),
)
end

Expand All @@ -457,7 +457,7 @@ function getpool(pool::Pool, ::Type{T})::CPool{T} where {T}
elseif T === OpenSSL.SSLStream
return pool.openssl
else
return Base.@lock pool.lock get!(() -> CPool{T}(pool.max), pool.other, T)
return Base.@lock pool.lock get!(() -> CPool{T}(pool.limit), pool.other, T)
end
end

Expand Down
16 changes: 9 additions & 7 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ using InteractiveUtils: @which
using ConcurrentUtilities

# test we can adjust default_connection_limit
for x in (10, 12)
HTTP.set_default_connection_limit!(x)
@test HTTP.Connections.TCP_POOL[].max == x
@test HTTP.Connections.MBEDTLS_POOL[].max == x
@test HTTP.Connections.OPENSSL_POOL[].max == x
@testset "set_default_connection_limit!" begin
for x in (10, 12)
HTTP.set_default_connection_limit!(x)
@test HTTP.Connections.TCP_POOL[].limit == x
@test HTTP.Connections.MBEDTLS_POOL[].limit == x
@test HTTP.Connections.OPENSSL_POOL[].limit == x
end
end

@testset "@client macro" begin
Expand Down Expand Up @@ -325,11 +327,11 @@ end
end

@testset "connect_timeout does not include the time needed to acquire a connection from the pool" begin
connection_limit = HTTP.Connections.TCP_POOL[].max
connection_limit = HTTP.Connections.TCP_POOL[].limit
try
dummy_conn = HTTP.Connection(Sockets.TCPSocket())
HTTP.set_default_connection_limit!(1)
@assert HTTP.Connections.TCP_POOL[].max == 1
@assert HTTP.Connections.TCP_POOL[].limit == 1
# drain the pool
acquire(()->dummy_conn, HTTP.Connections.TCP_POOL[], HTTP.Connections.connectionkey(dummy_conn))
# Put it back in 10 seconds
Expand Down

0 comments on commit 2810ecc

Please sign in to comment.