Skip to content

Commit 8a16481

Browse files
committed
* make Source optional in KafkaPollingSystem
1 parent e7d7f10 commit 8a16481

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Sources/SwiftKafka/KafkaPollingSystem.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class KafkaPollingSystem<Element>: Sendable {
3737
///
3838
/// - Parameter pollInterval: The desired time interval between two consecutive polls.
3939
/// - Returns: An awaitable task representing the execution of the poll loop.
40-
func run(pollInterval: Duration, pollClosure: @escaping () -> Void, source: Producer.Source) async {
40+
func run(pollInterval: Duration, pollClosure: @escaping () -> Void, source: Producer.Source?) async {
4141
switch self.stateMachineLock.withLockedValue({ $0.run(source, pollClosure) }) {
4242
case .alreadyClosed:
4343
return
@@ -87,7 +87,7 @@ final class KafkaPollingSystem<Element>: Sendable {
8787
case .started(let source, _, _), .producing(let source, _, _), .stopProducing(let source, _, _, _):
8888
// We can also yield when in .stopProducing,
8989
// the AsyncSequenceProducer will buffer for us
90-
let yieldResult = source.yield(element)
90+
let yieldResult = source?.yield(element)
9191
switch yieldResult {
9292
case .produceMore:
9393
let action = stateMachine.produceMore()
@@ -102,6 +102,8 @@ final class KafkaPollingSystem<Element>: Sendable {
102102
case .dropped:
103103
let action = stateMachine.terminate()
104104
self.handleTerminateAction(action)
105+
case .none:
106+
break
105107
}
106108
case .idle, .finished:
107109
return
@@ -114,9 +116,9 @@ final class KafkaPollingSystem<Element>: Sendable {
114116
func handleTerminateAction(_ action: StateMachine.TerminateAction?) {
115117
switch action {
116118
case .finishSequenceSource(let source):
117-
source.finish()
119+
source?.finish()
118120
case .finishSequenceSourceAndResume(let source, let continuation):
119-
source.finish()
121+
source?.finish()
120122
continuation?.resume()
121123
case .none:
122124
break
@@ -150,20 +152,20 @@ extension KafkaPollingSystem {
150152
case idle
151153
/// The ``run()`` method has been invoked and the ``KafkaPollingSystem`` is ready.
152154
case started(
153-
source: Producer.Source,
155+
source: Producer.Source?,
154156
pollClosure: () -> Void,
155157
running: Bool
156158
)
157159
/// The system up and producing acknowledgement messages.
158160
case producing(
159-
source: Producer.Source,
161+
source: Producer.Source?,
160162
pollClosure: () -> Void,
161163
running: Bool
162164
)
163165
/// The pool loop is currently suspended and we are waiting for an invocation
164166
/// of `produceMore()` to continue producing messages.
165167
case stopProducing(
166-
source: Producer.Source,
168+
source: Producer.Source?,
167169
continuation: CheckedContinuation<Void, Never>?,
168170
pollClosure: () -> Void,
169171
running: Bool
@@ -185,7 +187,7 @@ extension KafkaPollingSystem {
185187
case startLoop
186188
}
187189

188-
mutating func run(_ source: Producer.Source, _ pollClosure: @escaping () -> Void) -> RunAction {
190+
mutating func run(_ source: Producer.Source?, _ pollClosure: @escaping () -> Void) -> RunAction {
189191
switch self.state {
190192
case .idle:
191193
self.state = .started(source: source, pollClosure: pollClosure, running: true)
@@ -290,11 +292,11 @@ extension KafkaPollingSystem {
290292
/// Actions to take after ``terminate()`` has been invoked on the ``KafkaPollingSystem/StateMachine``.
291293
enum TerminateAction {
292294
/// Invoke `finish()` on the given `NIOAsyncSequenceProducer.Source`.
293-
case finishSequenceSource(source: Producer.Source)
295+
case finishSequenceSource(source: Producer.Source?)
294296
/// Invoke `finish()` on the given `NIOAsyncSequenceProducer.Source`
295297
/// and resume the given `continuation`.
296298
case finishSequenceSourceAndResume(
297-
source: Producer.Source,
299+
source: Producer.Source?,
298300
continuation: CheckedContinuation<Void, Never>?
299301
)
300302
}

0 commit comments

Comments
 (0)