@@ -259,42 +259,10 @@ public final class KafkaProducer: Service, Sendable {
259259 return KafkaProducerMessageID ( rawValue: newMessageID)
260260 }
261261 }
262-
263- func initTransactions( timeout: Duration = . seconds( 5 ) ) async throws {
264- guard config. transactionalId != nil else {
265- throw KafkaError . config (
266- reason: " Could not initialize transactions because transactionalId is not set in config " )
267- }
268- let client = try self . stateMachine. withLockedValue { try $0. initTransactions ( ) }
269- try await client. initTransactions ( timeout: timeout)
270- }
271-
272- func beginTransaction( ) throws {
273- let client = try self . stateMachine. withLockedValue { try $0. transactionsClient ( ) }
274- try client. beginTransaction ( )
275- }
276-
277- func send(
278- offsets: RDKafkaTopicPartitionList ,
279- forConsumer consumer: KafkaConsumer ,
280- timeout: Duration = . kafkaUntilEndOfTransactionTimeout,
281- attempts: UInt64 = . max
282- ) async throws {
283- let client = try self . stateMachine. withLockedValue { try $0. transactionsClient ( ) }
284- let consumerClient = try consumer. client ( )
285- try await consumerClient. withKafkaHandlePointer {
286- try await client. send ( attempts: attempts, offsets: offsets, forConsumerKafkaHandle: $0, timeout: timeout)
287- }
288- }
289-
290- func abortTransaction(
291- timeout: Duration = . kafkaUntilEndOfTransactionTimeout,
292- attempts: UInt64 ) async throws {
293- let client = try self . stateMachine. withLockedValue { try $0. transactionsClient ( ) }
294- try await client. abortTransaction ( attempts: attempts, timeout: timeout)
262+
263+ func client( ) throws -> RDKafkaClient {
264+ try self . stateMachine. withLockedValue { try $0. client ( ) }
295265 }
296-
297-
298266}
299267
300268// MARK: - KafkaProducer + StateMachine
@@ -322,18 +290,6 @@ extension KafkaProducer {
322290 source: Producer . Source ? ,
323291 topicHandles: RDKafkaTopicHandles
324292 )
325- /// The ``KafkaProducer`` has started and is ready to use, transactions were initialized.
326- ///
327- /// - Parameter messageIDCounter:Used to incrementally assign unique IDs to messages.
328- /// - Parameter client: Client used for handling the connection to the Kafka cluster.
329- /// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
330- /// - Parameter topicHandles: Class containing all topic names with their respective `rd_kafka_topic_t` pointer.
331- case startedWithTransactions(
332- client: RDKafkaClient ,
333- messageIDCounter: UInt ,
334- source: Producer . Source ? ,
335- topicHandles: RDKafkaTopicHandles
336- )
337293 /// Producer is still running but the event asynchronous sequence was terminated.
338294 /// All incoming events will be dropped.
339295 ///
@@ -401,7 +357,7 @@ extension KafkaProducer {
401357 switch self . state {
402358 case . uninitialized:
403359 fatalError ( " \( #function) invoked while still in state \( self . state) " )
404- case . started( let client, _, let source, _) , . startedWithTransactions ( let client , _ , let source , _ ) :
360+ case . started( let client, _, let source, _) :
405361 return . pollAndYield( client: client, source: source)
406362 case . consumptionStopped( let client) :
407363 return . pollWithoutYield( client: client)
@@ -444,19 +400,6 @@ extension KafkaProducer {
444400 newMessageID: newMessageID,
445401 topicHandles: topicHandles
446402 )
447- case . startedWithTransactions( let client, let messageIDCounter, let source, let topicHandles) :
448- let newMessageID = messageIDCounter + 1
449- self . state = . startedWithTransactions(
450- client: client,
451- messageIDCounter: newMessageID,
452- source: source,
453- topicHandles: topicHandles
454- )
455- return . send(
456- client: client,
457- newMessageID: newMessageID,
458- topicHandles: topicHandles
459- )
460403 case . consumptionStopped:
461404 throw KafkaError . connectionClosed ( reason: " Sequence consuming events was abruptly terminated, producer closed " )
462405 case . finishing:
@@ -482,7 +425,7 @@ extension KafkaProducer {
482425 fatalError ( " \( #function) invoked while still in state \( self . state) " )
483426 case . consumptionStopped:
484427 fatalError ( " messageSequenceTerminated() must not be invoked more than once " )
485- case . started( let client, _, let source, _) , . startedWithTransactions ( let client , _ , let source , _ ) :
428+ case . started( let client, _, let source, _) :
486429 self . state = . consumptionStopped( client: client)
487430 return . finishSource( source: source)
488431 case . finishing( let client, let source) :
@@ -502,34 +445,28 @@ extension KafkaProducer {
502445 switch self . state {
503446 case . uninitialized:
504447 fatalError ( " \( #function) invoked while still in state \( self . state) " )
505- case . started( let client, _, let source, _) , . startedWithTransactions ( let client , _ , let source , _ ) :
448+ case . started( let client, _, let source, _) :
506449 self . state = . finishing( client: client, source: source)
507450 case . consumptionStopped( let client) :
508451 self . state = . finishing( client: client, source: nil )
509452 case . finishing, . finished:
510453 break
511454 }
512455 }
513-
514- mutating func initTransactions ( ) throws -> RDKafkaClient {
456+
457+ func client ( ) throws -> RDKafkaClient {
515458 switch self . state {
516459 case . uninitialized:
517460 fatalError ( " \( #function) invoked while still in state \( self . state) " )
518- case . started( let client, let messageIDCounter, let source, let topicHandles) :
519- self . state = . startedWithTransactions( client: client, messageIDCounter: messageIDCounter, source: source, topicHandles: topicHandles)
461+ case . started( let client, _, _, _) :
520462 return client
521- case . startedWithTransactions:
522- throw KafkaError . config ( reason: " Transactions were already initialized " )
523- case . consumptionStopped, . finishing, . finished:
524- throw KafkaError . connectionClosed ( reason: " Producer is stopping or finished " )
525- }
526- }
527-
528- func transactionsClient( ) throws -> RDKafkaClient {
529- guard case let . startedWithTransactions( client, _, _, _) = self . state else {
530- throw KafkaError . transactionAborted ( reason: " Transactions were not initialized or producer is being stopped " )
463+ case . consumptionStopped( let client) :
464+ return client
465+ case . finishing( let client, _) :
466+ return client
467+ case . finished:
468+ throw KafkaError . connectionClosed ( reason: " Client stopped " )
531469 }
532- return client
533470 }
534471 }
535472}
0 commit comments