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

How to shut down the server? #307

Open
garious opened this issue Jul 9, 2020 · 4 comments
Open

How to shut down the server? #307

garious opened this issue Jul 9, 2020 · 4 comments

Comments

@garious
Copy link
Contributor

garious commented Jul 9, 2020

Looking at HelloServer in example-service, how would one add a "Press to shut down" feature?

@tikue
Copy link
Collaborator

tikue commented Jul 9, 2020

Hey! Tarpc doesn't build in any shutdown functionality, because different people may want different behavior. But, adding it yourself via native futures/streams features wouldn't be too hard; it just kind of depends what you mean by shutdown. Are you interested in...

  • an insta-kill that severs all client and server connections and drops all in-flight requests?
    • would you want outbound requests to be cancelled cleanly or to just kill those connections?
  • a lameduck mode where...
    • existing connections are honored?
    • existing requests are allowed to complete, after which connections are closed?

One simple approach is to just make your server future abortable. If you're spawning client connections onto different tasks, you'd need to take some extra steps to shut down those connections.

@garious
Copy link
Contributor Author

garious commented Jul 9, 2020

Either approach would work for me, but with preference to the lameduck mode. I'll take a look at abortable, thanks.

Maybe this would be helpful here? https://github.com/jonhoo/stream-cancel

@tikue
Copy link
Collaborator

tikue commented Jul 9, 2020

stream-cancel looks very relevant -- also, jonhoo makes great stuff, so I'd trust it as a dependency. In general, I think tarpc's approach to servers is modular enough to allow fitting in custom approaches to shutdown. If you end up playing around with shutdown with tarpc, I'd be very interested in seeing some examples. Maybe we could even incorporate them into the readme or example-service/.

@garious
Copy link
Contributor Author

garious commented Jul 15, 2020

Hey @tikue, here's what I ended up going with:

 async fn start_abortable_tcp_server(addr: SocketAddr, exit: Arc<AtomicBool>) {
     let server = start_tcp_server(addr).fuse();
     let interval = time::interval(Duration::from_millis(100)).fuse();
     pin_mut!(server, interval);
     loop {
         select! {
             _ = server => {},
             _ = interval.select_next_some() => {
                 if exit.load(Ordering::Relaxed) {
                     break;
                 }
             }
         }
     }
 }

Not a graceful shutdown, but gets the job done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants