Skip to content
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

Use Semaphore #1142

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ lazy val zioKafka =
.settings(
libraryDependencies ++= Seq(
kafkaClients,
scalaCollectionCompat,
"dev.zio" %% "zio-concurrent" % zioVersion.value
scalaCollectionCompat
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import zio.kafka.consumer.ConsumerSettings
import zio.kafka.consumer.internal.ConsumerAccess.ByteArrayKafkaConsumer

import scala.jdk.CollectionConverters._
import zio.concurrent.ReentrantLock

private[consumer] final class ConsumerAccess(
private[consumer] val consumer: ByteArrayKafkaConsumer,
access: ReentrantLock
access: Semaphore
) {

def withConsumer[A](f: ByteArrayKafkaConsumer => A): Task[A] =
withConsumerZIO[Any, A](c => ZIO.attempt(f(c)))

def withConsumerZIO[R, A](f: ByteArrayKafkaConsumer => RIO[R, A]): RIO[R, A] =
access.lock.zipRight(withConsumerNoPermit(f)).ensuring(access.unlock)
access.withPermit(withConsumerNoPermit(f))

private def withConsumerNoPermit[R, A](
f: ByteArrayKafkaConsumer => RIO[R, A]
Expand All @@ -36,7 +35,7 @@ private[consumer] final class ConsumerAccess(
* Use this method only from Runloop.
*/
private[internal] def runloopAccess[R, E, A](f: ByteArrayKafkaConsumer => ZIO[R, E, A]): ZIO[R, E, A] =
access.lock.zipRight(f(consumer)).ensuring(access.unlock)
access.withPermit(f(consumer))

/**
* Use this method ONLY from the rebalance listener.
Expand Down Expand Up @@ -67,6 +66,6 @@ private[consumer] object ConsumerAccess {

def make(consumer: ByteArrayKafkaConsumer): ZIO[Scope, Throwable, ConsumerAccess] =
for {
access <- ReentrantLock.make(fairness = true)
access <- Semaphore.make(1)
} yield new ConsumerAccess(consumer, access)
}