Dapr - actors之构建块(4)
Thanks to the turn-based access model, you don't need to worry about multiple threads with actors, making it much easier to write concurrent systems. The following actor example closely mirrors the code from the previous sample, but doesn't require any locking mechanisms to be correct:
由于使用的是Turn-based 的访问模型,因此,您无需担心使用actors 的多线程,使得编写并发系统变得更加容易。 下面的执行组件示例将对上一个示例中的代码进行密切镜像,但不需要任何锁定机制是正确的:
public async Task<int> IncrementAsync()
{
var counterValue = await StateManager.TryGetStateAsync<int>("counter");
var currentValue = counterValue.HasValue ? counterValue.Value : 0;
var newValue = currentValue + 1;
await StateManager.SetStateAsync("counter", newValue);
return newValue;
}
Timers and reminders
计时器和提醒
Actors can use timers and reminders to schedule calls to themselves. Both concepts support the configuration of a due time. The difference lies in the lifetime of the callback registrations:
Actors 可以使用计时器和提醒来调度自身的调用。 这两个概念都支持配置截止时间。 不同之处在于回调注册的生存期:
- Timers will only stay active as long as the the actor is activated. Timers will not reset the idle-timer, so they cannot keep an actor active on their own.
- 只要激活Actor,计时器就会保持活动状态。 计时器 不会 重置空闲计时器,因此它们不能使Actor 处于活动状态。
- Reminders outlive actor activations. If an actor is deactivated, a reminder will re-activate the actor. Reminders will reset the idle-timer.
- 提醒长于Actor激活。 如果停用了某个Actor,则会重新激活该执行组件。 提醒 将 重置空闲计时器。
Timers are registered by making a call to the actor API. In the following example, a timer is registered with a due time of 0 and a period of 10 seconds.
计时器是通过调用Actor API 来注册的。 在下面的示例中,在时间为0的情况下注册计时器,时间为10秒。
Bash
curl -X POST http://localhost:3500/v1.0/actors/<actorType>/<actorId>/timers/<name> \
-H "Content-Type: application/json" \
-d '{
"dueTime": "0h0m0s0ms",
"period": "0h0m10s0ms"
}'
Because the due time is 0, the timer will fire immediately. After a timer callback has finished, the timer will wait 10 seconds before firing again.
由于截止时间为0,因此将立即触发计时器。 计时器回调完成后,计时器将等待10秒,然后再次触发。
Reminders are registered in a similar way. The following example shows a reminder registration with a due time of 5 minutes, and an empty period:
提醒注册方式类似。 下面的示例演示了一个提醒注册,该注册的截止时间为5分钟,空时间为空:
Bash
curl -X POST http://localhost:3500/v1.0/actors/<actorType>/<actorId>/reminders/<name> \
-H "Content-Type: application/json" \
-d '{
"dueTime": "0h5m0s0ms",
"period": ""
}'
This reminder will fire in 5 minutes. Because the given period is empty, this will be a one-time reminder.
此提醒将在5分钟后激发。 由于给定时间段为空,这将为一次性提醒。
Note
注意:
Timers and reminders both respect the turn-based access model. When a timer or reminder fires, the callback will not be executed until any other method invocation or timer/reminder callback has finished.
计时器和提醒均遵循turn-based 的访问模型。 当计时器或提醒触发时,直到任何其他方法调用或计时器/提醒回调完成后才会执行回调。
- 点赞
- 收藏
- 关注作者
评论(0)