Skip to content

Commit

Permalink
Fix: Prevent message duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
grachevko committed Jul 8, 2021
1 parent 4fac0e0 commit 9d2ef8e
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Messenger/NsqSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,19 @@ public function send(Envelope $envelope): Envelope
$delayStamp = $envelope->last(DelayStamp::class);
$delay = null !== $delayStamp ? $delayStamp->getDelay() : 0;

$promise = null;
if (null === $envelope->last(NsqReceivedStamp::class)) {
$encodedMessage = $this->serializer->encode($envelope);
$encodedMessage = json_encode($encodedMessage, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);

if (null !== $envelope->last(NsqReceivedStamp::class)) {
wait($producer->publish($this->topic, $encodedMessage, $delay));
} else {
$message = NsqReceivedStamp::getMessageFromEnvelope($envelope);

if (!$message->isProcessed()) {
$promise = $message->requeue($delay);
wait($message->requeue($delay));
}
}

if (null === $promise) {
$encodedMessage = $this->serializer->encode($envelope->withoutAll(NsqReceivedStamp::class));
$encodedMessage = json_encode($encodedMessage, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);

$promise = $producer->publish($this->topic, $encodedMessage, $delay);
}

wait($promise);

return $envelope;
}

Expand Down

0 comments on commit 9d2ef8e

Please sign in to comment.