Serviceless is an simple actor model in rust, like actix but simple and async.
Currently, this crate only use tokio as backend.
This crate is no unsafe code.
This crate provide API same like actix
. But all api is async include handler.
Service
is a Actor
in Actor Model.
We can impl an Service
on an struct to delcare an Service
.
pub struct Service0 {}
impl Service for Service0 {}
The Service
provide hook named started
and stopped
, them all are async.
Please use async_trait
macros.
pub struct Service1 {}
#[async_trait]
impl Service for Service1 {
async fn started(&mut self, _ctx: &mut Context<Self>) {
println!("Started")
}
}
Start a service is very simple, only create it and call start
method.
let svc = Service1 {};
svc.start();
Note: this function must call in async function or after async runtime initialized. If not, it will panic.
When a service started, we can call stop and pause method on context
.
You can call these function in Service Hook or in Handler.
A service can sending an message to other service, or called by other service.
To call other Service, we must declare an Message
first.
Any type can be a message, only need impl Message trait on struct and define an result.
pub struct Messagep {}
pub struct MessageResult {}
impl Message for Message0 {
type Result = MessageResult;
}
Impl Handler on service, we can make a service accept call from other service.
#[async_trait]
impl Handler<U8> for Service0 {
async fn handler(&mut self, message: U8, _ctx: &mut Context<Self>) -> U8 {
U8(message.0 + 2)
}
}
Handler also an async function, please use async_trait
macros.
When we start an service, we can get an address. We also can get it from Context.
The address can make call or send.
- Call means caller want to known the result.
- This is an async function
- When an service stop, caller will get
ServiceStopped
from Error. - When an service pause, caller will get
ServicePaused
from Error.
- Send means caller don't care the result, so
- This is an plain function
- When an service stop, caller will get
ServiceStopped
from Error. - Caller can't known service paused