Skip to content

Consumer: assign / subscribe on run() #133

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

Merged
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
14 changes: 7 additions & 7 deletions Sources/Kafka/KafkaConsumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,6 @@ public final class KafkaConsumer: Sendable, Service {

// Forward main queue events to the consumer queue.
try client.pollSetConsumer()

switch configuration.consumptionStrategy._internal {
case .partition(topic: let topic, partition: let partition, offset: let offset):
try self.assign(topic: topic, partition: partition, offset: offset)
case .group(groupID: _, topics: let topics):
try self.subscribe(topics: topics)
}
}

/// Initialize a new ``KafkaConsumer``.
Expand Down Expand Up @@ -331,6 +324,13 @@ public final class KafkaConsumer: Sendable, Service {
}

private func _run() async throws {
switch self.configuration.consumptionStrategy._internal {
case .partition(topic: let topic, partition: let partition, offset: let offset):
try self.assign(topic: topic, partition: partition, offset: offset)
case .group(groupID: _, topics: let topics):
try self.subscribe(topics: topics)
}
Comment on lines +327 to +332
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably go through the state machine here

Copy link
Contributor Author

@felixschlegel felixschlegel Sep 21, 2023

Choose a reason for hiding this comment

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

func assign() and func subscribe() already go through the state machine by invoking stateMachine.setupConnection

I could however hoist the state check to func run() (so state check -> check consumption strategy -> assign / subscribe instead of check consumption strategy -> state check -> assign / subscribe)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah got you. Just to double check if somebody called run twice we wouldn't subscribe twice right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it only works when in state .initializing 👍


while !Task.isCancelled {
let nextAction = self.stateMachine.withLockedValue { $0.nextPollLoopAction() }
switch nextAction {
Expand Down