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

Destroy the preload consumer when complete in loadAndRun #135

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
24 changes: 18 additions & 6 deletions src/main/scala/uk/sky/fs2/kafka/topicloader/TopicLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ trait TopicLoader {
topics: NonEmptyList[String],
consumerSettings: ConsumerSettings[F, K, V]
)(onLoad: Resource.ExitCase => F[Unit]): Stream[F, ConsumerRecord[K, V]] = {
def preLoad(
logOffsets: NonEmptyMap[TopicPartition, LogOffsets]
)(using Logger[F]): Stream[F, ConsumerRecord[K, V]] = for {
preloadConsumer <- KafkaConsumer.stream(consumerSettings)
record <- load(logOffsets, preloadConsumer).onFinalizeCase(onLoad)
} yield record

def postLoad(
logOffsets: NonEmptyMap[TopicPartition, LogOffsets]
)(using Logger[F]): Stream[F, ConsumerRecord[K, V]] =
Expand All @@ -84,10 +91,14 @@ trait TopicLoader {

for {
given Logger[F] <- Stream.eval(LoggerFactory[F].create)
preloadConsumer <- KafkaConsumer.stream(consumerSettings)
logOffsets <- Stream.eval(logOffsetsForTopics(topics, LoadAll, preloadConsumer)).flatMap(Stream.fromOption(_))
maybeLogOffsets <- Stream
.eval(
// Destroy the consumer after calculating the offsets
KafkaConsumer.resource(consumerSettings).use(logOffsetsForTopics(topics, LoadAll, _))
)
logOffsets <- Stream.fromOption(maybeLogOffsets)
_ <- Stream.eval(info"log offsets: ${logOffsets.show}")
record <- load(logOffsets, preloadConsumer).onFinalizeCase(onLoad) ++ postLoad(logOffsets)
record <- preLoad(logOffsets) ++ postLoad(logOffsets)
} yield record
}

Expand All @@ -97,9 +108,10 @@ trait TopicLoader {
consumer: KafkaConsumer[F, K, V]
): Stream[F, ConsumerRecord[K, V]] =
for {
logOffsets <- Stream.eval(logOffsetsForTopics(topics, strategy, consumer)).flatMap(Stream.fromOption(_))
_ <- Stream.eval(info"log offsets: ${logOffsets.show}")
record <- load(logOffsets, consumer)
maybeLogOffsets <- Stream.eval(logOffsetsForTopics(topics, strategy, consumer))
logOffsets <- Stream.fromOption(maybeLogOffsets)
_ <- Stream.eval(info"log offsets: ${logOffsets.show}")
record <- load(logOffsets, consumer)
} yield record

private def load[F[_] : Async : Logger, K, V](
Expand Down