Crates.io | serviceless |
lib.rs | serviceless |
version | 0.1.2 |
source | src |
created_at | 2023-09-11 17:51:00.976831 |
updated_at | 2023-12-15 14:56:55.133874 |
description | An simple actor model in rust, like actix |
homepage | |
repository | |
max_upload_size | |
id | 969863 |
size | 16,976 |
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.
ServiceStopped
from Error.ServicePaused
from Error.ServiceStopped
from Error.