| 7 | |
| 8 | /** @internal */ |
| 9 | export class EntityReaper extends Effect.Service<EntityReaper>()("@effect/cluster/EntityReaper", { |
| 10 | scoped: Effect.gen(function*() { |
| 11 | let currentResolution = 30_000 |
| 12 | const registered: Array<{ |
| 13 | readonly maxIdleTime: number |
| 14 | readonly servers: Map<EntityId, EntityState> |
| 15 | readonly entities: ResourceMap<EntityAddress, EntityState, EntityNotAssignedToRunner> |
| 16 | }> = [] |
| 17 | const latch = yield* Effect.makeLatch() |
| 18 | |
| 19 | const register = (options: { |
| 20 | readonly maxIdleTime: number |
| 21 | readonly servers: Map<EntityId, EntityState> |
| 22 | readonly entities: ResourceMap<EntityAddress, EntityState, EntityNotAssignedToRunner> |
| 23 | }) => |
| 24 | Effect.suspend(() => { |
| 25 | currentResolution = Math.max(Math.min(currentResolution, options.maxIdleTime), 5000) |
| 26 | registered.push(options) |
| 27 | return latch.open |
| 28 | }) |
| 29 | |
| 30 | const clock = yield* Effect.clock |
| 31 | yield* Effect.gen(function*() { |
| 32 | while (true) { |
| 33 | yield* Effect.sleep(currentResolution) |
| 34 | const now = clock.unsafeCurrentTimeMillis() |
| 35 | for (const { entities, maxIdleTime, servers } of registered) { |
| 36 | for (const state of servers.values()) { |
| 37 | const duration = now - state.lastActiveCheck |
| 38 | if (state.keepAliveEnabled || state.activeRequests.size > 0 || duration < maxIdleTime) { |
| 39 | continue |
| 40 | } |
| 41 | yield* Effect.fork(entities.removeIgnore(state.address)) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | }).pipe( |
| 46 | latch.whenOpen, |
| 47 | Effect.interruptible, |
| 48 | Effect.forkScoped |
| 49 | ) |
| 50 | |
| 51 | return { register } as const |
| 52 | }) |
| 53 | }) {} |
nothing calls this directly
no test coverage detected
searching dependent graphs…