New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JetStream Work Queue - PullSubscribe & Fetch #833
Comments
Note I have also tried explicitly creating the consumer and using Bind, but the results were the same, so simplified the above example. // Create a Consumer
_, err = js.AddConsumer("WORK", &nats.ConsumerConfig{
Durable: "WORK",
FilterSubject: "WORK.request",
DeliverPolicy: nats.DeliverAllPolicy,
AckPolicy: nats.AckExplicitPolicy,
AckWait: 30 * time.Second,
MaxDeliver: -1,
ReplayPolicy: nats.ReplayInstantPolicy,
SampleFrequency: "100",
})
if err != nil {
log.Fatalln(err)
} Edit - The above is now working. The example was invalid due to the static MsgId. I'll try and get a simple example replicated. |
I've been testing this further, and I wonder if the usage of a context without a timeout in: msgs, err := sub.Fetch(1, nats.Context(ctx)) is an issue, it seems everything works correctly if I use a child context per fetch with a short timeout, e.g., 10 seconds. I had assumed this would block indefinitely until a message was received. |
Hi @T-J-L yes would recommend for now to use an explicit timeout, by default the timeout that context uses is the same one as the JetStream context which by default is msg.InProgress(nats.AckWait(10*time.Second), nats.Context(ctx)) The context will be given preference and since it does not have a timeout it could potentially block, we will update the client so that you cannot pass both options. |
Thank you @wallyqs, removing the contexts has solved the problem. I've included my current fetch loop; this picks up new messages as soon as they are available but takes up to 30 seconds to react to the shutdown; this is acceptable for my application. For information, I tried the method of creating a context per fetch call, and there seems to be pauses between the messages being received. I would have to replicate this again, but it looked like rather than waiting for 30 seconds for a message, it would check for a message, then pause until the timeout was reached. As the messages would be picked up on the next loop. I’m happy with the current approach, so feel free to close this. Thanks again for your assistance. for {
msgs, err := sub.Fetch(1, nats.MaxWait(30*time.Second))
select {
case <-ctx.Done():
return
default:
}
if err != nil {
if errors.Is(err, nats.ErrTimeout) {
continue
}
s.Logger.Info("fetch failed", zap.Error(err))
continue
}
for _, msg := range msgs {
// ...
}
} |
Thanks @T-J-L for the report. This has been fixed via |
Hello,
I’m working on a POC with nats JetStream, to implement a durable work queue. The jobs will be low throughput, long running jobs, which may run for as long as 20 minutes. There will be multiple services publishing and receiving from a single subject.
I’m trying to accept the message, mark it as in progress using an upper bound for the processing time, perform the work, then ack once complete.
The issue I’m having is with js.PullSubscribe and sub.Fetch. If I use nats.Context(ctx) with Fetch, I seem to receive zero or one messages, the subscription then stops receiving any further messages.
I'm new to nats so I expect this is user error, any help would be appreciated. See an example below which reflects the scenario. I am expecting this to continuously publish and receive messages.
Versions:
-js -m 8222 -n nats_js_1
)The text was updated successfully, but these errors were encountered: