You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 5, 2019. It is now read-only.
<1> Inside the `server`, we create broker's channel and `task`.
435
-
<2> After ``server``'s end, we join broker to make sure all pending messages are delivered.
436
-
Note that this doesn't quite do the trick yet, as we are not joining readers and writers themselves.
437
-
<3> Inside `client`, we need to wrap `TcpStream` into an `Arc`, to be able to share it with the `client_writer`.
438
-
<4> On login, we notify the broker.
434
+
<2> Inside `client`, we need to wrap `TcpStream` into an `Arc`, to be able to share it with the `client_writer`.
435
+
<3> On login, we notify the broker.
439
436
Note that we `.unwrap` on send: broker should outlive all the clients and if that's not the case the broker probably panicked, so we can escalate the panic as well.
440
-
<5> Similarly, we forward parsed messages to the broker, assuming that it is alive.
437
+
<4> Similarly, we forward parsed messages to the broker, assuming that it is alive.
438
+
439
+
== Clean Shutdown
440
+
441
+
On of the problems of the current implementation is that it doesn't handle graceful shutdown.
442
+
If we break from the accept loop for some reason, all in-flight tasks are just dropped on the floor.
443
+
A more correct shutdown sequence would be:
444
+
445
+
. Stop accepting new clients
446
+
. Deliver all pending messages
447
+
. Exit the process
448
+
449
+
A clean shutdown in a channel based architecture is easy, although it can appear a magic trick at first.
450
+
In Rust, receiver side of a channel is closed as soon as all senders are dropped.
451
+
That is, as soon as producers exit and drop their senders, the rest of the system shutdowns naturally.
452
+
In `async_std` this translates to two rules:
453
+
454
+
. Make sure that channels form an acyclic graph.
455
+
. Take care to wait, in the correct order, until intermediate layers of the system process pending messages.
456
+
457
+
In `a-chat`, we already have an unidirectional flow of messages: `reader -> broker -> writer`.
458
+
However, we never wait for broker and writers, which might cause some messages to get dropped.
0 commit comments