Skip to content

Commit

Permalink
Add retry connect (#350)
Browse files Browse the repository at this point in the history
Co-Authored-By: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Sep 22, 2023
1 parent 882b888 commit 9667f9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions publish/trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'databases_only' => env('TRIGGER_DATABASES_ONLY', '') ? explode(',', env('TRIGGER_DATABASES_ONLY')) : [],
'tables_only' => env('TRIGGER_TABLES_ONLY', '') ? explode(',', env('TRIGGER_TABLES_ONLY')) : [],
'heartbeat_period' => (int) env('TRIGGER_HEARTBEAT', 3),
'connect_retries' => 10,

'server_mutex' => [
'enable' => true,
Expand Down
12 changes: 9 additions & 3 deletions src/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use MySQLReplication\Socket\SocketException;

use function Hyperf\Support\make;
use function Hyperf\Support\retry;
use function Hyperf\Tappable\tap;

class Consumer
Expand Down Expand Up @@ -101,7 +102,12 @@ public function start(): void
try {
$replication->consume();
} catch (SocketException $e) {
$replication->connect();
retry(
(int) $this->getOption('connect_retries', 10),
fn () => $replication->connect(),
200
);
$this->debug('Connection lost, reconnected.');
}
}
};
Expand Down Expand Up @@ -200,9 +206,9 @@ protected function makeReplication(): MySQLReplicationFactory
make(MySQLReplicationFactory::class, [
'config' => $configBuilder->build(),
'eventDispatcher' => $eventDispatcher,
'logger' => $this->logger,
]),
function ($factory) use ($connection) {
/** @var MySQLReplicationFactory $factory */
function (MySQLReplicationFactory $factory) use ($connection) {
$subscribers = $this->subscriberManager->get($connection);
$subscribers[] = TriggerSubscriber::class;
$subscribers[] = SnapshotSubscriber::class;
Expand Down

0 comments on commit 9667f9e

Please sign in to comment.