-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcounter.rs
44 lines (36 loc) · 1.22 KB
/
counter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use restate_sdk::prelude::*;
#[restate_sdk::object]
trait Counter {
#[shared]
async fn get() -> Result<u64, TerminalError>;
async fn add(val: u64) -> Result<u64, TerminalError>;
async fn increment() -> Result<u64, TerminalError>;
async fn reset() -> Result<(), TerminalError>;
}
struct CounterImpl;
const COUNT: &str = "count";
impl Counter for CounterImpl {
async fn get(&self, ctx: SharedObjectContext<'_>) -> Result<u64, TerminalError> {
Ok(ctx.get::<u64>(COUNT).await?.unwrap_or(0))
}
async fn add(&self, ctx: ObjectContext<'_>, val: u64) -> Result<u64, TerminalError> {
let current = ctx.get::<u64>(COUNT).await?.unwrap_or(0);
let new = current + val;
ctx.set(COUNT, new);
Ok(new)
}
async fn increment(&self, ctx: ObjectContext<'_>) -> Result<u64, TerminalError> {
self.add(ctx, 1).await
}
async fn reset(&self, ctx: ObjectContext<'_>) -> Result<(), TerminalError> {
ctx.clear(COUNT);
Ok(())
}
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
HttpServer::new(Endpoint::builder().bind(CounterImpl.serve()).build())
.listen_and_serve("0.0.0.0:9080".parse().unwrap())
.await;
}