Skip to content

Commit 1ea75f1

Browse files
authored
Fix slow memory leak while an actor stays active (#109)
* Fix memory leak * Fix return type
1 parent 1b76d54 commit 1ea75f1

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

entities/src/main/scala/com/devsisters/shardcake/internal/EntityManager.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ private[shardcake] object EntityManager {
6161
val maxIdleTime = entityMaxIdleTime getOrElse config.entityMaxIdleTime
6262

6363
def sleep(duration: Duration): UIO[Unit] =
64-
for {
65-
_ <- Clock.sleep(duration)
66-
cdt <- currentTimeInMilliseconds
67-
map <- entitiesLastReceivedAt.get
68-
lastReceivedAt = map.getOrElse(entityId, 0L)
69-
remaining = maxIdleTime minus Duration.fromMillis(cdt - lastReceivedAt)
70-
_ <- sleep(remaining).when(remaining > Duration.Zero)
71-
} yield ()
64+
(Clock.sleep(duration) *> currentTimeInMilliseconds <*> entitiesLastReceivedAt.get).flatMap { case (cdt, map) =>
65+
val lastReceivedAt = map.getOrElse(entityId, 0L)
66+
val remaining = maxIdleTime minus Duration.fromMillis(cdt - lastReceivedAt)
67+
// do not use ZIO.when to prevent zio stack memory leak
68+
if (remaining > Duration.Zero) sleep(remaining) else ZIO.unit
69+
}
7270

7371
(for {
7472
_ <- sleep(maxIdleTime)

0 commit comments

Comments
 (0)