[−][src]Module splinter::mesh
Mesh is an asynchronous Connection handler that sends and receives across many Connections in a background thread.
use splinter::{mesh::{Envelope, Mesh}, transport::{Transport, inproc::InprocTransport}}; let mut transport = InprocTransport::default(); let mesh = Mesh::new(1, 1); let mut listener = transport.listen("inproc://my-connection").unwrap(); mesh.add(transport.connect(&listener.endpoint()).unwrap(), "client".to_string()).unwrap(); mesh.add(listener.accept().unwrap(), "server".to_string()).unwrap(); mesh.send(Envelope::new("client".to_string(), b"hello".to_vec())).unwrap(); mesh.recv().unwrap(); let client = mesh.remove("client").unwrap(); // If we were to drop client above, the reactor could detect that client disconnected from // the server and automatically cleanup and remove server, causing this to fail with // RemoveError::NotFound. let server = mesh.remove("server").unwrap();
Mesh can be cloned relatively cheaply and passed between threads. If receiving is performed from many clones, envelopes will be distributed among them.
The following goals influenced this implementation:
- The main reactor in the background thread should hold no locks. Adding a single RwLock read acquisition to an otherwise simple event loop was observed to decrease performance by a factor of 5x.
- Sends to connections should be queued and handled independently. This means if one Connection has a bunch of sends queued but its underlying socket is not writable, other Connections must still be able to send. This implementation uses a separate outgoing queue for each Connection that can be polled in the event loop to accomplish this, but there may be a more efficient implementation.
- Backpressure should be built in. This means all queues should be bounded so that a backpressure error can be returned when the queue is full.
Structs
Envelope | Contains a payload and the identifier for the connection on which the payload was received |
Mesh | A Connection reactor |
MeshLifeCycle | Mesh specific implementation of ConnectionMatrixLifeCycle |
MeshMatrixReceiver | Mesh specific implementation of MatrixReceiver |
MeshMatrixSender | Mesh specific implementation of ConnectionMatrixSender |
MeshMatrixShutdown | Mesh specific implementation of MatrixShutdown |
MeshShutdownSignaler |
Enums
AddError | |
RecvError | |
RecvTimeoutError | |
RemoveError | |
SendError |