From d0e83cc0552bb1b1048a259ab7f0153dc2f02b8a Mon Sep 17 00:00:00 2001 From: Zeke Date: Wed, 21 Aug 2024 13:03:53 -0300 Subject: [PATCH] Add an activity timeout configuration option (#241) * Update activity timeout to match configuration. The current `activity_timeout` option when establishing a connection was hardcoded to 30 seconds, but sometimes we need our application to ping at a different rate. This value should be configured through the proper config file using the `ping_interval` option which takes `REVERB_APP_PING_INTERVAL` environment variable or 60 by default. This update takes the value which is loaded onto the `Application` class and is available through the `Connection` class, making this an easy fix. * Update configuration to match old hardcoded value This change makes sure that all future applications that re-publish the configuration keep the original 30 second value instead of the 60 second default configuration. All applications that modified this value in their environment file should not be affected. * This update introduced the `activity_timeout` configuration to use instead of `ping_interval` to separate the concerns and accomplish the same result. * Updated test case to include `activity_timeout` in the environment definition. Using `30` as the default value as this was the original value in the package. * set default value * update docblock * Update Application.php --------- Co-authored-by: Joe Dixon Co-authored-by: Taylor Otwell --- config/reverb.php | 1 + src/Application.php | 12 +++++++++++- src/ConfigApplicationProvider.php | 1 + src/Protocols/Pusher/EventHandler.php | 2 +- tests/FakeApplicationProvider.php | 2 +- tests/ReverbTestCase.php | 2 ++ 6 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config/reverb.php b/config/reverb.php index 798ead63..3fead53b 100644 --- a/config/reverb.php +++ b/config/reverb.php @@ -82,6 +82,7 @@ ], 'allowed_origins' => ['*'], 'ping_interval' => env('REVERB_APP_PING_INTERVAL', 60), + 'activity_timeout' => env('REVERB_APP_ACTIVITY_TIMEOUT', 30), 'max_message_size' => env('REVERB_APP_MAX_MESSAGE_SIZE', 10_000), ], ], diff --git a/src/Application.php b/src/Application.php index 6fed151e..b4f50bc5 100644 --- a/src/Application.php +++ b/src/Application.php @@ -12,6 +12,7 @@ public function __construct( protected string $key, protected string $secret, protected int $pingInterval, + protected int $activityTimeout, protected array $allowedOrigins, protected int $maxMessageSize, protected array $options = [], @@ -54,13 +55,21 @@ public function allowedOrigins(): array } /** - * Get the interval in minutes to ping the client. + * Get the client ping interval in seconds. */ public function pingInterval(): int { return $this->pingInterval; } + /** + * Get the activity timeout in seconds. + */ + public function activityTimeout(): int + { + return $this->activityTimeout; + } + /** * Get the maximum message size allowed from the client. */ @@ -89,6 +98,7 @@ public function toArray(): array 'key' => $this->key, 'secret' => $this->secret, 'ping_interval' => $this->pingInterval, + 'activity_timeout' => $this->activityTimeout, 'allowed_origins' => $this->allowedOrigins, 'max_message_size' => $this->maxMessageSize, 'options' => $this->options, diff --git a/src/ConfigApplicationProvider.php b/src/ConfigApplicationProvider.php index e39e43cc..642408c5 100644 --- a/src/ConfigApplicationProvider.php +++ b/src/ConfigApplicationProvider.php @@ -66,6 +66,7 @@ public function find(string $key, mixed $value): Application $app['key'], $app['secret'], $app['ping_interval'], + $app['activity_timeout'] ?? 30, $app['allowed_origins'], $app['max_message_size'], $app['options'] ?? [], diff --git a/src/Protocols/Pusher/EventHandler.php b/src/Protocols/Pusher/EventHandler.php index 5360fa04..c886e90e 100644 --- a/src/Protocols/Pusher/EventHandler.php +++ b/src/Protocols/Pusher/EventHandler.php @@ -46,7 +46,7 @@ public function acknowledge(Connection $connection): void { $this->send($connection, 'connection_established', [ 'socket_id' => $connection->id(), - 'activity_timeout' => 30, + 'activity_timeout' => $connection->app()->activityTimeout(), ]); } diff --git a/tests/FakeApplicationProvider.php b/tests/FakeApplicationProvider.php index dbc57e89..92efd810 100644 --- a/tests/FakeApplicationProvider.php +++ b/tests/FakeApplicationProvider.php @@ -21,7 +21,7 @@ class FakeApplicationProvider implements ApplicationProvider public function __construct() { $this->apps = collect([ - new Application('id', 'key', 'secret', 60, ['*'], 10_000, [ + new Application('id', 'key', 'secret', 60, 30, ['*'], 10_000, [ 'host' => 'localhost', 'port' => 443, 'scheme' => 'https', diff --git a/tests/ReverbTestCase.php b/tests/ReverbTestCase.php index b30b42ac..a8ee272b 100644 --- a/tests/ReverbTestCase.php +++ b/tests/ReverbTestCase.php @@ -53,6 +53,7 @@ protected function defineEnvironment($app): void 'capacity' => null, 'allowed_origins' => ['*'], 'ping_interval' => 10, + 'activity_timeout' => 30, 'max_message_size' => 1_000_000, ]); @@ -63,6 +64,7 @@ protected function defineEnvironment($app): void 'capacity' => null, 'allowed_origins' => ['laravel.com'], 'ping_interval' => 10, + 'activity_timeout' => 30, 'max_message_size' => 1, ]); }