Skip to content

Commit 2046724

Browse files
committed
fix: add explicit deinit to clean up nicely
1 parent 4fd547a commit 2046724

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Sources/Pulsar/PulsarClient/PulsarClient.swift

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public final actor PulsarClient {
2727
// Callback function called whenever the client gets closed, forcefully or user intended.
2828
public let onClosed: ((Error) throws -> Void)?
2929

30+
deinit {
31+
Task { [weak self] in
32+
try await self?.close()
33+
}
34+
}
35+
3036
/// Creates a new Pulsar Client and tries to connect it.
3137
/// - Parameters:
3238
/// - host: The host to connect to. Doesn't need the `pulsar://` prefix.

Sources/PulsarExample/PulsarExample.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ struct PulsarExample {
3333
func connect(eventLoopGroup: EventLoopGroup) async throws {
3434
var msgCount = 0
3535

36-
let client = await PulsarClient(host: "localhost", port: 6650, reconnectLimit: 1) { error in
37-
print("Client closed")
38-
throw error
36+
let client = await PulsarClient(host: "localhost", port: 6650, reconnectLimit: 2) { error in
37+
do {
38+
throw error
39+
} catch {
40+
print("Client closed")
41+
exit(0)
42+
}
43+
44+
// throw error
3945
}
4046
let consumer = try await client.consumer(topic: "persistent://public/default/my-topic2", subscription: "test", subscriptionType: .shared, schema: .string) as PulsarConsumer<String>
4147
Task {

0 commit comments

Comments
 (0)