-
Couldn't load subscription status.
- Fork 1.4k
[9390] - Add tryAcquire to Semaphore #9600
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
Changes from all commits
696f3b5
9942122
cae94b3
ddcf2d1
6196ea6
3aae3b5
11217c8
3d1c5a7
9ddfc6d
a97d877
45decbb
d1ef09b
5a8de0e
d96e32c
ae5fce9
c64488f
2f0ef9c
74a8287
dbf1dba
3588e10
1211fbf
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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -45,6 +45,20 @@ sealed trait Semaphore extends Serializable { | |||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| def awaiting(implicit trace: Trace): UIO[Long] = ZIO.succeed(0L) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Executes the effect, acquiring a permit if available and releasing it after | ||||||||||||||||||||||||||||||||||
| * execution. Returns `None` if no permits were available. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| final def tryWithPermit[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]] = | ||||||||||||||||||||||||||||||||||
| tryWithPermits(1L)(zio) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Executes the effect, acquiring `n` permits if available and releasing them | ||||||||||||||||||||||||||||||||||
| * after execution. Returns `None` if no permits were available. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| def tryWithPermits[R, E, A](n: Long)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]] = | ||||||||||||||||||||||||||||||||||
| ZIO.none | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Executes the specified workflow, acquiring a permit immediately before the | ||||||||||||||||||||||||||||||||||
| * workflow begins execution and releasing it immediately after the workflow | ||||||||||||||||||||||||||||||||||
|
|
@@ -71,6 +85,7 @@ sealed trait Semaphore extends Serializable { | |||||||||||||||||||||||||||||||||
| * permits and releasing them when the scope is closed. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| def withPermitsScoped(n: Long)(implicit trace: Trace): ZIO[Scope, Nothing, Unit] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| object Semaphore { | ||||||||||||||||||||||||||||||||||
|
|
@@ -110,13 +125,35 @@ object Semaphore { | |||||||||||||||||||||||||||||||||
| def withPermitsScoped(n: Long)(implicit trace: Trace): ZIO[Scope, Nothing, Unit] = | ||||||||||||||||||||||||||||||||||
| ZIO.acquireRelease(reserve(n))(_.release).flatMap(_.acquire) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| override def tryWithPermits[R, E, A](n: Long)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, Option[A]] = | ||||||||||||||||||||||||||||||||||
| ZIO.acquireReleaseWith(tryReserve(n)) { | ||||||||||||||||||||||||||||||||||
| case Some(reservation) => reservation.release | ||||||||||||||||||||||||||||||||||
| case _ => Exit.unit | ||||||||||||||||||||||||||||||||||
| } { | ||||||||||||||||||||||||||||||||||
| case _: Some[?] => zio.asSome | ||||||||||||||||||||||||||||||||||
| case _ => Exit.none | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| case class Reservation(acquire: UIO[Unit], release: UIO[Any]) | ||||||||||||||||||||||||||||||||||
| object Reservation { | ||||||||||||||||||||||||||||||||||
| private[zio] val zero = Reservation(ZIO.unit, ZIO.unit) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def tryReserve(n: Long)(implicit trace: Trace): UIO[Option[Reservation]] = | ||||||||||||||||||||||||||||||||||
|
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 just realized this method doesn't need to return a Can it just return 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.
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. zio/core/shared/src/main/scala/zio/Semaphore.scala Lines 152 to 167 in c64488f
|
||||||||||||||||||||||||||||||||||
| if (n < 0) ZIO.die(new IllegalArgumentException(s"Unexpected negative `$n` permits requested.")) | ||||||||||||||||||||||||||||||||||
| else if (n == 0L) ZIO.succeed(Some(Reservation.zero)) | ||||||||||||||||||||||||||||||||||
IgorDorokhov marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||
| ref.modify { | ||||||||||||||||||||||||||||||||||
| case Right(permits) if permits >= n => | ||||||||||||||||||||||||||||||||||
| Some(Reservation(ZIO.unit, releaseN(n))) -> Right(permits - n) | ||||||||||||||||||||||||||||||||||
| case other => None -> other | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def reserve(n: Long)(implicit trace: Trace): UIO[Reservation] = | ||||||||||||||||||||||||||||||||||
| if (n < 0) | ||||||||||||||||||||||||||||||||||
| ZIO.die(new IllegalArgumentException(s"Unexpected negative `$n` permits requested.")) | ||||||||||||||||||||||||||||||||||
| else if (n == 0L) | ||||||||||||||||||||||||||||||||||
| ZIO.succeedNow(Reservation(ZIO.unit, ZIO.unit)) | ||||||||||||||||||||||||||||||||||
| ZIO.succeed(Reservation.zero) | ||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||
| Promise.make[Nothing, Unit].flatMap { promise => | ||||||||||||||||||||||||||||||||||
| ref.modify { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.