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
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
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.