BlackBird
Blackbird is framework like OTP for Erlang.
Blackbird is not a Actor Framewrok, it's Behavior for around Tokio task
This project currently provides the following functionality:
-
stage
helper and behaviours for implementing producer and consumer stages (stream processing) -
server
many features for tokio task to implement standalone service with request/response api also support
dynamic assign middleware for each service -
metric
a simple metric collector -
web
a web server send live state of tokio all task
Examples
Examples for Stage behaviors :
-
[ProducerConsumer] a stage between producer and consumer
A -> ( B ) -> C
-
[Consumer] a subscriber to ProducerConsumer or Producer , Send demand get events and consume it
-
[Producer] a producer await on channel/mailbox take request with demand and reply a Vec of Event
Some Code :
pub struct HttpServer {
...
}
#[async_trait]
impl Server for HttpServer {
pub async fn init(&mut self, uri: String,
self_mailbox: Sender
,
RespPack
>) -> GResult {
// here can do anything before listening on channel/mailbox
...
// always must return Continue/Terminate(Reason)
GResult::Continue
}
pub async fn handle_call(&mut self, req:
, from: oneshot::Sender
>) -> GResult { // req is request message // we handle request and reply reply(from, RespPack::Message(...)); GResult::Continue } pub async fn handle_cast(&mut self, req: Request) -> GResult { // this is like handle_call but sender dont await for response // for example we retrun Terminate GResult::Terminate(Reason::Err("....".to_owned())) } // when we return GResult::Terminate, // internaly call terminate before shutdown pub async fn terminate(&mut self, reason: Reason) { ..... } }
Running Httpserver
let (uri, sender_mailbox, _) = start_root_server("httpserver", // pass a name for generate uri for process
Box::new(HttpServer{...}),
5, // mailbox_size after reach to this, blocking caller with timeout 5ms
// we can define many middlewares to be called before req send to handle_cast/handle_call
middlewares: vec![Box::new(|&mut request| ....),
|&mut request| ....),
|&mut request| ....)],
// we can assign controller to get metric collector
controller: Some(sender_mailbox.clone())
));
assert!( uri == "blackbird://server-httpserver )
// RequestPack is for build a request message
let msg_pack = RequestPack::Message(...);
// cast to server and not await on response
sender_mailbox.cast(Request::new(msg_pack)).await;
License
Apache-2.0