Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dapr-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ pub fn actor(_attr: TokenStream, item: TokenStream) -> TokenStream {
return Err(dapr::server::actor::ActorRejection::ActorError(e.to_string()));
}
};
if let Some(lease) = parts
.extensions
.get::<std::sync::Arc<dapr::server::actor::runtime::ActorTurnLease>>()
{
lease.allow_reentrancy();
}
let actor = actor.as_ref();
let well_known_actor =
unsafe { &*(actor as *const dyn dapr::server::actor::Actor as *const #actor_struct_name) };
Expand Down
53 changes: 53 additions & 0 deletions dapr/src/server/actor/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ use std::{collections::HashMap, sync::Arc};
use tokio::sync::RwLock;

use crate::client::TonicClient;
pub use crate::server::models::{EntityConfig, ReentrancyConfig, RegisteredActorsResponse};

use super::{Actor, ActorError, ActorFactory, context_client::ActorContextClient};

pub mod turn_lock;
pub use self::turn_lock::{ActorTurnLease, ActorTurnLock, REENTRANCY_ID_HEADER};

#[derive(Clone)]
pub struct ActorState {
pub actor_type: String,
Expand Down Expand Up @@ -83,6 +87,7 @@ pub struct ActorTypeRegistration {
name: String,
factory: ActorFactory,
method_registrations: MethodRegistrationMap,
pub reentrancy: Option<ReentrancyConfig>,
}

impl ActorTypeRegistration {
Expand All @@ -94,9 +99,15 @@ impl ActorTypeRegistration {
name: name.to_string(),
factory,
method_registrations: HashMap::new(),
reentrancy: None,
}
}

pub fn with_reentrancy(mut self, reentrancy: ReentrancyConfig) -> Self {
self.reentrancy = Some(reentrancy);
self
}

/// Registers a method on the actor type to be exposed to actor clients.
///
/// # Arguments:
Expand Down Expand Up @@ -206,6 +217,7 @@ pub struct ActorRuntime {

registered_actors_types: ActorRegistrationMap,
active_actors: ActiveActorMap,
turn_lock: ActorTurnLock,
}

impl ActorRuntime {
Expand All @@ -214,6 +226,47 @@ impl ActorRuntime {
dapr_client,
registered_actors_types: Arc::new(RwLock::new(HashMap::new())),
active_actors: Arc::new(RwLock::new(HashMap::new())),
turn_lock: ActorTurnLock::new(),
}
}

pub fn turn_lock(&self) -> &ActorTurnLock {
&self.turn_lock
}

pub async fn is_reentrancy_enabled(&self, actor_type: &str) -> bool {
let types = self.registered_actors_types.read().await;
if let Some(reg) = types.get(actor_type) {
reg.reentrancy.as_ref().map(|r| r.enabled).unwrap_or(false)
} else {
false
}
}

pub async fn get_registered_actors_config(&self) -> RegisteredActorsResponse {
let types = self.registered_actors_types.read().await;
let entities: Vec<String> = types.keys().cloned().collect();

let mut entities_config = Vec::new();
for (name, reg) in types.iter() {
if let Some(reentrancy) = &reg.reentrancy {
entities_config.push(EntityConfig {
entities: vec![name.clone()],
reentrancy: Some(reentrancy.clone()),
});
}
}

let entities_config = if entities_config.is_empty() {
None
} else {
Some(entities_config)
};

RegisteredActorsResponse {
entities,
reentrancy: None,
entities_config,
}
}

Expand Down
Loading