Skip to content

Commit

Permalink
Race interruptibly in Channel.mergeAllWith (#1971)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 authored Jan 24, 2024
1 parent d404561 commit 7b84a3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-moose-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

race interruptibly in `Channel.mergeAllWith`
38 changes: 21 additions & 17 deletions packages/effect/src/internal/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,21 +1246,25 @@ export const mergeAllWith = (
),
onSuccess: Either.match({
onLeft: (outDone) =>
Effect.raceWith(Deferred.await(errorSignal), withPermits(concurrencyN)(Effect.unit), {
onSelfDone: (_, permitAcquisition) => Effect.as(Fiber.interrupt(permitAcquisition), false),
onOtherDone: (_, failureAwait) =>
Effect.zipRight(
Fiber.interrupt(failureAwait),
pipe(
Ref.get(lastDone),
Effect.flatMap(Option.match({
onNone: () => Queue.offer(queue, Effect.succeed(Either.left(outDone))),
onSome: (lastDone) => Queue.offer(queue, Effect.succeed(Either.left(f(lastDone, outDone))))
})),
Effect.as(false)
Effect.raceWith(
Effect.interruptible(Deferred.await(errorSignal)),
Effect.interruptible(withPermits(concurrencyN)(Effect.unit)),
{
onSelfDone: (_, permitAcquisition) => Effect.as(Fiber.interrupt(permitAcquisition), false),
onOtherDone: (_, failureAwait) =>
Effect.zipRight(
Fiber.interrupt(failureAwait),
pipe(
Ref.get(lastDone),
Effect.flatMap(Option.match({
onNone: () => Queue.offer(queue, Effect.succeed(Either.left(outDone))),
onSome: (lastDone) => Queue.offer(queue, Effect.succeed(Either.left(f(lastDone, outDone))))
})),
Effect.as(false)
)
)
)
}),
}
),
onRight: (channel) =>
_mergeStrategy.match(mergeStrategy, {
onBackPressure: () =>
Expand All @@ -1273,7 +1277,7 @@ export const mergeAllWith = (
Effect.flatMap((pull) =>
Effect.race(
evaluatePull(pull),
Deferred.await(errorSignal)
Effect.interruptible(Deferred.await(errorSignal))
)
),
Effect.scoped
Expand Down Expand Up @@ -1306,8 +1310,8 @@ export const mergeAllWith = (
Effect.flatMap((pull) =>
pipe(
evaluatePull(pull),
Effect.race(Deferred.await(errorSignal)),
Effect.race(Deferred.await(canceler))
Effect.race(Effect.interruptible(Deferred.await(errorSignal))),
Effect.race(Effect.interruptible(Deferred.await(canceler)))
)
),
Effect.scoped
Expand Down

0 comments on commit 7b84a3c

Please sign in to comment.