Skip to content

Commit

Permalink
ConnectionPool: monitor idle connections (#236)
Browse files Browse the repository at this point in the history
* ConnectionPool: monitor idle connections

When a connection is returned to the (read) pool,
add a monitor to it for receiving unexpected data (or EOF),
and kill / close the Connection object if any activity occurs
before the next write (when it should have simply been waiting idle in the pool)

per JuliaLang/MbedTLS.jl#145 (comment)

closes #214
closes #199
closes #220
closes JuliaWeb/GitHub.jl#106

*  - Encapsulate read|writebusy/sequence/count logic in new isbusy function.
 - Move close() on eof() || !isbusy() to new monitor_idle_connection function.
 - Make monitor_idle_connection() a noop for ::Connection{SSLContext}

* require Julia 0.6.3 #236 (comment)
  • Loading branch information
vtjnash authored and quinnj committed May 31, 2018
1 parent 794b320 commit 24aa08e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6.2
MbedTLS 0.5.7
julia 0.6.3
MbedTLS 0.5.9
IniFile
28 changes: 28 additions & 0 deletions src/ConnectionPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 24aa08e

Please sign in to comment.