Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions core/shared/src/main/scala/zio/Queue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,22 @@ object Queue extends QueuePlatformSpecific {
shutdownHook: Promise[Nothing, Unit],
shutdownFlag: AtomicBoolean,
strategy: Strategy[A]
): Queue[A] = new Queue[A] {
): Queue[A] = new QueueImpl[A](queue, takers, shutdownHook, shutdownFlag, strategy)

private final class QueueImpl[A](
queue: MutableConcurrentQueue[A],
takers: ConcurrentDeque[Promise[Nothing, A]],
shutdownHook: Promise[Nothing, Unit],
shutdownFlag: AtomicBoolean,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can merge the Atomic with the Queue to avoid another field.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I'm not sure to understand your comment 🤔

strategy: Strategy[A]
) extends Queue[A] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we need to make this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better stack traces. That's a kind of change we already made in some other places with Kyri


private def removeTaker(taker: Promise[Nothing, A])(implicit trace: Trace): UIO[Unit] =
ZIO.succeed(takers.remove(taker))

val capacity: Int = queue.capacity
override def capacity: Int = queue.capacity
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queue.capacity is already a val. We don't need a val here too


def offer(a: A)(implicit trace: Trace): UIO[Boolean] =
override def offer(a: A)(implicit trace: Trace): UIO[Boolean] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind it either way, but is there a reason other than readability for adding the override keyword?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer leaving it off - if someone adds a default implementation, they should get a compiler error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer adding them. Makes it more explicit. If you differ from the super class or the super has a default value, you get a/the right error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, explicitness > implicitness. So yeah it's about readability. So I don't have to guess or to remember that it's overriding something

ZIO.suspendSucceed {
if (shutdownFlag.get) ZIO.interrupt
else {
Expand Down Expand Up @@ -186,7 +194,7 @@ object Queue extends QueuePlatformSpecific {
}
}

def offerAll[A1 <: A](as: Iterable[A1])(implicit trace: Trace): UIO[Chunk[A1]] =
override def offerAll[A1 <: A](as: Iterable[A1])(implicit trace: Trace): UIO[Chunk[A1]] =
ZIO.suspendSucceed {
if (shutdownFlag.get) ZIO.interrupt
else {
Expand All @@ -212,17 +220,17 @@ object Queue extends QueuePlatformSpecific {
}
}

def awaitShutdown(implicit trace: Trace): UIO[Unit] = shutdownHook.await
override def awaitShutdown(implicit trace: Trace): UIO[Unit] = shutdownHook.await

def size(implicit trace: Trace): UIO[Int] =
override def size(implicit trace: Trace): UIO[Int] =
ZIO.suspendSucceed {
if (shutdownFlag.get)
ZIO.interrupt
else
Exit.succeed(queue.size() - takers.size() + strategy.surplusSize)
}

def shutdown(implicit trace: Trace): UIO[Unit] =
override def shutdown(implicit trace: Trace): UIO[Unit] =
ZIO.fiberIdWith { fiberId =>
shutdownFlag.set(true)

Expand All @@ -231,9 +239,9 @@ object Queue extends QueuePlatformSpecific {
)
}.uninterruptible

def isShutdown(implicit trace: Trace): UIO[Boolean] = ZIO.succeed(shutdownFlag.get)
override def isShutdown(implicit trace: Trace): UIO[Boolean] = ZIO.succeed(shutdownFlag.get)

def take(implicit trace: Trace): UIO[A] =
override def take(implicit trace: Trace): UIO[A] =
ZIO.fiberIdWith { fiberId =>
if (shutdownFlag.get) ZIO.interrupt
else {
Expand All @@ -258,7 +266,7 @@ object Queue extends QueuePlatformSpecific {
}
}

def takeAll(implicit trace: Trace): UIO[Chunk[A]] =
override def takeAll(implicit trace: Trace): UIO[Chunk[A]] =
ZIO.suspendSucceed {
if (shutdownFlag.get)
ZIO.interrupt
Expand All @@ -273,7 +281,7 @@ object Queue extends QueuePlatformSpecific {
}
}

def takeUpTo(max: Int)(implicit trace: Trace): UIO[Chunk[A]] =
override def takeUpTo(max: Int)(implicit trace: Trace): UIO[Chunk[A]] =
ZIO.suspendSucceed {
if (shutdownFlag.get)
ZIO.interrupt
Expand Down
Loading