Cancellable Query with AbortSignal option #3212
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A suggestion for a new cancellation API (based on the original request in #2774)
This query-specific cancellation API only makes sense because the library waits for each query to complete before it submits the next. With query pipelining it would be a lot messier - or simpler, because the API could offer less guarantees.
While the tests are turned off if
AbortController
is not available, the library code itself should still be compatible with older node versions.Example
First, in a separate commit (913967f) is a small change that moves the SSL initialization logic to the
Connection
.This means the
Connection
no longer needs a separate an'sslconnect'
event.Then, the
Connection
got a newcon.cancelWithClone()
method that tries to connect to the exact same server, and send a cancel request.This required a bit of extra state in the
Connection
, to keep track of thehost
,port
,config
and thebackendKeyData
.I'm not thrilled with duplicating state from the
Client
(or with the name of the method for that matter).What I like about this approach is that all the logic around the cancellation can be implemented in the
Submittable
and doesn't depend on any special handling by theClient
.Finally the
Query
got asignal
option that can be used to cancel it either before, or after it has been submitted to the server.If the query has been canceled before submit, it will return the abort reason in submit.
If an actual cancel request has been sent, the query callback and the
'error'
and'end'
events are delayed until the cancel request has finished.This is done to avoid accidentally canceling a different query on the same connection.
If the cancel request isn't completed cleanly, the original connection is destroyed.
This might be triggered if you get an ECONNRESET after sending the cancel request? Honestly I'm not sure.
This should probably treat the errors after the cancel request has been sent differently from errors during connecting.