Skip to content

Commit 9ac2fa8

Browse files
committed
pr feedback Signed-off-by: Daniel Gerlag <[email protected]>
Signed-off-by: Daniel Gerlag <[email protected]>
1 parent 92c1f25 commit 9ac2fa8

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

Diff for: examples/actors/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This example demonstrates the Dapr actor framework. To author an actor,
55
1. Create a struct with your custom actor methods that map to [Axum handlers](https://docs.rs/axum/latest/axum/handler/index.html), use [Axum extractors](https://docs.rs/axum/latest/axum/extract/index.html) to access the incoming request and return an [`impl IntoResponse`](https://docs.rs/axum/latest/axum/response/trait.IntoResponse.html).
66
Use the `DaprJson` extractor to deserialize the request from Json coming from a Dapr sidecar.
77
```rust
8+
struct MyActor {
9+
id: String,
10+
client: ActorContextClient
11+
}
12+
813
#[derive(Serialize, Deserialize)]
914
pub struct MyRequest {
1015
pub name: String,

Diff for: src/server/actor/context_client.rs

+36
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ impl Into<TransactionalActorStateOperation> for ActorStateOperation {
4545
}
4646
}
4747

48+
/// A client for interacting with the Dapr runtime within the scope of an actor.
49+
///
50+
/// Hosts methods for interacting with the Dapr sidecar specific to the actor instance.
4851
#[derive(Clone)]
4952
pub struct ActorContextClient{
5053
client: TonicClient,
@@ -62,6 +65,10 @@ impl ActorContextClient {
6265
}
6366
}
6467

68+
/// Retrieves a keyed state value within the scope of this instance of the actor.
69+
///
70+
/// # Arguments
71+
/// * `key` - The key of the state to retrieve.
6572
pub async fn get_actor_state<K>(&mut self, key: K) -> Result<GetActorStateResponse, DaprError>
6673
where K: Into<String>
6774
{
@@ -72,6 +79,10 @@ impl ActorContextClient {
7279
}).await?.into_inner())
7380
}
7481

82+
/// Saves a state value within the scope of this instance of the actor.
83+
///
84+
/// # Arguments
85+
/// * `operations` - A list of [ActorStateOperation] to perform on the state.
7586
pub async fn execute_actor_state_transaction(
7687
&mut self,
7788
operations: Vec<ActorStateOperation>,
@@ -85,6 +96,14 @@ impl ActorContextClient {
8596
}).await?.into_inner())
8697
}
8798

99+
/// Registers a reminder with the Dapr runtime.
100+
///
101+
/// # Arguments
102+
/// * `name` - The name of the reminder.
103+
/// * `due_time` - The time at which the reminder should first be invoked.
104+
/// * `period` - The time interval between invocations of the reminder.
105+
/// * `data` - The data to pass to the reminder when it is invoked.
106+
/// * `ttl` - The time to live for the reminder.
88107
pub async fn register_actor_reminder<I>(
89108
&mut self,
90109
name: I,
@@ -118,6 +137,10 @@ impl ActorContextClient {
118137
}).await?.into_inner())
119138
}
120139

140+
/// Unregisters a reminder with the Dapr runtime.
141+
///
142+
/// # Arguments
143+
/// * `name` - The name of the reminder to unregister.
121144
pub async fn unregister_actor_reminder<I>(
122145
&mut self,
123146
name: I
@@ -133,6 +156,15 @@ impl ActorContextClient {
133156
}).await?.into_inner())
134157
}
135158

159+
/// Registers a timer with the Dapr runtime.
160+
///
161+
/// # Arguments
162+
/// * `name` - The name of the timer.
163+
/// * `due_time` - The time at which the timer should first be invoked.
164+
/// * `period` - The time interval between invocations of the timer.
165+
/// * `data` - The data to pass to the timer when it is invoked.
166+
/// * `callback` - The callback name to include in the invocation.
167+
/// * `ttl` - The time to live for the timer.
136168
pub async fn register_actor_timer<I>(
137169
&mut self,
138170
name: I,
@@ -167,6 +199,10 @@ impl ActorContextClient {
167199
}).await?.into_inner())
168200
}
169201

202+
/// Unregisters a timer with the Dapr runtime.
203+
///
204+
/// # Arguments
205+
/// * `name` - The name of the timer to unregister.
170206
pub async fn unregister_actor_timer<I>(
171207
&mut self,
172208
name: I

Diff for: src/server/actor/runtime/mod.rs

+56
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ pub struct ActorState {
1818
pub runtime: Arc<ActorRuntime>,
1919
}
2020

21+
/// Describes the registration of an actor type, including the methods that can be invoked on it and the factory to create instances of it.
22+
/// # Example:
23+
/// ```rust
24+
/// let mut dapr_server = dapr::server::DaprHttpServer::new().await;
25+
///
26+
/// dapr_server.register_actor(ActorTypeRegistration::new::<MyActor>("MyActor", Box::new(|_actor_type, actor_id, context| {
27+
/// Arc::new(MyActor {
28+
/// id: actor_id.to_string(),
29+
/// client: context,
30+
/// })}))
31+
/// .register_method("do_stuff", MyActor::do_stuff)
32+
/// .register_method("do_other_stuff", MyActor::do_other_stuff))
33+
/// .await;
34+
/// ```
2135
pub struct ActorTypeRegistration {
2236
name: String,
2337
factory: ActorFactory,
@@ -33,6 +47,45 @@ impl ActorTypeRegistration {
3347
}
3448
}
3549

50+
/// Registers a method on the actor type to be exposed to actor clients.
51+
///
52+
/// # Arguments:
53+
/// * `method_name` - The name of the method to be registered. This name will be used by actor clients to invoke the method.
54+
/// * `handler` - The handler function to be invoked when the method is called.
55+
/// Can be any valid [Axum handler](https://docs.rs/axum/latest/axum/handler/index.html),
56+
/// use [Axum extractors](https://docs.rs/axum/latest/axum/extract/index.html) to access the incoming request and return an [`impl IntoResponse`](https://docs.rs/axum/latest/axum/response/trait.IntoResponse.html).
57+
/// Use the `DaprJson` extractor to deserialize the request from Json coming from a Dapr sidecar.
58+
/// # Example:
59+
/// ```rust
60+
/// #[derive(Serialize, Deserialize)]
61+
/// pub struct MyRequest {
62+
/// pub name: String,
63+
///}
64+
///
65+
///#[derive(Serialize, Deserialize)]
66+
///pub struct MyResponse {
67+
/// pub available: bool,
68+
///}
69+
///
70+
///impl MyActor {
71+
/// fn do_stuff(&self, DaprJson(data): DaprJson<MyRequest>) -> Json<MyResponse> {
72+
/// println!("doing stuff with {}", data.name);
73+
/// Json(MyResponse {
74+
/// available: true
75+
/// })
76+
/// }
77+
///}
78+
///
79+
/// let mut dapr_server = dapr::server::DaprHttpServer::new().await;
80+
///
81+
/// dapr_server.register_actor(ActorTypeRegistration::new::<MyActor>("MyActor", Box::new(|_actor_type, actor_id, context| {
82+
/// Arc::new(MyActor {
83+
/// id: actor_id.to_string(),
84+
/// client: context,
85+
/// })}))
86+
/// .register_method("do_stuff", MyActor::do_stuff)
87+
/// .await;
88+
/// ```
3689
pub fn register_method<T>(mut self, method_name: &str, handler: impl Handler<T, ActorState> + Send + Sync) -> Self
3790
where T: 'static
3891
{
@@ -78,6 +131,9 @@ impl ActorRuntime {
78131
}
79132
}
80133

134+
/// Registers an actor type to be exposed to actor clients.
135+
/// # Arguments:
136+
/// * `registration` - The [ActorTypeRegistration] that describes the actor implementation.
81137
pub async fn register_actor(&self, registration: ActorTypeRegistration) {
82138
let name = registration.name.clone();
83139
let mut g = self.registered_actors_types.write().await;

Diff for: src/server/http.rs

+27
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@ use super::actor::runtime::{ActorRuntime, ActorTypeRegistration};
55
use super::super::client::TonicClient;
66

77

8+
/// The Dapr HTTP server.
9+
///
10+
/// Supports Http callbacks from the Dapr sidecar.
11+
///
12+
/// # Example:
13+
/// ```rust
14+
/// let mut dapr_server = dapr::server::DaprHttpServer::new().await;
15+
///
16+
/// dapr_server.register_actor(ActorTypeRegistration::new::<MyActor>("MyActor", Box::new(|_actor_type, actor_id, context| {
17+
/// Arc::new(MyActor {
18+
/// id: actor_id.to_string(),
19+
/// client: context,
20+
/// })}))
21+
/// .register_method("do_stuff", MyActor::do_stuff)
22+
/// .await;
23+
///
24+
/// dapr_server.start(None).await?;
25+
/// ```
826
pub struct DaprHttpServer {
927
actor_runtime: Arc<ActorRuntime>,
1028
}
1129

1230
impl DaprHttpServer {
31+
/// Creates a new instance of the Dapr HTTP server with default options.
1332
pub async fn new() -> Self {
1433
let dapr_port: u16 = std::env::var("DAPR_GRPC_PORT").unwrap_or("3501".into()).parse().unwrap();
1534
Self::with_dapr_port(dapr_port).await
@@ -29,10 +48,18 @@ impl DaprHttpServer {
2948
}
3049
}
3150

51+
/// Registers an actor type with the Dapr runtime.
52+
///
53+
/// # Arguments:
54+
/// * `registration` - The [ActorTypeRegistration] struct, carries the methods that can be invoked on it and the factory to create instances of it.
3255
pub async fn register_actor(&self, registration: ActorTypeRegistration) {
3356
self.actor_runtime.register_actor(registration).await;
3457
}
3558

59+
/// Starts the Dapr HTTP server.
60+
///
61+
/// # Arguments:
62+
/// * `port` - The port to listen on. If not specified, the APP_PORT environment variable will be used. If that is not specified, 8080 will be used.
3663
pub async fn start(&mut self, port: Option<u16>) -> Result<(), Box<dyn std::error::Error>> {
3764
let app = self.build_router().await;
3865

0 commit comments

Comments
 (0)