Skip to content

Commit

Permalink
A few random stability/precompile improvements (#1030)
Browse files Browse the repository at this point in the history
* migrate global -> Ref, should help type inference

* mark several definition checks with `@static`

* fix scoping issue with timeout definition
  • Loading branch information
sjkelly authored Mar 30, 2023
1 parent 8dc7039 commit 2e8c99d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
20 changes: 9 additions & 11 deletions src/ConnectionPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,26 +452,24 @@ end

const nosslconfig = SSLConfig()
const nosslcontext = Ref{OpenSSL.SSLContext}()
default_sslconfig = nothing
noverify_sslconfig = nothing
const default_sslconfig = Ref{Union{Nothing, SSLConfig}}(nothing)
const noverify_sslconfig = Ref{Union{Nothing, SSLConfig}}(nothing)

function global_sslconfig(require_ssl_verification::Bool)::SSLConfig
global default_sslconfig
global noverify_sslconfig
if default_sslconfig === nothing
default_sslconfig = SSLConfig(true)
noverify_sslconfig = SSLConfig(false)
if default_sslconfig[] === nothing
default_sslconfig[] = SSLConfig(true)
noverify_sslconfig[] = SSLConfig(false)
end
if haskey(ENV, "HTTP_CA_BUNDLE")
MbedTLS.ca_chain!(default_sslconfig, MbedTLS.crt_parse(read(ENV["HTTP_CA_BUNDLE"], String)))
MbedTLS.ca_chain!(default_sslconfig[], MbedTLS.crt_parse(read(ENV["HTTP_CA_BUNDLE"], String)))
elseif haskey(ENV, "CURL_CA_BUNDLE")
MbedTLS.ca_chain!(default_sslconfig, MbedTLS.crt_parse(read(ENV["CURL_CA_BUNDLE"], String)))
MbedTLS.ca_chain!(default_sslconfig[], MbedTLS.crt_parse(read(ENV["CURL_CA_BUNDLE"], String)))
end
return require_ssl_verification ? default_sslconfig : noverify_sslconfig
return require_ssl_verification ? default_sslconfig[] : noverify_sslconfig[]
end

function global_sslcontext()::OpenSSL.SSLContext
if isdefined(OpenSSL, :ca_chain!)
@static if isdefined(OpenSSL, :ca_chain!)
if haskey(ENV, "HTTP_CA_BUNDLE")
sslcontext = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
OpenSSL.ca_chain!(sslcontext, ENV["HTTP_CA_BUNDLE"])
Expand Down
8 changes: 4 additions & 4 deletions src/IOExtras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,31 @@ _doc = """
Signal start/end of write or read operations.
"""
if isdefined(Base, :startwrite)
@static if isdefined(Base, :startwrite)
"$_doc"
Base.startwrite(io) = nothing
else
"$_doc"
startwrite(io) = nothing
end

if isdefined(Base, :closewrite)
@static if isdefined(Base, :closewrite)
"$_doc"
Base.closewrite(io) = nothing
else
"$_doc"
closewrite(io) = nothing
end

if isdefined(Base, :startread)
@static if isdefined(Base, :startread)
"$_doc"
Base.startread(io) = nothing
else
"$_doc"
startread(io) = nothing
end

if isdefined(Base, :closeread)
@static if isdefined(Base, :closeread)
"$_doc"
Base.closeread(io) = nothing
else
Expand Down
6 changes: 5 additions & 1 deletion src/Parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ const unhex = Int8[
function __init__()
# FIXME Consider turing off `PCRE.UTF` in `Regex.compile_options`
# https://github.com/JuliaLang/julia/pull/26731#issuecomment-380676770
nt = isdefined(Base.Threads, :maxthreadid) ? Threads.maxthreadid() : Threads.nthreads()
nt = @static if isdefined(Base.Threads, :maxthreadid)
Threads.maxthreadid()
else
Threads.nthreads()
end
resize!(empty!(status_line_regex), nt)
resize!(empty!(request_line_regex), nt)
resize!(empty!(header_field_regex), nt)
Expand Down
2 changes: 1 addition & 1 deletion src/Servers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ After `reuse_limit + 1` transactions, signal `final_transaction` to the
transaction handler, which will close the connection.
"""
function handle_connection(f, c::Connection, listener, readtimeout, access_log)
wait_for_timeout = Ref{Bool}(true)
if readtimeout > 0
wait_for_timeout = Ref{Bool}(true)
@async check_readtimeout(c, readtimeout, wait_for_timeout)
end
try
Expand Down

0 comments on commit 2e8c99d

Please sign in to comment.