Skip to content

Commit

Permalink
Fix slow memory leak while an actor stays active (#109)
Browse files Browse the repository at this point in the history
* Fix memory leak

* Fix return type
  • Loading branch information
nox213 authored Mar 4, 2024
1 parent 1b76d54 commit 1ea75f1
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ private[shardcake] object EntityManager {
val maxIdleTime = entityMaxIdleTime getOrElse config.entityMaxIdleTime

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

(for {
_ <- sleep(maxIdleTime)
Expand Down

0 comments on commit 1ea75f1

Please sign in to comment.