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

RPC: use AbortController and AbortSignal to cancel requests #730

Closed
paralin opened this issue Dec 12, 2022 · 3 comments · Fixed by #731
Closed

RPC: use AbortController and AbortSignal to cancel requests #730

paralin opened this issue Dec 12, 2022 · 3 comments · Fixed by #731
Labels

Comments

@paralin
Copy link
Collaborator

paralin commented Dec 12, 2022

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 -

const abortController = new AbortController()
const responsePromise = rpcClient.DoSomething(request, abortController.signal)
abortController.abort()
// responsePromise is rejected w/ Aborted

This can be used to stop a request before it completes.

@paralin paralin changed the title RPC: cancelation API RPC: use AbortController and AbortSignal to cancel requests Dec 12, 2022
@paralin
Copy link
Collaborator Author

paralin commented 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
  }
}

@paralin
Copy link
Collaborator Author

paralin commented Dec 12, 2022

Changes required:

  • Add optional AbortSignal parameter to RPC calls
  • Add optional AbortSignal parameter to serverStreamingRequest, etc in the Rpc interface.

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>
stephenh pushed a commit that referenced this issue Dec 14, 2022
# [1.136.0](v1.135.3...v1.136.0) (2022-12-14)

### Features

* RPC: add useAbortSignal option ([#731](#731)) ([69313a7](69313a7)), closes [#730](#730)
@stephenh
Copy link
Owner

🎉 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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants