tide icon indicating copy to clipboard operation
tide copied to clipboard

Proposal: Cancellation

Open pbzweihander opened this issue 4 years ago • 8 comments
trafficstars

Related issue: #528

Related pull request: #766

I made StopSource and StopToken types similar to the API described in https://github.com/yoshuawuyts/notes/blob/7ecb13a9999ae1fccd366ef0e246be5ca78867c0/rust/cancellation.md and used in tcp_listener and unix_listener modules.

But I'm not sure it actually 'gracefully' shuts down the server...

pbzweihander avatar Jun 25 '21 03:06 pbzweihander

Is there a reason we wouldn't use stop-token (or stopper)?

Having implemented graceful shutdown for my other framework, it seems likely we'd need to integrate this fairly deeply into http-types and async-h1 as well

jbr avatar Jun 25 '21 04:06 jbr

stopper seems great! I was worried that stop-token states in the documentation that it is 'Experimental'.

pbzweihander avatar Jun 25 '21 04:06 pbzweihander

Two things that we'll want to address in addition to stopping the tcp stream: we will need to keep track of how many outstanding requests there are so we know when to shut down the server, and we will need to cut off keep alive connections. Currently, async-h1 will hold onto the tcp stream and wait for a subsequent request, which would delay shutdown by the keepalive timeout

jbr avatar Jun 25 '21 06:06 jbr

As far as stop-token vs stopper, I don't have a great understanding of the current state of development of stop-token, which is part of why I wrote stopper. I also needed it to be runtime independent, but tide can depend on async-std.

We may very well discover bugs in stopper, but I'm committed to fixing them promptly

jbr avatar Jun 25 '21 06:06 jbr

I made a draft PR in async-h1: https://github.com/http-rs/async-h1/pull/188

And it seems working in this test. (It fails without the last commit)

pbzweihander avatar Jun 25 '21 09:06 pbzweihander

I replaced vector of JoinHandles with waitgroup

pbzweihander avatar Jun 25 '21 12:06 pbzweihander

Waitgroup looks a lot like how I did this in my other framework, with very minor differences (they're using an arc, I'm using an atomicusize which lets me keep track of how many outstanding requests there are): https://github.com/trillium-rs/trillium/blob/main/server-common/src/clone_counter.rs

I'd be totally fine copying mine over into tide, but waitgroup seems good and less code is certainly preferable

jbr avatar Jun 28 '21 18:06 jbr

@jbr waitgroup author here, just for technical discussion. :)

they're using an arc, I'm using an atomicusize which lets me keep track of how many outstanding requests there are

  1. Actually, Arc's strong_count can track the number of outstanding tasks, so another atomicusize is not needed.
  2. Using Arc's strong_count has another benefit: waitgroup will be waked up only once when all outstanding tasks are finished, instead of waked up every time some task is finished.

laizy avatar Jun 29 '21 09:06 laizy