forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresConnection.swift
50 lines (40 loc) · 1.24 KB
/
PostgresConnection.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
import Foundation
import Logging
public final class PostgresConnection {
let channel: Channel
public var eventLoop: EventLoop {
return self.channel.eventLoop
}
public var closeFuture: EventLoopFuture<Void> {
return channel.closeFuture
}
public var logger: Logger
private var didClose: Bool
public var isClosed: Bool {
return !self.channel.isActive
}
init(channel: Channel, logger: Logger) {
self.channel = channel
self.logger = logger
self.didClose = false
}
public func close() -> EventLoopFuture<Void> {
guard !self.didClose else {
return self.eventLoop.makeSucceededFuture(())
}
self.didClose = true
let promise = self.eventLoop.makePromise(of: Void.self)
self.eventLoop.submit {
switch self.channel.isActive {
case true:
promise.succeed(())
case false:
self.channel.close(mode: .all, promise: promise)
}
}.cascadeFailure(to: promise)
return promise.futureResult
}
deinit {
assert(self.didClose, "PostgresConnection deinitialized before being closed.")
}
}