r/rust•
apentlander

Tokio streams working with state

So I've been trying to write a very simple gossip protocol daemon using Tokio and UDP sockets. I'm at the point where I have a very simple program that is able to send heartbeats and will print any messages that it receives.

The problem is that I'm at the point where I want to mutate the state (e.g. increment the heart beat for the Gossiper), and I'm having a difficult time trying to figure out how to do this. I was thinking of using a RWLock, but that would block the event loop. The other solution I was thinking of was to send all of the state change requests over a futures channel and just do all of the state changes in a separate thread.

Is there something here I'm missing? It seems pretty painful to deal with state mutation and streams. All of the examples are stateless so they aren't much help in this situation, a slightly less trivial example in tokio-core or futures that keeps track of state would be super useful. Here's my code, I know it's not the cleanest but it works: https://gist.github.com/pentlander/a2b90421d3f8b9502bca169a844696da

Posted Apr 7, 2017, 4:28 AM UTC

56
rootnod3rust · wtftw•

What you can do is: let's say you have your stream of actual messages and a timer stream for your heartbeats. Select on them, by packing them in an enum, and then fold that stream. By initializing the fold with a target sink and your initial state, you can use that to keep track of the state without using a separate thread.

The result will be a future that you can just throw into your chain of futures/streams. Very rough code (and neglecting a few details because on mobile), but here you go:

https://gist.github.com/Kintaro/d4210b7a049d331429b9851f48670cef

7
apentlanderOP•

Thanks this makes a lot of sense, I'm trying to implement this now. It looks like this would work just fine and if I wanted to add another type of event I would just have to add another enum value to Package and put it in the match statement.

1
Ralith•

You don't seem to be using threads, so you can store state in an Rc<RefCell<_>> and access it from as many different futures as you like. If you only need access the state from a single future [combinator], you don't even need to do that; just store it in the future directly, e.g. as a mutable element of a closure environment.

3
apentlanderOP•

Oh duh. I completely forgot that this is running in the context of a single thread. I guess in my head I was conflating "concurrent" with "parallel" and thought that the futures could somehow mutate the state at the same time. I'm gonna give u/rootnod3's method a try as well to see which I like better. Thanks!

1
kaesos•

If you only need access the state from a single future [combinator], you don't even need to do that; just store it in the future directly, e.g. as a mutable element of a closure environment.

Do you have some example code or small snippet showing this? In particular, I'm not sure I got the part about mixing streams and combinator and storing in the future (stream?) itself.

1
Ralith•

For example:

let mut counter = 0;
my_stream.map(move |x| { counter += 1; (x, counter) })

Thanks to the move keyword, counter actually lives in the closure environment, rather than merely being referenced.

1