Crate memfault_ssf

Source
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 services
  • Message: a trait implemented by messages
  • Handler<M: Message>: a trait implemented by services that can handle messages of type M.

We provide some important structs to deploy the services:

  • ServiceThread: will start and run a service inside a dedicated thread. It returns a Mailbox.
  • Mailbox<S: Service>: a lightweight (cheap to clone()) 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§

BoundedMailbox
BoundedServiceThread
BoundedTaskMailbox
BoundedTaskServiceThread
DeliveryStats
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.
PingMessage
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.
ServiceJig
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.
ServiceJoinHandle
ServiceManager
ServiceMock
The ServiceMock allows you to mock a service processing messages of a specific type.
ServiceThread
Run a service inside a dedicated thread using a mpsc::channel to send/receive messages
SharedServiceThread
This runs a service into a thread but, unlike ServiceThread, it will use an Arc<Mutex<S>> so that the service object is also available as shared memory.
ShutdownHandle
ShutdownServiceMessage
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.
StatsAggregator

Enums§

MailboxError
The only reason for a message to fail to send is if the receiver channel is closed.
ServiceJoinHandleError

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).
TaskService
Implement this trait if your service needs to run a task in the background.