Skip to content

Commit 0410920

Browse files
committed
refactor: snapshot stale keys before deleting in device GC loop
1 parent 7054939 commit 0410920

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/main/kotlin/eu/darken/octi/server/device/DeviceRepo.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ class DeviceRepo @Inject constructor(
7171
delay(config.deviceGCInterval.toMillis() / 10)
7272
while (currentCoroutineContext().isActive) {
7373
val now = Instant.now()
74-
devices.forEach { (key, device) ->
75-
if (Duration.between(device.lastSeen, now) < config.deviceExpiration) return@forEach
74+
val staleKeys = devices.entries
75+
.filter { Duration.between(it.value.lastSeen, now) >= config.deviceExpiration }
76+
.map { it.key }
77+
78+
for (key in staleKeys) {
7679
try {
7780
log(TAG, WARN) { "Deleting stale device $key" }
7881
deleteDevice(key)

0 commit comments

Comments
 (0)