From dd888f62878fde62b9cf736bc08653704e8a2181 Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Wed, 21 Oct 2026 03:02:11 +0000 Subject: [PATCH] refactor(persistence): inline CircuitBreaker option types --- .../effect/src/persistence/CircuitBreaker.ts | 88 ++++++++----------- .../test/persistence/CircuitBreakerTest.ts | 2 +- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/packages/effect/src/persistence/CircuitBreaker.ts b/packages/effect/src/persistence/CircuitBreaker.ts index 2fee2325db..1e76779d2b 100644 --- a/packages/effect/src/persistence/CircuitBreaker.ts +++ b/packages/effect/src/persistence/CircuitBreaker.ts @@ -49,7 +49,11 @@ export type TypeId = "~effect/persistence/CircuitBreaker" * **Details** * * - `execute` admits the call through the circuit for `key`, runs it, - * classifies its exit, and records the outcome. + * classifies its exit, and records the outcome. `classify` sees the full + * exit of the call and returns an `Outcome`, either directly or as an + * effect. Without it, every success is a `Success` and every failure, + * defects included, is a `Failure`. An exit that is only interruption is + * recorded as `Ignored` before `classify` runs. * - `state` reads the shared state of a circuit, with relative times only. * - `transitions` emits the transitions made by this breaker instance only, * and ends when the breaker's scope closes. @@ -62,12 +66,16 @@ export interface CircuitBreaker { readonly [TypeId]: TypeId readonly execute: { - (options: ExecuteOptions): ( - self: Effect.Effect - ) => Effect.Effect + (options: { + readonly key: string + readonly classify?: ((exit: Exit.Exit) => Outcome | Effect.Effect) | undefined + }): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, - options: ExecuteOptions + options: { + readonly key: string + readonly classify?: ((exit: Exit.Exit) => Outcome | Effect.Effect) | undefined + } ): Effect.Effect } @@ -76,25 +84,6 @@ export interface CircuitBreaker { readonly transitions: Stream.Stream } -/** - * Options for a single `CircuitBreaker.execute` call. - * - * **Details** - * - * `classify` sees the full exit of the call and returns an `Outcome`, either - * directly or as an effect. Without it, every success is a `Success` and every - * failure, defects included, is a `Failure`. An exit that is only - * interruption is recorded as `Ignored` before `classify` runs. - * - * @stability experimental - * @category models - * @since 4.0.6 - */ -export interface ExecuteOptions { - readonly key: string - readonly classify?: ((exit: Exit.Exit) => Outcome | Effect.Effect) | undefined -} - /** * Service tag for the default `CircuitBreaker`. * @@ -107,7 +96,7 @@ export const CircuitBreaker: Context.Service = C ) /** - * Policy used to build a `CircuitBreaker`. + * Creates a `CircuitBreaker` from the current `CircuitBreakerStore`. * * **Details** * @@ -120,30 +109,19 @@ export const CircuitBreaker: Context.Service = C * `"allow"` (the default) runs it unrecorded and logs a warning, `"fail"` * fails it with `CircuitBreakerStoreError`. * - * @stability experimental - * @category models - * @since 4.0.6 - */ -export interface Options { - readonly tripPolicy: TripPolicy - readonly openDuration: Duration.Input | Schedule.Schedule - readonly maxOpenDuration?: Duration.Input | undefined - readonly onStoreError?: "allow" | "fail" | undefined -} - -/** - * Creates a `CircuitBreaker` from the current `CircuitBreakerStore`. - * - * **Details** - * * The breaker's `transitions` stream ends when the surrounding scope closes. * * @stability experimental * @category constructors * @since 4.0.6 */ -export const make: (options: Options) => Effect.Effect = - Effect.fnUntraced(function*(options: Options) { +export const make: (options: { + readonly tripPolicy: TripPolicy + readonly openDuration: Duration.Input | Schedule.Schedule + readonly maxOpenDuration?: Duration.Input | undefined + readonly onStoreError?: "allow" | "fail" | undefined +}) => Effect.Effect = Effect.fnUntraced( + function*(options: Parameters[0]) { const store = yield* CircuitBreakerStore const scope = yield* Effect.scope const pubsub = yield* PubSub.sliding>(128) @@ -169,7 +147,10 @@ export const make: (options: Options) => Effect.Effect( self: Effect.Effect, - { classify, key }: ExecuteOptions + { classify, key }: { + readonly key: string + readonly classify?: ((exit: Exit.Exit) => Outcome | Effect.Effect) | undefined + } ): Effect.Effect => Effect.uninterruptibleMask((restore) => Effect.flatMap(Scope.make(), (admitScope) => @@ -222,21 +203,30 @@ export const make: (options: Options) => Effect.Effect => - Layer.effect(CircuitBreaker, make(options)) +export const layer = (options: { + readonly tripPolicy: TripPolicy + readonly openDuration: Duration.Input | Schedule.Schedule + readonly maxOpenDuration?: Duration.Input | undefined + readonly onStoreError?: "allow" | "fail" | undefined +}): Layer.Layer => Layer.effect(CircuitBreaker, make(options)) const classifyExit = ( exit: Exit.Exit, - classify: ExecuteOptions["classify"], + classify: ((exit: Exit.Exit) => Outcome | Effect.Effect) | undefined, restore: (effect: Effect.Effect) => Effect.Effect ): Effect.Effect, never, R2> => { if (Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)) { @@ -252,7 +242,7 @@ const classifyExit = ( } const makeOpenDuration = ( - openDuration: Options["openDuration"] + openDuration: Duration.Input | Schedule.Schedule ): (consecutiveOpenings: number) => Effect.Effect => { if (!Schedule.isSchedule(openDuration)) { const duration = Effect.succeed(Duration.fromInputUnsafe(openDuration)) diff --git a/packages/effect/test/persistence/CircuitBreakerTest.ts b/packages/effect/test/persistence/CircuitBreakerTest.ts index f2c5bdac59..e75474ab65 100644 --- a/packages/effect/test/persistence/CircuitBreakerTest.ts +++ b/packages/effect/test/persistence/CircuitBreakerTest.ts @@ -27,7 +27,7 @@ const held = Effect.fnUntraced(function*( breaker: CircuitBreaker.CircuitBreaker, key: string, result: Effect.Effect, - options?: { readonly classify?: CircuitBreaker.ExecuteOptions["classify"] } + options?: { readonly classify?: (exit: Exit.Exit) => CircuitBreaker.Outcome } ) { const started = yield* Deferred.make() const gate = yield* Deferred.make()