MCPcopy Index your code
hub / github.com/Effect-TS/effect / strategyCreationTTL

Function strategyCreationTTL

packages/effect/src/internal/pool.ts:352–384  ·  view source on GitHub ↗
(ttl: Duration.DurationInput)

Source from the content-addressed store, hash-verified

350})
351
352const strategyCreationTTL = <A, E>(ttl: Duration.DurationInput) =>
353 defaultServices.clockWith((clock) =>
354 core.map(internalQueue.unbounded<PoolItem<A, E>>(), (queue) => {
355 const ttlMillis = Duration.toMillis(ttl)
356 const creationTimes = new WeakMap<PoolItem<A, E>, number>()
357 return identity<Strategy<A, E>>({
358 run: (pool) => {
359 const process = (item: PoolItem<A, E>): Effect<void> =>
360 core.suspend(() => {
361 if (!pool.items.has(item) || pool.invalidated.has(item)) {
362 return core.void
363 }
364 const now = clock.unsafeCurrentTimeMillis()
365 const created = creationTimes.get(item)!
366 const remaining = ttlMillis - (now - created)
367 return remaining > 0
368 ? coreEffect.delay(process(item), remaining)
369 : pool.invalidatePoolItem(item)
370 })
371 return queue.take.pipe(
372 core.tap(process),
373 coreEffect.forever
374 )
375 },
376 onAcquire: (item) =>
377 core.suspend(() => {
378 creationTimes.set(item, clock.unsafeCurrentTimeMillis())
379 return queue.offer(item)
380 }),
381 reclaim: (_) => coreEffect.succeedNone
382 })
383 })
384 )
385
386const strategyUsageTTL = <A, E>(ttl: Duration.DurationInput) =>
387 core.map(internalQueue.unbounded<PoolItem<A, E>>(), (queue) => {

Callers 1

makeWithTTLFunction · 0.85

Calls 6

identityFunction · 0.85
mapMethod · 0.65
pipeMethod · 0.65
setMethod · 0.65
offerMethod · 0.65

Tested by

no test coverage detected