Dapr - actors之构建块(4)

举报
小云悠悠zZ 发表于 2023/01/30 16:32:47 2023/01/30
【摘要】 由于使用的是Turn-based 的访问模型,因此,您无需担心使用actors 的多线程,使得编写并发系统变得更加容易。 下面的执行组件示例将对上一个示例中的代码进行密切镜像,但不需要任何锁定机制是正确的。

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 的访问模型。 当计时器或提醒触发时,直到任何其他方法调用或计时器/提醒回调完成后才会执行回调。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。