-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KafkaProducer
: events- instead of acks sequence (#96)
* `KafkaProducer`: event instead of acks sequence Motivation: `KafkaProducer`: we want to expose a general `KafkaProducerEvent` type in the `AsyncSequence` that was formerly just for message acknowledgements. This enables us to add more events such as statistics in the future. The reason behind having a single `AsyncSequence` for all event types instead of having separate `AsyncSequence`s for each event type is that we need to ensure that all events that we subscribe to in `librdkafka` are actually consumed by the `AsyncSequence` in our `KafkaProducer` type. Otherwise we could run out of memory. Now by giving the user the entire events `AsyncSequence`, they decide if they want to consume event or drop it. > **Note**: Logs will be consumed regardless of the > `KafkaProducerEvents` `AsyncSequence` Modifications: * create a new `enum` `KafkaProducerEvent` * rename `KafkaMessageAcknowledgements` -> `KafkaProducerEvents` * rename `KafkaProducer.makeProducerWithAcknowledgements` -> `.makeProducerWithEvents` * update tests * update README * Review Franz Modifications: * fix documentation typos * `KafkaProducerEvent`: replace factory method with `init` * create new type `KafkaProducerMessageStatus` effectively representing if a `KafkaProducerMessage` was acknowledged or not * fix README nit * `KafkaDeliveryReport` type Modifications: * create new type `KafkaDeliveryReport` containing a message's status and its id * remove ID from `KafkaAcknowledgedMessage` * remove `KafkaAcknowledgedMessageError` * rename `KafkaProducerEvent.deliverReport` -> `.deliverReports` * update README * update tests * Fix documentation nits * Review Gus Modifications: * fix typos * rename `KafkaProducerEvents.KafkaProducerEventAsyncIterator` -> `KafkaProducerEvents.AsyncIterator` * Fix typo * Fix (letter) case nit Co-authored-by: Franz Busch <[email protected]> --------- Co-authored-by: Franz Busch <[email protected]>
- Loading branch information
1 parent
5b07fe2
commit f8cb0a0
Showing
11 changed files
with
239 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the swift-kafka-gsoc open source project | ||
// | ||
// Copyright (c) 2022 Apple Inc. and the swift-kafka-gsoc project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of swift-kafka-gsoc project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Crdkafka | ||
|
||
/// A delivery report for a message that was sent to the Kafka cluster. | ||
public struct KafkaDeliveryReport: Sendable, Hashable { | ||
public enum Status: Sendable, Hashable { | ||
/// The message has been successfully acknowledged by the Kafka cluster. | ||
case acknowledged(message: KafkaAcknowledgedMessage) | ||
/// The message failed to be acknowledged by the Kafka cluster and encountered an error. | ||
case failure(KafkaError) | ||
} | ||
|
||
/// The status of a Kafka producer message after attempting to send it. | ||
public var status: Status | ||
|
||
/// The unique identifier assigned by the ``KafkaProducer`` when the message was sent to Kafka. | ||
/// The same identifier is returned by ``KafkaProducer/send(_:)`` and can be used to correlate | ||
/// a sent message with a delivery report. | ||
public var id: KafkaProducerMessageID | ||
|
||
internal init?(messagePointer: UnsafePointer<rd_kafka_message_t>?) { | ||
guard let messagePointer else { | ||
return nil | ||
} | ||
|
||
self.id = KafkaProducerMessageID(rawValue: UInt(bitPattern: messagePointer.pointee._private)) | ||
|
||
do { | ||
let message = try KafkaAcknowledgedMessage(messagePointer: messagePointer) | ||
self.status = .acknowledged(message: message) | ||
} catch { | ||
guard let error = error as? KafkaError else { | ||
fatalError("Caught error that is not of type \(KafkaError.self)") | ||
} | ||
self.status = .failure(error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.