Search code examples
c#nats-streaming-server

Nats streaming redelivery problems with c# stan.client


I am using nats streaming server and stan.client as client library.

My subscriber can do long work. Sometimes they are greater than default ack wait time (30sec). So I try to manually ack my message, doing like this:

StanSubscriptionOptions sOpts = StanSubscriptionOptions.GetDefaultOptions();

    sOpts.ManualAcks = true;

    EventHandler<StanMsgHandlerArgs> msgHandler = (sender, args) =>

    {

        args.Message.Ack();

        Thread.Sleep(40000);

    };

    sOpts.DurableName = "my-durable";

    var s = c.Subscribe(subject, qGroup, sOpts, msgHandler);

I set the manual ack then I ack the message as first action, before my work.

In this code, subscriber does a work for 40sec. If I send 2 messages, the second has always redelivered.

What is my mistake?


Solution

  • The first message will be acknowledged as soon as it is received, so will not be resent.

    If the second message is delivered to the client library and expires before it is processed by your callback, the NATS streaming server will redeliver it.

    With a 30 second AckWait, and a 40 second delay, this is very likely to happen.

    I solved increasing the AckWait through the StanSubscriptionOptions.AckWait option.