-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathKafkaConsumer.swift
693 lines (630 loc) · 29 KB
/
KafkaConsumer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-kafka-client open source project
//
// Copyright (c) 2022 Apple Inc. and the swift-kafka-client project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-kafka-client project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
import NIOConcurrencyHelpers
import NIOCore
import ServiceLifecycle
// MARK: - KafkaConsumerCloseOnTerminate
/// `NIOAsyncSequenceProducerDelegate` that terminates the closes the producer when
/// `didTerminate()` is invoked.
internal struct KafkaConsumerCloseOnTerminate: Sendable {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
}
extension KafkaConsumerCloseOnTerminate: NIOAsyncSequenceProducerDelegate {
func produceMore() {
return // No back pressure
}
func didTerminate() {
self.stateMachine.withLockedValue { $0.messageSequenceTerminated() }
}
}
// MARK: - KafkaConsumerEvents
/// `AsyncSequence` implementation for handling ``KafkaConsumerEvent``s emitted by Kafka.
public struct KafkaConsumerEvents: Sendable, AsyncSequence {
public typealias Element = KafkaConsumerEvent
typealias BackPressureStrategy = NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure
typealias WrappedSequence = NIOAsyncSequenceProducer<Element, BackPressureStrategy, KafkaConsumerCloseOnTerminate>
let wrappedSequence: WrappedSequence
/// `AsynceIteratorProtocol` implementation for handling ``KafkaConsumerEvent``s emitted by Kafka.
public struct AsyncIterator: AsyncIteratorProtocol {
var wrappedIterator: WrappedSequence.AsyncIterator
public mutating func next() async -> Element? {
await self.wrappedIterator.next()
}
}
public func makeAsyncIterator() -> AsyncIterator {
return AsyncIterator(wrappedIterator: self.wrappedSequence.makeAsyncIterator())
}
}
// MARK: - KafkaConsumerMessages
/// `AsyncSequence` implementation for handling messages received from the Kafka cluster (``KafkaConsumerMessage``).
public struct KafkaConsumerMessages: Sendable, AsyncSequence {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
public typealias Element = KafkaConsumerMessage
typealias BackPressureStrategy = NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure
typealias WrappedSequence = NIOThrowingAsyncSequenceProducer<
Result<KafkaConsumerMessage, Error>,
Error,
BackPressureStrategy,
KafkaConsumerCloseOnTerminate
>
let wrappedSequence: WrappedSequence
/// `AsynceIteratorProtocol` implementation for handling messages received from the Kafka cluster (``KafkaConsumerMessage``).
public struct AsyncIterator: AsyncIteratorProtocol {
let stateMachine: NIOLockedValueBox<KafkaConsumer.StateMachine>
var wrappedIterator: WrappedSequence.AsyncIterator?
public mutating func next() async throws -> Element? {
guard let result = try await self.wrappedIterator?.next() else {
self.deallocateIterator()
return nil
}
switch result {
case .success(let message):
let action = self.stateMachine.withLockedValue { $0.storeOffset() }
switch action {
case .storeOffset(let client):
do {
try client.storeMessageOffset(message)
} catch {
self.deallocateIterator()
throw error
}
return message
case .terminateConsumerSequence:
self.deallocateIterator()
return nil
}
case .failure(let error):
self.deallocateIterator()
throw error
}
}
private mutating func deallocateIterator() {
self.wrappedIterator = nil
}
}
public func makeAsyncIterator() -> AsyncIterator {
return AsyncIterator(
stateMachine: self.stateMachine,
wrappedIterator: self.wrappedSequence.makeAsyncIterator()
)
}
}
// MARK: - KafkaConsumer
/// A ``KafkaConsumer `` can be used to consume messages from a Kafka cluster.
public final class KafkaConsumer: Sendable, Service {
typealias Producer = NIOThrowingAsyncSequenceProducer<
Result<KafkaConsumerMessage, Error>,
Error,
NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure,
KafkaConsumerCloseOnTerminate
>
/// The configuration object of the consumer client.
private let configuration: KafkaConsumerConfiguration
/// A logger.
private let logger: Logger
/// State of the `KafkaConsumer`.
private let stateMachine: NIOLockedValueBox<StateMachine>
/// An asynchronous sequence containing messages from the Kafka cluster.
public let messages: KafkaConsumerMessages
// Private initializer, use factory method or convenience init to create KafkaConsumer
/// Initialize a new ``KafkaConsumer``.
/// To listen to incoming messages, please subscribe to a list of topics using ``subscribe(topics:)``
/// or assign the consumer to a particular topic + partition pair using ``assign(topic:partition:offset:)``.
///
/// - Parameters:
/// - client: Client used for handling the connection to the Kafka cluster.
/// - stateMachine: The state machine containing the state of the ``KafkaConsumer``.
/// - configuration: The ``KafkaConsumerConfiguration`` for configuring the ``KafkaConsumer``.
/// - logger: A logger.
/// - Throws: A ``KafkaError`` if the initialization failed.
private init(
client: RDKafkaClient,
stateMachine: NIOLockedValueBox<StateMachine>,
configuration: KafkaConsumerConfiguration,
logger: Logger
) throws {
self.configuration = configuration
self.stateMachine = stateMachine
self.logger = logger
let sourceAndSequence = NIOThrowingAsyncSequenceProducer.makeSequence(
elementType: Result<KafkaConsumerMessage, Error>.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
delegate: KafkaConsumerCloseOnTerminate(stateMachine: self.stateMachine)
)
self.messages = KafkaConsumerMessages(
stateMachine: self.stateMachine,
wrappedSequence: sourceAndSequence.sequence
)
self.stateMachine.withLockedValue {
$0.initialize(
client: client,
source: sourceAndSequence.source
)
}
// Forward main queue events to the consumer queue.
try client.pollSetConsumer()
switch configuration.consumptionStrategy._internal {
case .partition(topic: let topic, partition: let partition, offset: let offset):
try self.assign(topic: topic, partition: partition, offset: offset)
case .group(groupID: _, topics: let topics):
try self.subscribe(topics: topics)
}
}
/// Initialize a new ``KafkaConsumer``.
///
/// This creates a consumer without that does not listen to any events other than consumer messages.
///
/// - Parameters:
/// - configuration: The ``KafkaConsumerConfiguration`` for configuring the ``KafkaConsumer``.
/// - logger: A logger.
/// - Returns: The newly created ``KafkaConsumer``.
/// - Throws: A ``KafkaError`` if the initialization failed.
public convenience init(
configuration: KafkaConsumerConfiguration,
logger: Logger
) throws {
let stateMachine = NIOLockedValueBox(StateMachine(logger: logger))
var subscribedEvents: [RDKafkaEvent] = [.log, .fetch]
// Only listen to offset commit events when autoCommit is false
if configuration.isAutoCommitEnabled == false {
subscribedEvents.append(.offsetCommit)
}
let client = try RDKafkaClient.makeClient(
type: .consumer,
configDictionary: configuration.dictionary,
events: subscribedEvents,
logger: logger
)
try self.init(
client: client,
stateMachine: stateMachine,
configuration: configuration,
logger: logger
)
}
/// Initialize a new ``KafkaConsumer`` and a ``KafkaConsumerEvents`` asynchronous sequence.
///
/// Use the asynchronous sequence to consume events.
///
/// - Important: When the asynchronous sequence is deinited the producer will be shut down and disallowed from sending more messages.
/// Additionally, make sure to consume the asynchronous sequence otherwise the events will be buffered in memory indefinitely.
///
/// - Parameters:
/// - configuration: The ``KafkaConsumerConfiguration`` for configuring the ``KafkaConsumer``.
/// - logger: A logger.
/// - Returns: A tuple containing the created ``KafkaConsumer`` and the ``KafkaConsumerEvents``
/// `AsyncSequence` used for receiving message events.
/// - Throws: A ``KafkaError`` if the initialization failed.
public static func makeConsumerWithEvents(
configuration: KafkaConsumerConfiguration,
logger: Logger
) throws -> (KafkaConsumer, KafkaConsumerEvents) {
let stateMachine = NIOLockedValueBox(StateMachine(logger: logger))
var subscribedEvents: [RDKafkaEvent] = [.log, .fetch]
// Only listen to offset commit events when autoCommit is false
if configuration.isAutoCommitEnabled == false {
subscribedEvents.append(.offsetCommit)
}
let client = try RDKafkaClient.makeClient(
type: .consumer,
configDictionary: configuration.dictionary,
events: subscribedEvents,
logger: logger
)
let consumer = try KafkaConsumer(
client: client,
stateMachine: stateMachine,
configuration: configuration,
logger: logger
)
// Note:
// It's crucial to initialize the `sourceAndSequence` variable AFTER `client`.
// This order is important to prevent the accidental triggering of `KafkaConsumerCloseOnTerminate.didTerminate()`.
// If this order is not met and `RDKafkaClient.makeClient()` fails,
// it leads to a call to `stateMachine.messageSequenceTerminated()` while it's still in the `.uninitialized` state.
let sourceAndSequence = NIOAsyncSequenceProducer.makeSequence(
elementType: KafkaConsumerEvent.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
delegate: KafkaConsumerCloseOnTerminate(stateMachine: stateMachine)
)
let eventsSequence = KafkaConsumerEvents(wrappedSequence: sourceAndSequence.sequence)
return (consumer, eventsSequence)
}
/// Subscribe to the given list of `topics`.
/// The partition assignment happens automatically using `KafkaConsumer`'s consumer group.
/// - Parameter topics: An array of topic names to subscribe to.
/// - Throws: A ``KafkaError`` if subscribing to the topic list failed.
private func subscribe(topics: [String]) throws {
let action = self.stateMachine.withLockedValue { $0.setUpConnection() }
switch action {
case .setUpConnection(let client):
let subscription = RDKafkaTopicPartitionList()
for topic in topics {
subscription.add(
topic: topic,
partition: KafkaPartition.unassigned
)
}
try client.subscribe(topicPartitionList: subscription)
}
}
/// Assign the``KafkaConsumer`` to a specific `partition` of a `topic`.
/// - Parameter topic: Name of the topic that this ``KafkaConsumer`` will read from.
/// - Parameter partition: Partition that this ``KafkaConsumer`` will read from.
/// - Parameter offset: The offset to start consuming from.
/// Defaults to the end of the Kafka partition queue (meaning wait for next produced message).
/// - Throws: A ``KafkaError`` if the consumer could not be assigned to the topic + partition pair.
private func assign(
topic: String,
partition: KafkaPartition,
offset: KafkaOffset
) throws {
let action = self.stateMachine.withLockedValue { $0.setUpConnection() }
switch action {
case .setUpConnection(let client):
let assignment = RDKafkaTopicPartitionList()
assignment.setOffset(topic: topic, partition: partition, offset: offset)
try client.assign(topicPartitionList: assignment)
}
}
/// Start the ``KafkaConsumer``.
///
/// - Important: This method **must** be called and will run until either the calling task is cancelled or gracefully shut down.
public func run() async throws {
try await withGracefulShutdownHandler {
try await self._run()
} onGracefulShutdown: {
self.triggerGracefulShutdown()
}
}
private func _run() async throws {
while !Task.isCancelled {
let nextAction = self.stateMachine.withLockedValue { $0.nextPollLoopAction() }
switch nextAction {
case .pollForAndYieldMessage(let client, let source):
let events = client.eventPoll()
for event in events {
switch event {
case .consumerMessages(let result):
// We do not support back pressure, we can ignore the yield result
_ = source.yield(result)
default:
break // Ignore
}
}
try await Task.sleep(for: self.configuration.pollInterval)
case .pollWithoutYield(let client):
// Ignore poll result.
// We are just polling to serve any remaining events queued inside of `librdkafka`.
// All remaining queued consumer messages will get dropped and not be committed (marked as read).
_ = client.eventPoll()
try await Task.sleep(for: self.configuration.pollInterval)
case .terminatePollLoop:
return
}
}
}
/// Mark all messages up to the passed message in the topic as read.
///
/// This method is only used for manual offset management.
///
/// - Warning: This method fails if the ``KafkaConsumerConfiguration/isAutoCommitEnabled`` configuration property is set to `true` (default).
///
/// - Parameters:
/// - message: Last received message that shall be marked as read.
/// - Throws: A ``KafkaError`` if committing failed.
public func commitSync(_ message: KafkaConsumerMessage) async throws {
let action = self.stateMachine.withLockedValue { $0.commitSync() }
switch action {
case .throwClosedError:
throw KafkaError.connectionClosed(reason: "Tried to commit message offset on a closed consumer")
case .commitSync(let client):
guard self.configuration.isAutoCommitEnabled == false else {
throw KafkaError.config(reason: "Committing manually only works if isAutoCommitEnabled set to false")
}
try await client.commitSync(message)
}
}
/// This function is used to gracefully shut down a Kafka consumer client.
///
/// - Note: Invoking this function is not always needed as the ``KafkaConsumer``
/// will already shut down when consumption of the ``KafkaConsumerMessages`` has ended.
private func triggerGracefulShutdown() {
let action = self.stateMachine.withLockedValue { $0.finish() }
switch action {
case .triggerGracefulShutdown(let client):
self._triggerGracefulShutdown(
client: client,
logger: self.logger
)
case .triggerGracefulShutdownAndFinishSource(let client, let source):
source.finish()
self._triggerGracefulShutdown(
client: client,
logger: self.logger
)
case .none:
return
}
}
private func _triggerGracefulShutdown(
client: RDKafkaClient,
logger: Logger
) {
do {
try client.consumerClose()
} catch {
if let error = error as? KafkaError {
logger.error("Closing KafkaConsumer failed: \(error.description)")
} else {
logger.error("Caught unknown error: \(error)")
}
}
}
func client() throws -> RDKafkaClient {
return try self.stateMachine.withLockedValue { try $0.client() }
}
}
// MARK: - KafkaConsumer + StateMachine
extension KafkaConsumer {
/// State machine representing the state of the ``KafkaConsumer``.
struct StateMachine: Sendable {
/// A logger.
let logger: Logger
/// The state of the ``StateMachine``.
enum State: Sendable {
/// The state machine has been initialized with init() but is not yet Initialized
/// using `func initialize()` (required).
case uninitialized
/// We are in the process of initializing the ``KafkaConsumer``,
/// though ``subscribe()`` / ``assign()`` have not been invoked.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
case initializing(
client: RDKafkaClient,
source: Producer.Source
)
/// The ``KafkaConsumer`` is consuming messages.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
case consuming(
client: RDKafkaClient,
source: Producer.Source
)
/// Consumer is still running but the messages asynchronous sequence was terminated.
/// All incoming messages will be dropped.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case consumptionStopped(client: RDKafkaClient)
/// The ``KafkaConsumer/triggerGracefulShutdown()`` has been invoked.
/// We are now in the process of commiting our last state to the broker.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case finishing(client: RDKafkaClient)
/// The ``KafkaConsumer`` is closed.
case finished
}
/// The current state of the StateMachine.
var state: State = .uninitialized
/// Delayed initialization of `StateMachine` as the `source` and the `pollClosure` are
/// not yet available when the normal initialization occurs.
mutating func initialize(
client: RDKafkaClient,
source: Producer.Source
) {
guard case .uninitialized = self.state else {
fatalError("\(#function) can only be invoked in state .uninitialized, but was invoked in state \(self.state)")
}
self.state = .initializing(
client: client,
source: source
)
}
/// Action to be taken when wanting to poll for a new message.
enum PollLoopAction {
/// Poll for a new ``KafkaConsumerMessage``.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
case pollForAndYieldMessage(
client: RDKafkaClient,
source: Producer.Source
)
/// The ``KafkaConsumer`` stopped consuming messages or
/// is in the process of shutting down.
/// Poll to serve any queued events and commit outstanding state to the broker.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case pollWithoutYield(client: RDKafkaClient)
/// Terminate the poll loop.
case terminatePollLoop
}
/// Returns the next action to be taken when wanting to poll.
/// - Returns: The next action to be taken when wanting to poll, or `nil` if there is no action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
mutating func nextPollLoopAction() -> PollLoopAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before reading messages")
case .consuming(let client, let source):
return .pollForAndYieldMessage(client: client, source: source)
case .consumptionStopped(let client):
return .pollWithoutYield(client: client)
case .finishing(let client):
if client.isConsumerClosed {
self.state = .finished
return .terminatePollLoop
} else {
return .pollWithoutYield(client: client)
}
case .finished:
return .terminatePollLoop
}
}
/// Action to be taken when wanting to set up the connection through ``subscribe()`` or ``assign()``.
enum SetUpConnectionAction {
/// Set up the connection through ``subscribe()`` or ``assign()``.
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case setUpConnection(client: RDKafkaClient)
}
/// Get action to be taken when wanting to set up the connection through ``subscribe()`` or ``assign()``.
///
/// - Returns: The action to be taken.
mutating func setUpConnection() -> SetUpConnectionAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing(let client, let source):
self.state = .consuming(
client: client,
source: source
)
return .setUpConnection(client: client)
case .consuming, .consumptionStopped, .finishing, .finished:
fatalError("\(#function) should only be invoked upon initialization of KafkaConsumer")
}
}
/// The messages asynchronous sequence was terminated.
/// All incoming messages will be dropped.
mutating func messageSequenceTerminated() {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Call to \(#function) before setUpConnection() was invoked")
case .consumptionStopped:
fatalError("messageSequenceTerminated() must not be invoked more than once")
case .consuming(let client, _):
self.state = .consumptionStopped(client: client)
case .finishing, .finished:
break
}
}
/// Action to take when wanting to store a message offset (to be auto-committed by `librdkafka`).
enum StoreOffsetAction {
/// Store the message offset with the given `client`.
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case storeOffset(client: RDKafkaClient)
/// The consumer is in the process of `.finishing` or even `.finished`.
/// Stop yielding new elements and terminate the asynchronous sequence.
case terminateConsumerSequence
}
/// Get action to take when wanting to store a message offset (to be auto-committed by `librdkafka`).
func storeOffset() -> StoreOffsetAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before committing offsets")
case .consumptionStopped:
fatalError("Cannot store offset when consumption has been stopped")
case .consuming(let client, _):
return .storeOffset(client: client)
case .finishing, .finished:
return .terminateConsumerSequence
}
}
/// Action to be taken when wanting to do a synchronous commit.
enum CommitSyncAction {
/// Do a synchronous commit.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case commitSync(
client: RDKafkaClient
)
/// Throw an error. The ``KafkaConsumer`` is closed.
case throwClosedError
}
/// Get action to be taken when wanting to do a synchronous commit.
/// - Returns: The action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
func commitSync() -> CommitSyncAction {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("Subscribe to consumer group / assign to topic partition pair before committing offsets")
case .consumptionStopped:
fatalError("Cannot commit when consumption has been stopped")
case .consuming(let client, _):
return .commitSync(client: client)
case .finishing, .finished:
return .throwClosedError
}
}
/// Action to be taken when wanting to do close the consumer.
enum FinishAction {
/// Shut down the ``KafkaConsumer``.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
case triggerGracefulShutdown(
client: RDKafkaClient
)
/// Shut down the ``KafkaConsumer`` and finish the given `source` object.
///
/// - Parameter client: Client used for handling the connection to the Kafka cluster.
/// - Parameter source: ``NIOAsyncSequenceProducer/Source`` used for yielding new elements.
case triggerGracefulShutdownAndFinishSource(
client: RDKafkaClient,
source: Producer.Source
)
}
/// Get action to be taken when wanting to do close the consumer.
/// - Returns: The action to be taken, or `nil` if there is no action to be taken.
///
/// - Important: This function throws a `fatalError` if called while in the `.initializing` state.
mutating func finish() -> FinishAction? {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing:
fatalError("subscribe() / assign() should have been invoked before \(#function)")
case .consuming(let client, let source):
self.state = .finishing(client: client)
return .triggerGracefulShutdownAndFinishSource(
client: client,
source: source
)
case .consumptionStopped(let client):
self.state = .finishing(client: client)
return .triggerGracefulShutdown(client: client)
case .finishing, .finished:
return nil
}
}
func client() throws -> RDKafkaClient {
switch self.state {
case .uninitialized:
fatalError("\(#function) invoked while still in state \(self.state)")
case .initializing(let client, _):
return client
case .consuming(let client, _):
return client
case .consumptionStopped(let client):
return client
case .finishing(let client):
return client
case .finished:
throw KafkaError.client(reason: "Client is stopped")
}
}
}
}