Skip to content

Commit

Permalink
Add an activity timeout configuration option (#241)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent 5949bef commit d0e83cc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/reverb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
],
Expand Down
12 changes: 11 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/ConfigApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? [],
Expand Down
2 changes: 1 addition & 1 deletion src/Protocols/Pusher/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/FakeApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions tests/ReverbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand All @@ -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,
]);
}
Expand Down

0 comments on commit d0e83cc

Please sign in to comment.