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

KafkaConsumer: back pressure + improved read speed #139

Merged
merged 5 commits into from
Nov 1, 2023
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
33 changes: 32 additions & 1 deletion Sources/Kafka/Configuration/KafkaConsumerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@ public struct KafkaConsumerConfiguration {
/// Default: `.milliseconds(100)`
public var pollInterval: Duration = .milliseconds(100)

/// A struct representing different back pressure strategies for consuming messages in ``KafkaConsumer``.
public struct BackPressureStrategy: Sendable, Hashable {
enum _BackPressureStrategy: Sendable, Hashable {
case watermark(low: Int, high: Int)
}

let _internal: _BackPressureStrategy

private init(backPressureStrategy: _BackPressureStrategy) {
self._internal = backPressureStrategy
}

/// A back pressure strategy based on high and low watermarks.
///
/// The consumer maintains a buffer size between a low watermark and a high watermark
/// to control the flow of incoming messages.
///
/// - Parameter low: The lower threshold for the buffer size (low watermark).
/// - Parameter high: The upper threshold for the buffer size (high watermark).
public static func watermark(low: Int, high: Int) -> BackPressureStrategy {
return .init(backPressureStrategy: .watermark(low: low, high: high))
}
}

/// The backpressure strategy to be used for message consumption.
/// See ``KafkaConsumerConfiguration/BackPressureStrategy-swift.struct`` for more information.
public var backPressureStrategy: BackPressureStrategy = .watermark(
low: 10,
high: 50
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you happy with these default values?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these values should work good for a wide range of cases

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine by me as well. Kafka is all around people tuning the settings to fit their messages.

)

/// A struct representing the different Kafka message consumption strategies.
public struct ConsumptionStrategy: Sendable, Hashable {
enum _ConsumptionStrategy: Sendable, Hashable {
Expand Down Expand Up @@ -63,7 +94,7 @@ public struct KafkaConsumerConfiguration {
}

/// The strategy used for consuming messages.
/// See ``KafkaConfiguration/ConsumptionStrategy`` for more information.
/// See ``KafkaConsumerConfiguration/ConsumptionStrategy-swift.struct-swift.struct`` for more information.
public var consumptionStrategy: ConsumptionStrategy

// MARK: - Consumer-specific Config Properties
Expand Down
Loading