Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectionPool: monitor idle connections #236

Merged
merged 3 commits into from
May 31, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth doing let c = t.c; @schedule monitor_idle_connection(t.c); end, so we don't capture all of t. But afaik, there's no finalizers on t, or other way to see this difference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, t is an immutable struct containing just c and an Int, so probably not much difference.
But it's a good reminder to consider what is being captured in closures. I have a feeling I should be more cognisant of that more often...

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