Reset app traces if no activity was found since last save

This commit is contained in:
Alex Renoki 2020-12-07 23:16:51 +02:00
parent 8d1369ee02
commit 9a0d56d6d3
4 changed files with 45 additions and 0 deletions

View File

@ -66,4 +66,13 @@ interface StatisticsCollector
* @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null] * @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null]
*/ */
public function getAppStatistics($appId): PromiseInterface; public function getAppStatistics($appId): PromiseInterface;
/**
* Remove all app traces from the database if no connections have been set
* in the meanwhile since last save.
*
* @param string|int $appId
* @return void
*/
public function resetAppTraces($appId);
} }

View File

@ -92,6 +92,12 @@ class MemoryCollector implements StatisticsCollector
continue; continue;
} }
if ($statistic->shouldHaveTracesRemoved()) {
$this->resetAppTraces($appId);
continue;
}
$this->createRecord($statistic, $appId); $this->createRecord($statistic, $appId);
$this->channelManager->getGlobalConnectionsCount($appId)->then(function ($connections) use ($statistic) { $this->channelManager->getGlobalConnectionsCount($appId)->then(function ($connections) use ($statistic) {
@ -136,6 +142,18 @@ class MemoryCollector implements StatisticsCollector
); );
} }
/**
* Remove all app traces from the database if no connections have been set
* in the meanwhile since last save.
*
* @param string|int $appId
* @return void
*/
public function resetAppTraces($appId)
{
unset($this->statistics[$appId]);
}
/** /**
* Find or create a defined statistic for an app. * Find or create a defined statistic for an app.
* *

View File

@ -161,6 +161,10 @@ class RedisCollector extends MemoryCollector
$appId, Helpers::redisListToArray($list) $appId, Helpers::redisListToArray($list)
); );
if ($statistic->shouldHaveTracesRemoved()) {
return $this->resetAppTraces($appId);
}
$this->createRecord($statistic, $appId); $this->createRecord($statistic, $appId);
$this->channelManager $this->channelManager
@ -272,6 +276,8 @@ class RedisCollector extends MemoryCollector
*/ */
public function resetAppTraces($appId) public function resetAppTraces($appId)
{ {
parent::resetAppTraces($appId);
$this->channelManager->getPublishClient()->hdel( $this->channelManager->getPublishClient()->hdel(
$this->channelManager->getRedisKey($appId, null, ['stats']), $this->channelManager->getRedisKey($appId, null, ['stats']),
'current_connections_count' 'current_connections_count'

View File

@ -183,6 +183,18 @@ class Statistic
$this->apiMessagesCount = 0; $this->apiMessagesCount = 0;
} }
/**
* Check if the current statistic entry is empty. This means
* that the statistic entry can be easily deleted if no activity
* occured for a while.
*
* @return bool
*/
public function shouldHaveTracesRemoved(): bool
{
return $this->currentConnectionsCount === 0 && $this->peakConnectionsCount === 0;
}
/** /**
* Transform the statistic to array. * Transform the statistic to array.
* *