Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 9a0d56d

Browse files
committed
Reset app traces if no activity was found since last save
1 parent 8d1369e commit 9a0d56d

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Contracts/StatisticsCollector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,13 @@ public function getStatistics(): PromiseInterface;
6666
* @return PromiseInterface[\BeyondCode\LaravelWebSockets\Statistics\Statistic|null]
6767
*/
6868
public function getAppStatistics($appId): PromiseInterface;
69+
70+
/**
71+
* Remove all app traces from the database if no connections have been set
72+
* in the meanwhile since last save.
73+
*
74+
* @param string|int $appId
75+
* @return void
76+
*/
77+
public function resetAppTraces($appId);
6978
}

src/Statistics/Collectors/MemoryCollector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ public function save()
9292
continue;
9393
}
9494

95+
if ($statistic->shouldHaveTracesRemoved()) {
96+
$this->resetAppTraces($appId);
97+
98+
continue;
99+
}
100+
95101
$this->createRecord($statistic, $appId);
96102

97103
$this->channelManager->getGlobalConnectionsCount($appId)->then(function ($connections) use ($statistic) {
@@ -136,6 +142,18 @@ public function getAppStatistics($appId): PromiseInterface
136142
);
137143
}
138144

145+
/**
146+
* Remove all app traces from the database if no connections have been set
147+
* in the meanwhile since last save.
148+
*
149+
* @param string|int $appId
150+
* @return void
151+
*/
152+
public function resetAppTraces($appId)
153+
{
154+
unset($this->statistics[$appId]);
155+
}
156+
139157
/**
140158
* Find or create a defined statistic for an app.
141159
*

src/Statistics/Collectors/RedisCollector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ public function save()
161161
$appId, Helpers::redisListToArray($list)
162162
);
163163

164+
if ($statistic->shouldHaveTracesRemoved()) {
165+
return $this->resetAppTraces($appId);
166+
}
167+
164168
$this->createRecord($statistic, $appId);
165169

166170
$this->channelManager
@@ -272,6 +276,8 @@ public function resetStatistics($appId, int $currentConnectionCount)
272276
*/
273277
public function resetAppTraces($appId)
274278
{
279+
parent::resetAppTraces($appId);
280+
275281
$this->channelManager->getPublishClient()->hdel(
276282
$this->channelManager->getRedisKey($appId, null, ['stats']),
277283
'current_connections_count'

src/Statistics/Statistic.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,18 @@ public function reset(int $currentConnectionsCount)
183183
$this->apiMessagesCount = 0;
184184
}
185185

186+
/**
187+
* Check if the current statistic entry is empty. This means
188+
* that the statistic entry can be easily deleted if no activity
189+
* occured for a while.
190+
*
191+
* @return bool
192+
*/
193+
public function shouldHaveTracesRemoved(): bool
194+
{
195+
return $this->currentConnectionsCount === 0 && $this->peakConnectionsCount === 0;
196+
}
197+
186198
/**
187199
* Transform the statistic to array.
188200
*

0 commit comments

Comments
 (0)