From 2e8c99dba1eaa9c4546901c1fb63294e36eb48e0 Mon Sep 17 00:00:00 2001 From: Steve Kelly Date: Thu, 30 Mar 2023 16:20:38 -0400 Subject: [PATCH] A few random stability/precompile improvements (#1030) * migrate global -> Ref, should help type inference * mark several definition checks with `@static` * fix scoping issue with timeout definition --- src/ConnectionPool.jl | 20 +++++++++----------- src/IOExtras.jl | 8 ++++---- src/Parsers.jl | 6 +++++- src/Servers.jl | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index a41c8fe44..1b3940cc8 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -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"]) diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 2ebd85f2d..50adfc624 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -51,7 +51,7 @@ _doc = """ Signal start/end of write or read operations. """ -if isdefined(Base, :startwrite) +@static if isdefined(Base, :startwrite) "$_doc" Base.startwrite(io) = nothing else @@ -59,7 +59,7 @@ else startwrite(io) = nothing end -if isdefined(Base, :closewrite) +@static if isdefined(Base, :closewrite) "$_doc" Base.closewrite(io) = nothing else @@ -67,7 +67,7 @@ else closewrite(io) = nothing end -if isdefined(Base, :startread) +@static if isdefined(Base, :startread) "$_doc" Base.startread(io) = nothing else @@ -75,7 +75,7 @@ else startread(io) = nothing end -if isdefined(Base, :closeread) +@static if isdefined(Base, :closeread) "$_doc" Base.closeread(io) = nothing else diff --git a/src/Parsers.jl b/src/Parsers.jl index 09f22594a..2bbf1095f 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -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) diff --git a/src/Servers.jl b/src/Servers.jl index 7ebf4033e..0d4ac6c72 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -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