Hey all, I’m implementing the Swirld Hashgraph Conensus Algorithm in Elixir right now. I have the basics of the Algorithm down; however, I want to start to test it.
Basically, I want to create a TCP server capable of discovering other nodes on the network (using mDNS, Erlang has this) that will both Listen and Send in parallel.
For those of you unfamiliar with the algorithm, basically a node running on the network performs 2 operations in parallel:
- Receive data from another node
- Simultaneously send data to a randomly selected on the network
I know how to implement a basic TCP server in Elixir using :gen_tcp
and a GenServer
, but is it possible to perform these two operations asynchronously on the same node? Is it as simple as using spawn
or the Task API?
I don’t work with TCP and this kind of stuff very often, so I’m not exactly sure where to go with this.
Yup. Processes are always async with respect to each other. If you’ve got an erlang node and you want multiple things to run at the same time, just spawn a process to do each thing. If that process will be short lived, use a Task. If it will be long lived, you probably want to spawn a GenServer, which is a regular process + some “best practice” conventions around life cycle management.
4 Likes
Thanks!
In the case of Sending data, if I want to repeatedly send some data to a random node on the network after some set time delay, is it best to do this in the same, long-living process? OR should a process be spawned every time I want to send something?
That is:
- spawn process, send data, delay, send data, delay
OR
- spawn process, send data, kill process, delay, spawn process, send data, kill process, delay
It is probably enough to use Process.send_after in the long-living process.
4 Likes