Expand description
Simple Service Framework
This library provides a simple abstraction to implement a service oriented architecture in a non-async Rust program.
Each service runs in its own thread with a processing loop waiting on a channel.
This library provides a few essential traits:
Service
: a trait implemented by servicesMessage
: a trait implemented by messagesHandler<M: Message>
: a trait implemented by services that can handle messages of typeM
.
We provide some important structs to deploy the services:
ServiceThread
: will start and run a service inside a dedicated thread. It returns aMailbox
.Mailbox<S: Service>
: a lightweight (cheap toclone()
) handle to send messages to a thread.Scheduler
: a utility thread which keeps a schedule of messages that need to be sent at fixed intervals.
As well as important testing utilities that are a big part of the value provided by this framework:
ServiceJig
: a way to run a service inside a test without using threads. The test can precisely decide when messages should be delivered and inspect the state of the service at any time.ServiceMock
: a service mock. Use this when you just need a place where to send messages. Your test can then verify that the right messages were sent to the mock.
Structs§
- Bounded
Mailbox - Bounded
Service Thread - Bounded
Task Mailbox - Bounded
Task Service Thread - Delivery
Stats - Envelope
- Wrap a message that can be handled by
S
. - Mailbox
- MsgMailbox
- A
MsgMailbox
only depends on the type of the messages it can contain. - Ping
Message - The
PingMessage
is supported by all services. It allows the caller to verify that the service is still running and that it has processed its queue. - Scheduler
- The
Scheduler
is a tool to schedule sending specific messages at a given interval. It runs as its own thread. - Service
Jig - The ServiceJig allows you to create a mailbox for a service and control when messages will be processed. It does not use thread and is specifically well suited for unit tests.
- Service
Join Handle - Service
Manager - Service
Mock - The ServiceMock allows you to mock a service processing messages of a specific type.
- Service
Thread - Run a service inside a dedicated thread using a mpsc::channel to send/receive messages
- Shared
Service Thread - This runs a service into a thread but, unlike
ServiceThread
, it will use anArc<Mutex<S>>
so that the service object is also available as shared memory. - Shutdown
Handle - Shutdown
Service Message - The
ShutdownServiceMessage
is supported by all services. It terminates the thread. No other message in the queue will be processed. The service will receive this message and can do something before shutting down. - Stats
Aggregator
Enums§
- Mailbox
Error - The only reason for a message to fail to send is if the receiver channel is closed.
- Service
Join Handle Error
Traits§
- Handler
- Implement this trait to indicate that your service can process a specific message.
- Message
- Any type that will be sent between services needs to implement this trait.
- Service
- All services should implement this trait. It guarantees that we will be able
to run the service inside a thread (it is
Send
). - Task
Service - Implement this trait if your service needs to run a task in the background.