-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release loser eagerly in Resource.race
#3226
Changes from all commits
ebf8c2a
1f379b2
e27d21a
0c69b8d
7f14fa3
a5798d4
be9831c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,7 +303,67 @@ sealed abstract class Resource[F[_], +A] extends Serializable { | |
def race[B]( | ||
that: Resource[F, B] | ||
)(implicit F: Concurrent[F]): Resource[F, Either[A, B]] = | ||
Concurrent[Resource[F, *]].race(this, that) | ||
Resource.applyFull { poll => | ||
def cancelLoser[C](f: Fiber[F, Throwable, (C, ExitCase => F[Unit])]): F[Unit] = | ||
f.cancel *> | ||
f.join | ||
.flatMap( | ||
_.fold( | ||
F.unit, | ||
_ => F.unit, | ||
_.flatMap(_._2.apply(ExitCase.Canceled)) | ||
) | ||
) | ||
|
||
poll(F.racePair(this.allocatedCase, that.allocatedCase)).flatMap { | ||
case Left((oc, f)) => | ||
oc match { | ||
case Outcome.Succeeded(fa) => | ||
cancelLoser(f).start.flatMap { f => | ||
fa.map { | ||
case (a, fin) => | ||
( | ||
Either.left[A, B](a), | ||
fin(_: ExitCase).guarantee(f.join.flatMap(_.embedNever))) | ||
} | ||
} | ||
Comment on lines
+322
to
+329
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike racing |
||
case Outcome.Errored(ea) => | ||
F.raiseError[(Either[A, B], ExitCase => F[Unit])](ea).guarantee(cancelLoser(f)) | ||
case Outcome.Canceled() => | ||
poll(f.join).onCancel(f.cancel).flatMap { | ||
case Outcome.Succeeded(fb) => | ||
fb.map { case (b, fin) => (Either.right[A, B](b), fin) } | ||
case Outcome.Errored(eb) => | ||
F.raiseError[(Either[A, B], ExitCase => F[Unit])](eb) | ||
case Outcome.Canceled() => | ||
poll(F.canceled) *> F.never[(Either[A, B], ExitCase => F[Unit])] | ||
} | ||
} | ||
case Right((f, oc)) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if the code for the |
||
oc match { | ||
case Outcome.Succeeded(fb) => | ||
cancelLoser(f).start.flatMap { f => | ||
fb.map { | ||
case (b, fin) => | ||
( | ||
Either.right[A, B](b), | ||
fin(_: ExitCase).guarantee(f.join.flatMap(_.embedNever))) | ||
} | ||
} | ||
case Outcome.Errored(eb) => | ||
F.raiseError[(Either[A, B], ExitCase => F[Unit])](eb).guarantee(cancelLoser(f)) | ||
case Outcome.Canceled() => | ||
poll(f.join).onCancel(f.cancel).flatMap { | ||
case Outcome.Succeeded(fa) => | ||
fa.map { case (a, fin) => (Either.left[A, B](a), fin) } | ||
case Outcome.Errored(ea) => | ||
F.raiseError[(Either[A, B], ExitCase => F[Unit])](ea) | ||
case Outcome.Canceled() => | ||
poll(F.canceled) *> F.never[(Either[A, B], ExitCase => F[Unit])] | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Implementation for the `flatMap` operation, as described via the `cats.Monad` type class. | ||
|
@@ -1277,6 +1337,9 @@ abstract private[effect] class ResourceConcurrent[F[_]] | |
override def both[A, B](fa: Resource[F, A], fb: Resource[F, B]): Resource[F, (A, B)] = | ||
fa.both(fb) | ||
|
||
override def race[A, B](fa: Resource[F, A], fb: Resource[F, B]): Resource[F, Either[A, B]] = | ||
fa.race(fb) | ||
|
||
override def memoize[A](fa: Resource[F, A]): Resource[F, Resource[F, A]] = { | ||
Resource.eval(F.ref(false)).flatMap { allocated => | ||
val fa2 = F.uncancelable(poll => poll(fa.allocatedCase) <* allocated.set(true)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth it to describe loser release behaviour (eager and concurrent) in the scaladoc, similarly to how it's done for the
Resource#both
. 🙂