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

Make access to the java consumer fair #1109

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ 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: Semaphore
access: ReentrantLock
) {

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.withPermit(withConsumerNoPermit(f))
access.lock *> withConsumerNoPermit(f) <* access.unlock
flavienbert marked this conversation as resolved.
Show resolved Hide resolved

private[consumer] def withConsumerNoPermit[R, A](
f: ByteArrayKafkaConsumer => RIO[R, A]
Expand All @@ -34,7 +36,7 @@ private[consumer] final class ConsumerAccess(
* Do not use this method outside of the Runloop
*/
private[internal] def runloopAccess[R, E, A](f: ByteArrayKafkaConsumer => ZIO[R, E, A]): ZIO[R, E, A] =
access.withPermit(f(consumer))
access.lock *> f(consumer) <* access.unlock
}

private[consumer] object ConsumerAccess {
Expand All @@ -58,6 +60,6 @@ private[consumer] object ConsumerAccess {

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