Replies: 1 comment
-
Ultimately you are going to need some direct communication to the task you are cancelling. For this, there are many methods, e.g. an If you want cancellation messages, I would probably go for two separate channels. One for new jobs, and one for cancellations. Not only does this allow cancellations to catch up, it also prevents cancellations from being caught behind a large number of requests waiting in line. It does pose another problem though: cancellation before the task arrives. There are some tombstone methods where you remember the cancelled tasks, but this seems complicated. To me, the simplest method appears having an |
Beta Was this translation helpful? Give feedback.
-
I'm trying to write a set of chained actors to handle a HTTP request using Tokio and Reqwest. Each actor would modify or act on the request in some way, before forwarding it to the next actor in the chain. In this fashion, it would reach the terminal actor, which wraps a reqwest client and, will actually do the network fetch.
My basic actor design is as follows - This one just prints out the Http Request received.
Each HttpRequest has a id. I would like to be able to cancel a request (by id) if needed, after it has been initiated.
I could do a cancel by setting up a cancel channel and using a tokio_select! as
A couple of issues that come to mind -
With the current design, the
cancel
message is received on the same channel as theload
request. So, it would necessarily process only after theload
. Ideally, I would like the chain to be broken as soon as I get a cancel message. But, at the moment I think it would propogate until it gets to a loader that actually awaits something in it's load action.I could setup a second cancellation channel, spawn another task and wrap this up in
tokio_select!
. But, I'm not sure how I could set up the patterns to match against the Id. If I were to have two request A and B running, and, I send a message on the cancellation channel to cancel A, I imagine the message would be received by the tokio_select! wrapping B as well - even though the Id doesn't match, it would process the message, return, and effectively cancel the load of B too.To avoid this I could setup a separate cancellation channel for each request, store this somewhere, and discard it once the load either completes or is cancelled.
Would there be a simpler way to do this? Thanks
Beta Was this translation helpful? Give feedback.
All reactions