/** * @since 1.0.0 */ import type * as Data from "@effect/data/Data" import type * as Duration from "@effect/data/Duration" import type { Pipeable } from "@effect/data/Pipeable" import type * as Effect from "@effect/io/Effect" import * as internal from "@effect/io/internal/pool" import type * as Scope from "@effect/io/Scope" /** * @since 1.0.0 * @category symbols */ export const PoolTypeId: unique symbol = internal.PoolTypeId /** * @since 1.0.0 * @category symbols */ export type PoolTypeId = typeof PoolTypeId /** * A `Pool` is a pool of items of type `A`, each of which may be * associated with the acquisition and release of resources. An attempt to get * an item `A` from a pool may fail with an error of type `E`. * * @since 1.0.0 * @category models */ export interface Pool extends Data.Case, Pool.Variance, Pipeable { /** * Retrieves an item from the pool in a scoped effect. Note that if * acquisition fails, then the returned effect will fail for that same reason. * Retrying a failed acquisition attempt will repeat the acquisition attempt. */ get(): Effect.Effect /** * Invalidates the specified item. This will cause the pool to eventually * reallocate the item, although this reallocation may occur lazily rather * than eagerly. */ invalidate(item: A): Effect.Effect } /** * @since 1.0.0 */ export declare namespace Pool { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [PoolTypeId]: { readonly _E: (_: never) => E readonly _A: (_: never) => A } } } /** * Returns `true` if the specified value is a `Pool`, `false` otherwise. * * @since 1.0.0 * @category refinements */ export const isPool: (u: unknown) => u is Pool = internal.isPool /** * Makes a new pool of the specified fixed size. The pool is returned in a * `Scope`, which governs the lifetime of the pool. When the pool is shutdown * because the `Scope` is closed, the individual items allocated by the pool * will be released in some unspecified order. * * @since 1.0.0 * @category constructors */ export const make: ( options: { readonly acquire: Effect.Effect readonly size: number } ) => Effect.Effect> = internal.make /** * Makes a new pool with the specified minimum and maximum sizes and time to * live before a pool whose excess items are not being used will be shrunk * down to the minimum size. The pool is returned in a `Scope`, which governs * the lifetime of the pool. When the pool is shutdown because the `Scope` is * used, the individual items allocated by the pool will be released in some * unspecified order. * * ```ts * import * as Duration from "@effect/data/Duration" * import * as Effect from "@effect/io/Effect" * import * as Pool from "@effect/io/Pool" * import * as Scope from "@effect/io/Scope" * import { pipe } from "@effect/data/Function" * * Effect.scoped( * pipe( * Pool.make(acquireDbConnection, 10, 20, Duration.seconds(60)), * Effect.flatMap((pool) => * Effect.scoped( * pipe( * pool.get(), * Effect.flatMap((connection) => useConnection(connection)) * ) * ) * ) * ) * ) * ``` * * @since 1.0.0 * @category constructors */ export const makeWithTTL: (options: { readonly acquire: Effect.Effect readonly min: number readonly max: number readonly timeToLive: Duration.DurationInput }) => Effect.Effect> = internal.makeWithTTL /** * Retrieves an item from the pool in a scoped effect. Note that if * acquisition fails, then the returned effect will fail for that same reason. * Retrying a failed acquisition attempt will repeat the acquisition attempt. * * @since 1.0.0 * @category getters */ export const get: (self: Pool) => Effect.Effect = internal.get /** * Invalidates the specified item. This will cause the pool to eventually * reallocate the item, although this reallocation may occur lazily rather * than eagerly. * * @since 1.0.0 * @category combinators */ export const invalidate: { (value: A): (self: Pool) => Effect.Effect (self: Pool, value: A): Effect.Effect } = internal.invalidate