12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
- import Crdkafka
16
15
import Logging
17
16
import NIOConcurrencyHelpers
18
17
import NIOCore
@@ -136,7 +135,7 @@ public final class KafkaProducer: Service, Sendable {
136
135
}
137
136
}
138
137
139
- let client = try RDKafka . createClient (
138
+ let client = try RDKafkaClient . makeClient (
140
139
type: . producer,
141
140
configDictionary: config. dictionary,
142
141
events: [ . log] , // No .deliveryReport here!
@@ -189,7 +188,7 @@ public final class KafkaProducer: Service, Sendable {
189
188
)
190
189
let source = sourceAndSequence. source
191
190
192
- let client = try RDKafka . createClient (
191
+ let client = try RDKafkaClient . makeClient (
193
192
type: . producer,
194
193
configDictionary: config. dictionary,
195
194
events: [ . log, . deliveryReport] ,
@@ -268,9 +267,15 @@ public final class KafkaProducer: Service, Sendable {
268
267
if !flushing || events. isEmpty {
269
268
try await Task . sleep ( for: self . config. pollInterval)
270
269
}
271
- case . terminatePollLoopAndFinishSource( let source, let statisticsSource) :
270
+ case . flushFinishSourceAndTerminatePollLoop( let client, let source, let statisticsSource) :
271
+ precondition (
272
+ 0 ... Int ( Int32 . max) ~= self . config. flushTimeoutMilliseconds,
273
+ " Flush timeout outside of valid range \( 0 ... Int32 . max) "
274
+ )
275
+ try await client. flush ( timeoutMilliseconds: Int32 ( self . config. flushTimeoutMilliseconds) )
272
276
source? . finish ( )
273
277
statisticsSource? . finish ( )
278
+ statisticsSource? . finish ( )
274
279
return
275
280
case . terminatePollLoop:
276
281
return
@@ -329,7 +334,7 @@ extension KafkaProducer {
329
334
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
330
335
/// - Parameter topicHandles: Class containing all topic names with their respective `rd_kafka_topic_t` pointer.
331
336
case started(
332
- client: KafkaClient ,
337
+ client: RDKafkaClient ,
333
338
messageIDCounter: UInt ,
334
339
source: Producer . Source ? ,
335
340
statisticsSource: StatisticsProducer . Source ? ,
@@ -340,8 +345,8 @@ extension KafkaProducer {
340
345
///
341
346
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
342
347
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
343
- case flushing (
344
- client: KafkaClient ,
348
+ case finishing (
349
+ client: RDKafkaClient ,
345
350
source: Producer . Source ? ,
346
351
statisticsSource: StatisticsProducer . Source ?
347
352
)
@@ -355,7 +360,7 @@ extension KafkaProducer {
355
360
/// Delayed initialization of `StateMachine` as the `source` is not yet available
356
361
/// when the normal initialization occurs.
357
362
mutating func initialize(
358
- client: KafkaClient ,
363
+ client: RDKafkaClient ,
359
364
source: Producer . Source ? ,
360
365
statisticsSource: StatisticsProducer . Source ?
361
366
) {
@@ -377,11 +382,12 @@ extension KafkaProducer {
377
382
///
378
383
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
379
384
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
380
- case pollAndYield( client: KafkaClient , source: Producer . Source ? , statisticsSource: StatisticsProducer . Source ? , flushing: Bool = false )
385
+ case pollAndYield( client: RDKafkaClient , source: Producer . Source ? , statisticsSource: StatisticsProducer . Source ? , flushing: Bool = false )
381
386
/// Terminate the poll loop and finish the given `NIOAsyncSequenceProducerSource`.
382
387
///
388
+ /// - Parameter client: Client used for handling the connection to the Kafka cluster.
383
389
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
384
- case terminatePollLoopAndFinishSource ( source: Producer . Source ? , statisticsSource: StatisticsProducer . Source ? )
390
+ case flushFinishSourceAndTerminatePollLoop ( client : RDKafkaClient , source: Producer . Source ? , statisticsSource: StatisticsProducer . Source ? )
385
391
/// Terminate the poll loop.
386
392
case terminatePollLoop
387
393
}
@@ -396,13 +402,8 @@ extension KafkaProducer {
396
402
fatalError ( " \( #function) invoked while still in state \( self . state) " )
397
403
case . started( let client, _, let source, let statisticsSource, _) :
398
404
return . pollAndYield( client: client, source: source, statisticsSource: statisticsSource)
399
- case . flushing( let client, let source, let statisticsSource) :
400
- if client. outgoingQueueSize > 0 {
401
- return . pollAndYield( client: client, source: source, statisticsSource: statisticsSource, flushing: true )
402
- } else {
403
- self . state = . finished
404
- return . terminatePollLoopAndFinishSource( source: source, statisticsSource: statisticsSource)
405
- }
405
+ case . finishing( let client, let source, let statisticsSource) :
406
+ return . flushFinishSourceAndTerminatePollLoop( client: client, source: source, statisticsSource: statisticsSource)
406
407
case . finished:
407
408
return . terminatePollLoop
408
409
}
@@ -414,7 +415,7 @@ extension KafkaProducer {
414
415
///
415
416
/// - Important: `newMessageID` is the new message ID assigned to the message to be sent.
416
417
case send(
417
- client: KafkaClient ,
418
+ client: RDKafkaClient ,
418
419
newMessageID: UInt ,
419
420
topicHandles: RDKafkaTopicHandles
420
421
)
@@ -444,8 +445,8 @@ extension KafkaProducer {
444
445
newMessageID: newMessageID,
445
446
topicHandles: topicHandles
446
447
)
447
- case . flushing :
448
- throw KafkaError . connectionClosed ( reason: " Producer in the process of flushing and shutting down " )
448
+ case . finishing :
449
+ throw KafkaError . connectionClosed ( reason: " Producer in the process of finishing " )
449
450
case . finished:
450
451
throw KafkaError . connectionClosed ( reason: " Tried to produce a message with a closed producer " )
451
452
}
@@ -479,9 +480,9 @@ extension KafkaProducer {
479
480
}
480
481
self . state = . started( client: client, messageIDCounter: counter, source: nil , statisticsSource: statisticsSource, topicHandles: topicHandlers)
481
482
return . finishSource( source: source)
482
- case . flushing ( let client, let source, let statisticsSource) :
483
+ case . finishing ( let client, let source, let statisticsSource) :
483
484
// Setting source to nil to prevent incoming acknowledgements from buffering in `source`
484
- self . state = . flushing ( client: client, source: nil , statisticsSource: statisticsSource)
485
+ self . state = . finishing ( client: client, source: nil , statisticsSource: statisticsSource)
485
486
return . finishSource( source: source)
486
487
case . finished:
487
488
break
@@ -498,13 +499,10 @@ extension KafkaProducer {
498
499
fatalError ( " stopStatistics() must not be invoked more than once " )
499
500
}
500
501
self . state = . started( client: client, messageIDCounter: counter, source: source, statisticsSource: nil , topicHandles: topicHandlers)
501
- client. withKafkaHandlePointer { kafkaHandle in
502
- rd_kafka_conf_set_stats_cb ( < #T##conf: OpaquePointer! ##OpaquePointer! #> , < #T##stats_cb: ( ( OpaquePointer? , UnsafeMutablePointer < CChar > ? , Int, UnsafeMutableRawPointer? ) - > Int32 ) !##( ( OpaquePointer? , UnsafeMutablePointer < CChar > ? , Int, UnsafeMutableRawPointer? ) - > Int32 ) !##( OpaquePointer? , UnsafeMutablePointer < CChar > ? , Int, UnsafeMutableRawPointer? ) - > Int32 #> )
503
- }
504
502
return . finishStatisticsSource( statisticsSource: statisticsSource)
505
- case . flushing ( let client, let source, let statisticsSource) :
503
+ case . finishing ( let client, let source, let statisticsSource) :
506
504
// Setting source to nil to prevent incoming acknowledgements from buffering in `source`
507
- self . state = . flushing ( client: client, source: source, statisticsSource: nil )
505
+ self . state = . finishing ( client: client, source: source, statisticsSource: nil )
508
506
return . finishStatisticsSource( statisticsSource: statisticsSource)
509
507
case . finished:
510
508
break
@@ -520,8 +518,8 @@ extension KafkaProducer {
520
518
case . uninitialized:
521
519
fatalError ( " \( #function) invoked while still in state \( self . state) " )
522
520
case . started( let client, _, let source, let statisticsSource, _) :
523
- self . state = . flushing ( client: client, source: source, statisticsSource: statisticsSource)
524
- case . flushing , . finished:
521
+ self . state = . finishing ( client: client, source: source, statisticsSource: statisticsSource)
522
+ case . finishing , . finished:
525
523
break
526
524
}
527
525
}
0 commit comments