forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationListener.swift
142 lines (115 loc) · 4.43 KB
/
NotificationListener.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
import NIOCore
// This object is @unchecked Sendable, since we syncronize state on the EL
final class NotificationListener: @unchecked Sendable {
let eventLoop: EventLoop
let channel: String
let id: Int
private var state: State
enum State {
case streamInitialized(CheckedContinuation<PostgresNotificationSequence, Error>)
case streamListening(AsyncThrowingStream<PostgresNotification, Error>.Continuation)
case closure(PostgresListenContext, (PostgresListenContext, PostgresMessage.NotificationResponse) -> Void)
case done
}
init(
channel: String,
id: Int,
eventLoop: EventLoop,
checkedContinuation: CheckedContinuation<PostgresNotificationSequence, Error>
) {
self.channel = channel
self.id = id
self.eventLoop = eventLoop
self.state = .streamInitialized(checkedContinuation)
}
init(
channel: String,
id: Int,
eventLoop: EventLoop,
context: PostgresListenContext,
closure: @Sendable @escaping (PostgresListenContext, PostgresMessage.NotificationResponse) -> Void
) {
self.channel = channel
self.id = id
self.eventLoop = eventLoop
self.state = .closure(context, closure)
}
func startListeningSucceeded(handler: PostgresChannelHandler) {
self.eventLoop.preconditionInEventLoop()
let handlerLoopBound = NIOLoopBound(handler, eventLoop: self.eventLoop)
switch self.state {
case .streamInitialized(let checkedContinuation):
let (stream, continuation) = AsyncThrowingStream.makeStream(of: PostgresNotification.self)
let eventLoop = self.eventLoop
let channel = self.channel
let listenerID = self.id
continuation.onTermination = { reason in
switch reason {
case .cancelled:
eventLoop.execute {
handlerLoopBound.value.cancelNotificationListener(channel: channel, id: listenerID)
}
case .finished:
break
@unknown default:
break
}
}
self.state = .streamListening(continuation)
let notificationSequence = PostgresNotificationSequence(base: stream)
checkedContinuation.resume(returning: notificationSequence)
case .streamListening, .done:
fatalError("Invalid state: \(self.state)")
case .closure:
break // ignore
}
}
func notificationReceived(_ backendMessage: PostgresBackendMessage.NotificationResponse) {
self.eventLoop.preconditionInEventLoop()
switch self.state {
case .streamInitialized, .done:
fatalError("Invalid state: \(self.state)")
case .streamListening(let continuation):
continuation.yield(.init(payload: backendMessage.payload))
case .closure(let postgresListenContext, let closure):
let message = PostgresMessage.NotificationResponse(
backendPID: backendMessage.backendPID,
channel: backendMessage.channel,
payload: backendMessage.payload
)
closure(postgresListenContext, message)
}
}
func failed(_ error: Error) {
self.eventLoop.preconditionInEventLoop()
switch self.state {
case .streamInitialized(let checkedContinuation):
self.state = .done
checkedContinuation.resume(throwing: error)
case .streamListening(let continuation):
self.state = .done
continuation.finish(throwing: error)
case .closure(let postgresListenContext, _):
self.state = .done
postgresListenContext.cancel()
case .done:
break // ignore
}
}
func cancelled() {
self.eventLoop.preconditionInEventLoop()
switch self.state {
case .streamInitialized(let checkedContinuation):
self.state = .done
checkedContinuation.resume(throwing: PSQLError(code: .queryCancelled))
case .streamListening(let continuation):
self.state = .done
continuation.finish()
case .closure(let postgresListenContext, _):
self.state = .done
postgresListenContext.cancel()
case .done:
break // ignore
}
}
}