@@ -57,7 +57,7 @@ public struct KafkaConsumerConfiguration {
57
57
/// A struct representing the different Kafka message consumption strategies.
58
58
public struct ConsumptionStrategy : Sendable , Hashable {
59
59
enum _ConsumptionStrategy : Sendable , Hashable {
60
- case partition( topic: String , partition: KafkaPartition , offset: KafkaOffset )
60
+ case partition( groupID : String ? = nil , topic: String , partition: KafkaPartition , offset: KafkaOffset )
61
61
case group( groupID: String , topics: [ String ] )
62
62
}
63
63
@@ -82,6 +82,23 @@ public struct KafkaConsumerConfiguration {
82
82
return . init( consumptionStrategy: . partition( topic: topic, partition: partition, offset: offset) )
83
83
}
84
84
85
+ /// A consumption strategy based on partition assignment.
86
+ /// The consumer reads from a specific partition of a topic at a given offset.
87
+ ///
88
+ /// - Parameters:
89
+ /// - partition: The partition of the topic to consume from.
90
+ /// - group: The ID of the consumer group to commit to.
91
+ /// - topic: The name of the Kafka topic.
92
+ /// - offset: The offset to start consuming from. Defaults to the end of the Kafka partition queue (meaning wait for the next produced message).
93
+ public static func partition(
94
+ _ partition: KafkaPartition ,
95
+ groupID: String ,
96
+ topic: String ,
97
+ offset: KafkaOffset = . end
98
+ ) -> ConsumptionStrategy {
99
+ return . init( consumptionStrategy: . partition( groupID: groupID, topic: topic, partition: partition, offset: offset) )
100
+ }
101
+
85
102
/// A consumption strategy based on consumer group membership.
86
103
/// The consumer joins a consumer group identified by a group ID and consumes from multiple topics.
87
104
///
@@ -261,12 +278,17 @@ extension KafkaConsumerConfiguration {
261
278
var resultDict : [ String : String ] = [ : ]
262
279
263
280
switch self . consumptionStrategy. _internal {
264
- case . partition:
265
- // Although an assignment is not related to a consumer group,
266
- // librdkafka requires us to set a `group.id`.
267
- // This is a known issue:
268
- // https://github.com/edenhill/librdkafka/issues/3261
269
- resultDict [ " group.id " ] = UUID ( ) . uuidString
281
+ case . partition( groupID: let groupID, topic: _, partition: _, offset: _) :
282
+ if let groupID = groupID {
283
+ resultDict [ " group.id " ] = groupID
284
+ } else {
285
+ // Although an assignment is not related to a consumer group,
286
+ // librdkafka requires us to set a `group.id`.
287
+ // This is a known issue:
288
+ // https://github.com/edenhill/librdkafka/issues/3261
289
+ resultDict [ " group.id " ] = UUID ( ) . uuidString
290
+ }
291
+
270
292
case . group( groupID: let groupID, topics: _) :
271
293
resultDict [ " group.id " ] = groupID
272
294
}
0 commit comments