-
Notifications
You must be signed in to change notification settings - Fork 349
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
RPC: use AbortController and AbortSignal to cancel requests #730
Labels
Comments
paralin
changed the title
RPC: cancelation API
RPC: use AbortController and AbortSignal to cancel requests
Dec 12, 2022
This can also be used with abortable-iterator to abort AsyncIterable objects: import { abortableSource } from 'abortable-iterator'
const asyncCounter = async function * (start, delay) {
let i = start
while (true) {
yield new Promise(resolve => setTimeout(() => resolve(i++), delay))
}
}
// Create a counter that'll yield numbers from 0 upwards every second
const everySecond = asyncCounter(0, 1000)
// Make everySecond abortable!
const controller = new AbortController()
const abortableEverySecond = abortableSource(everySecond, controller.signal)
// Abort after 5 seconds
setTimeout(() => controller.abort(), 5000)
try {
// Start the iteration, which will throw after 5 seconds when it is aborted
for await (const n of abortableEverySecond) {
console.log(n)
}
} catch (err) {
if (err.code === 'ERR_ABORTED') {
// Expected - all ok :D
} else {
throw err
}
} |
Changes required:
It would then be up to the client implementation to honor the AbortSignal. |
paralin
added a commit
to paralin/ts-proto
that referenced
this issue
Dec 13, 2022
Adds a new option "useAbortSignal" which adds an optional AbortSignal parameter to RPC functions. AbortController and AbortSignal are built-ins in both Node.JS and all web browsers, which implement aborting long-lived processes. For example: const abortController = new AbortController() const responsePromise = rpcClient.DoSomething(request, abortController.signal) // abort the RPC call early abortController.abort() Fixes stephenh#730 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 13, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to paralin/ts-proto
that referenced
this issue
Dec 13, 2022
Adds a new option "useAbortSignal" which adds an optional AbortSignal parameter to RPC functions. AbortController and AbortSignal are built-ins in both Node.JS and all web browsers, which implement aborting long-lived processes. For example: const abortController = new AbortController() const responsePromise = rpcClient.DoSomething(request, abortController.signal) // abort the RPC call early abortController.abort() Fixes stephenh#730 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 13, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to paralin/ts-proto
that referenced
this issue
Dec 13, 2022
Adds a new option "useAbortSignal" which adds an optional AbortSignal parameter to RPC functions. AbortController and AbortSignal are built-ins in both Node.JS and all web browsers, which implement aborting long-lived processes. For example: const abortController = new AbortController() const responsePromise = rpcClient.DoSomething(request, abortController.signal) // abort the RPC call early abortController.abort() Fixes stephenh#730 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 13, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 13, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to paralin/ts-proto
that referenced
this issue
Dec 14, 2022
Adds a new option "useAbortSignal" which adds an optional AbortSignal parameter to RPC functions. AbortController and AbortSignal are built-ins in both Node.JS and all web browsers, which implement aborting long-lived processes. For example: const abortController = new AbortController() const responsePromise = rpcClient.DoSomething(request, abortController.signal) // abort the RPC call early abortController.abort() Fixes stephenh#730 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 14, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
🎉 This issue has been resolved in version 1.136.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 14, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 14, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
paralin
added a commit
to aperturerobotics/starpc
that referenced
this issue
Dec 14, 2022
Currently adds 1 patch for the following pending PR: - stephenh/ts-proto#730 - stephenh/ts-proto#731 Signed-off-by: Christian Stewart <christian@paral.in>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to #665 but not exactly the same.
TypeScript/JavaScript has AbortController now as a general way of canceling things:
https://developer.mozilla.org/en-US/docs/Web/API/AbortController
Here's an example of how it's used in yamux in javascript:
https://github.com/ChainSafe/js-libp2p-yamux/blob/c396f8c1b99f3c68104c894a1ac88a805bff68a3/src/muxer.ts#L49
https://github.com/ChainSafe/js-libp2p-yamux/blob/c396f8c1b99f3c68104c894a1ac88a805bff68a3/src/muxer.ts#L215
I propose adding an optional parameter to the RPC methods:
AbortSignal
-This can be used to stop a request before it completes.
The text was updated successfully, but these errors were encountered: