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

Add idle connection timeouts for HTTP client's connections pool. #324

Merged
merged 6 commits into from
Mar 21, 2023

Conversation

cheatfate
Copy link
Collaborator

Add timestamps and duration for both HTTP client requests/responses.

Add timestamps and duration for both HTTP client requests/responses.
Add test.
@cheatfate cheatfate force-pushed the http-client-idle-connections branch from 9dfb66e to be77bfb Compare February 25, 2023 07:23
maxConnections = -1): HttpSessionRef {.
maxConnections = -1,
idleTimeout = HttpConnectionIdleTimeout,
idlePeriod = HttpConnectionCheckPeriod): HttpSessionRef {.
Copy link
Member

Choose a reason for hiding this comment

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

this is imo an implementation detail that we shouldn't expose in public API - it doesn't matter greatly since we can always make it a no-op, but it feels like the kind of thing that doesn't greatly matter for the user of the API

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think some of the users could decrease idlePeriod to 1.seconds just to have more precise control.

chronos/apps/http/httpclient.nim Outdated Show resolved Hide resolved
chronos/apps/http/httpclient.nim Outdated Show resolved Hide resolved
chronos/apps/http/httpclient.nim Outdated Show resolved Hide resolved
@@ -1079,13 +1211,16 @@ proc open*(request: HttpClientRequestRef): Future[HttpBodyWriter] {.
try:
let headers = request.prepareRequest()
request.connection.state = HttpClientConnectionState.RequestHeadersSending
request.setTimestamp()
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be part of prepareRequest?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

prepareRequest is not related to actual data sending process, so why we need to start from there?


template isIdle(conn: HttpClientConnectionRef, timestamp: Moment,
timeout: Duration): bool =
(timestamp - conn.timestamp) >= timeout
Copy link
Member

Choose a reason for hiding this comment

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

could it happen that some really long send (several mb?) causes the connection to be seen as idle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course its possible, but we can't control it, if people willing to send gigabytes of data from memory to a socket, how we can check if send was actually happened or not?

except AsyncTimeoutError:
raiseHttpConnectionError("Connection timed out")
conn.state = HttpClientConnectionState.Acquired
session.connections.mgetOrPut(ha.id, default).add(conn)
Copy link
Member

@arnetheduck arnetheduck Mar 16, 2023

Choose a reason for hiding this comment

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

Suggested change
session.connections.mgetOrPut(ha.id, default).add(conn)
session.connections.mgetOrPut(ha.id).add(conn)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

proc mgetOrPut*[A, B](t: var Table[A, B], key: A, val: B): var B =

https://github.com/nim-lang/Nim/blob/v1.6.12/lib/pure/collections/tables.nim#L441

@cheatfate cheatfate merged commit 0688d2e into master Mar 21, 2023
@cheatfate cheatfate deleted the http-client-idle-connections branch March 21, 2023 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants