简体   繁体   English

Akka.Net Streams-缓冲区引发错误时,源停止提取元素

[英]Akka.Net Streams - Source stops pulling elements when buffer throws error

I've been playing a little with the streams extension package for Akka.Net and noticed this error at attempting to combine buffer and throttle methods: 我一直在玩Akka.Net的流扩展包 ,并在尝试组合缓冲区和节流方法时注意到此错误:

using (var system = ActorSystem.Create("test-system"))
using (var materializer = system.Materializer(GetSettings(system)))
{
            int index = 0;
            var sink = Sink.ActorRefWithAck<KeyValue>(
                system.ActorOf<Writer>(), 
                new OnInitMessage(), 
                new OnAcknowledgeMessage(), 
                OnComplete.Instance, 
                exception => new OnError(exception));

            ServiceBusSource
                .Create(client, message =>
                {
                    var json = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
                    var result = JsonConvert.DeserializeObject<KeyValue>(json);

                    message.Complete();

                    return result;
                })
                .WithLogger(system, entity => $"{entity.Key} => {entity.Value}")
                .Buffer(1, OverflowStrategy.Fail)
                .Throttle(1, TimeSpan.FromSeconds(5), 3, ThrottleMode.Shaping)
                .ToMaterialized(sink, Keep.Right)
                .Run(materializer);

            Console.ReadLine();
}

I'm using ServiceBusSource from Alpakka These are the packages I'm referencing: 我正在使用Alpakka的ServiceBusSource,这些是我引用的软件包:

  • Akka.Streams: 1.3.1 Akka.Streams:1.3.1
  • Akka.Streams.Azure.ServiceBus: 0.1.0 Akka.Streams.Azure.ServiceBus:0.1.0
  • WindowsAzure.ServiceBus: 4.1.3 WindowsAzure.ServiceBus:4.1.3

I'm intentionally making it fail in order to see how behaves BUT , after failing from buffer's strategy, the stream completes and no more elements are being pulled. 故意使它失败,以便查看BUT的行为,从缓冲区策略失败后,流完成,并且不再提取任何元素。

KeyValue.cs KeyValue.cs

public class KeyValue
{
    public int Id { get; set; }

    public string Key { get; set; }

    public string Value { get; set; }

    public DateTime Produced { get; set; }

    public DateTime Emitted { get; set; }

    public override string ToString()
    {
        return $"[{Produced}] - [{Emitted}] => {Id} {Key}:{Value}";
    }
}

GetSettings Method: GetSettings方法:

ActorMaterializerSettings GetSettings(ActorSystem system)
        {
            return ActorMaterializerSettings.Create(system)
                .WithSupervisionStrategy(cause =>
                {
                    system.Log.Error(cause, "Failed");
                    return Directive.Resume;
                });
        }

There are several ways of handling errors inside of a stream - most of them described in docs : 有几种处理流内部错误的方法-大多数方法在docs中进行了描述:

  1. Use Recover to make a fallback event from error. 使用Recover使错误发生后备事件。
  2. Use RecoverWithRetries to allow to redirect to a different stream upon error. 使用RecoverWithRetries允许在发生错误时重定向到其他流。
  3. Use Restart.WithBackoff to rebuild a retry stream after exponential backoff delay. 在指数回退延迟后,使用Restart.WithBackoff重建重试流。
  4. Use WithSupervisionStrategy - which is a very limited option, as it works only on stages that refer to it explicitly (as explained in docs). WithSupervisionStrategy使用-这是一个非常有限的选项,因为它仅在显式引用它的阶段起作用(如docs中所述)。

Your case is by design - when you use OverflowStrategy.Fail it means, that once overflow is reached, an error will be produced. 您的案例是设计OverflowStrategy.Fail -使用OverflowStrategy.Fail意味着一旦达到溢出,就会产生一个错误。 Reaction of most of the akka stages is to close stream immediately upon failure. 大部分akka阶段的反应是在发生故障时立即关闭流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM