Skip to content

Commit

Permalink
Publisher: reconnect if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvargiu committed Jan 10, 2018
1 parent 5426678 commit 55b7d00
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public function publish(string $body, string $routingKey = '', array $properties
);
$message = new AMQPMessage($body, $properties);

if ($this->reconnectEnabled && false === $this->getConnection()->select(1)) {
$this->reconnect();
if ($this->reconnectEnabled) {
$this->reconnectIfNecessary();
}

if ($this->isAutoSetupFabricEnabled()) {
Expand All @@ -96,4 +96,23 @@ public function publish(string $body, string $routingKey = '', array $properties
$routingKey
);
}

/**
* Check connection an reconnect if necessary
*/
private function reconnectIfNecessary(): void
{
// for lazy connections:
$this->getConnection()->getSocket();

try {
$connected = $this->getConnection()->select(1);
} catch (\ErrorException $e) {
$connected = false;
}

if (! $connected) {
$this->reconnect();
};
}
}

0 comments on commit 55b7d00

Please sign in to comment.