Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
Retry sending messages in the memory spool before aborting in case of…
Browse files Browse the repository at this point in the history
… transport failure
  • Loading branch information
Seldaek committed Apr 20, 2016
1 parent fffbc0e commit 4afba32
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/classes/Swift/MemorySpool.php
Expand Up @@ -16,6 +16,7 @@
class Swift_MemorySpool implements Swift_Spool
{
protected $messages = array();
private $flushRetries = 3;

/**
* Tests if this Transport mechanism has started.
Expand All @@ -41,6 +42,14 @@ public function stop()
{
}

/**
* @param int $retries
*/
public function setFlushRetries($retries)
{
$this->flushRetries = $retries;
}

/**
* Stores a message in the queue.
*
Expand Down Expand Up @@ -75,8 +84,25 @@ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null
}

$count = 0;
while ($message = array_pop($this->messages)) {
$count += $transport->send($message, $failedRecipients);
$retries = $this->flushRetries;
while ($retries--) {
try {
while ($message = array_pop($this->messages)) {
$count += $transport->send($message, $failedRecipients);
}
} catch (Swift_TransportException $exception) {
if ($retries) {
// re-queue the message at the end of the queue to give a chance
// to the other messages to be sent, in case the failure was due to
// this message and not just the transport failing
array_unshift($this->messages, $message);

// wait half a second before we try again
usleep(500000);
} else {
throw $exception;
}
}
}

return $count;
Expand Down

0 comments on commit 4afba32

Please sign in to comment.