diff --git a/REQUIRE b/REQUIRE index c87497786..07836cd36 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,3 +1,3 @@ -julia 0.6.2 -MbedTLS 0.5.7 +julia 0.6.3 +MbedTLS 0.5.9 IniFile diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 06b21ecf1..4bcfcd975 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -138,6 +138,12 @@ Base.isopen(t::Transaction) = isopen(t.c) && t.c.readcount <= t.sequence && t.c.writecount <= t.sequence +""" +Is `c` currently in use or expecting a response to request already sent? +""" +isbusy(c::Connection) = isopen(c) && (c.writebusy || c.readbusy || + c.writecount > c.readcount) + function Base.eof(t::Transaction) @require isreadable(t) || !isopen(t) if bytesavailable(t) > 0 @@ -264,10 +270,32 @@ function IOExtras.closeread(t::Transaction) notify(t.c.readdone) ;@debug 2 "✉️ Read done: $t" notify(poolcondition) + if !isbusy(t.c) + @schedule monitor_idle_connection(t.c) + end + @ensure !isreadable(t) return end +""" +Wait for `c` to receive data or reach EOF. +Close `c` on EOF or if response data arrives when no request was sent. +""" +function monitor_idle_connection(c::Connection) + if eof(c.io) ;@debug 2 "💀 Closed: $c" + close(c.io) + elseif !isbusy(c) ;@debug 1 "😈 Idle RX!!: $c" + close(c.io) + end +end + +function monitor_idle_connection(c::Connection{SSLContext}) + # MbedTLS.jl monitors idle connections for TLS close_notify messages. + # https://github.com/JuliaWeb/MbedTLS.jl/pull/145 +end + + function Base.close(t::Transaction) close(t.c) if iswritable(t)