Update InMemoryScheduler and ScheduledSend #1993
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So I have been working on adding NATS as a transport protocol for Wolverine and while I was running through the compliance tests before opening the PR, discovered a couple of issues. Would love any feedback or thoughts on this fix but the core of the issue is outlined below.
Summary
Scheduled messages fail to reach their destination when using transports that don't support native scheduled send (RabbitMQ, Kafka, Redis, AWS SQS/SNS, GCP Pub/Sub, MQTT, etc.) in inline or buffered mode. Instead, the message gets received back on the sender's response queue.
Discovery
While implementing a new NATS transport, the
schedule_sendcompliance test was failing. The scheduled message was being sent, but it arrived on the sender's own response subject instead of the receiver's subject.The bug
For transports without native scheduling, Wolverine wraps the original envelope inside a "scheduled-envelope" wrapper and sends it to
local://durable/. When the scheduled time arrives,ScheduledSendEnvelopeHandlerunwraps the inner envelope and forwards it to the actual transport.The problem: the inner envelope is deserialized with its original
Status = ScheduledandScheduledTimestill set. Later, whenFlushOutgoingMessagesAsync()processes outgoing messages, it callsIsScheduledForLater():Because
Status == Scheduled, this returnstrueeven though we're past the scheduled time and should be sending now. This causes the message to skip the actual send path and instead get re-scheduled locally, where it eventually ends up on the wrong queue.The fix
ScheduledSendEnvelopeHandler.cs
When the scheduled envelope wrapper is processed and we extract the inner envelope, we need to clear the scheduling properties since we're now executing the send:
Without this,
IsScheduledForLater()returnstruebecause it checksif (Status == EnvelopeStatus.Scheduled) return true;, causing the message to take the wrong code path inFlushOutgoingMessagesAsync().InMemoryScheduledJobProcessor.cs
While investigating, we also found two issues in the in-memory scheduler:
DateTimeOffset.Nowinstead ofDateTimeOffset.UtcNowwhen calculating delay, butExecutionTimeis stored in UTCTask.Delay()would receive a negative TimeSpanAffected transports
All transports where
SupportsNativeScheduledSendreturnsfalse:Testing
Verified with NATS transport compliance tests -
schedule_sendnow passes consistently.