Skip to content

KafkaProducer.makeProducerWithEvents() avoid fatalError #117

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
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
5 changes: 5 additions & 0 deletions Sources/Kafka/KafkaConsumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ public final class KafkaConsumer: Sendable, Service {
logger: logger
)

// Note:
// It's crucial to initialize the `sourceAndSequence` variable AFTER `client`.
// This order is important to prevent the accidental triggering of `KafkaConsumerCloseOnTerminate.didTerminate()`.
// If this order is not met and `RDKafkaClient.makeClient()` fails,
// it leads to a call to `stateMachine.messageSequenceTerminated()` while it's still in the `.uninitialized` state.
let sourceAndSequence = NIOAsyncSequenceProducer.makeSequence(
elementType: KafkaConsumerEvent.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
Expand Down
26 changes: 15 additions & 11 deletions Sources/Kafka/KafkaProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public final class KafkaProducer: Service, Sendable {
stateMachine: NIOLockedValueBox<KafkaProducer.StateMachine>,
configuration: KafkaProducerConfiguration,
topicConfiguration: KafkaTopicConfiguration
) throws {
) {
self.stateMachine = stateMachine
self.configuration = configuration
self.topicConfiguration = topicConfiguration
Expand Down Expand Up @@ -130,7 +130,7 @@ public final class KafkaProducer: Service, Sendable {
)
}

try self.init(
self.init(
stateMachine: stateMachine,
configuration: configuration,
topicConfiguration: configuration.topicConfiguration
Expand All @@ -156,30 +156,34 @@ public final class KafkaProducer: Service, Sendable {
) throws -> (KafkaProducer, KafkaProducerEvents) {
let stateMachine = NIOLockedValueBox(StateMachine(logger: logger))

let sourceAndSequence = NIOAsyncSequenceProducer.makeSequence(
elementType: KafkaProducerEvent.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
delegate: KafkaProducerCloseOnTerminate(stateMachine: stateMachine)
)
let source = sourceAndSequence.source

let client = try RDKafkaClient.makeClient(
type: .producer,
configDictionary: configuration.dictionary,
events: [.log, .deliveryReport],
logger: logger
)

let producer = try KafkaProducer(
let producer = KafkaProducer(
stateMachine: stateMachine,
configuration: configuration,
topicConfiguration: configuration.topicConfiguration
)

// Note:
// It's crucial to initialize the `sourceAndSequence` variable AFTER `client`.
// This order is important to prevent the accidental triggering of `KafkaProducerCloseOnTerminate.didTerminate()`.
// If this order is not met and `RDKafkaClient.makeClient()` fails,
// it leads to a call to `stateMachine.stopConsuming()` while it's still in the `.uninitialized` state.
let sourceAndSequence = NIOAsyncSequenceProducer.makeSequence(
elementType: KafkaProducerEvent.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
delegate: KafkaProducerCloseOnTerminate(stateMachine: stateMachine)
)

stateMachine.withLockedValue {
$0.initialize(
client: client,
source: source
source: sourceAndSequence.source
)
}

Expand Down