/
RedisDriver.swift
73 lines (60 loc) · 2.73 KB
/
RedisDriver.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
import NIO
import NIOConcurrencyHelpers
public final class RedisDriver {
/// The threading model to use for asynchronous tasks.
///
/// Using `.eventLoopGroup` will allow an external provider to handle the lifetime of the `EventLoopGroup`,
/// while using `spawnThreads` will cause this `NIORedis` instance to handle the lifetime of a new `EventLoopGroup`.
public enum ThreadOwnershipModel {
case `internal`(threadCount: Int)
case external(EventLoopGroup)
}
public let eventLoopGroup: EventLoopGroup
private let ownershipModel: ThreadOwnershipModel
/// Is the driver available for operations?
public var isRunning: Bool { return _isRunning.load() }
private let _isRunning = Atomic<Bool>(value: true)
deinit { assert(!_isRunning.load(), "Redis driver was not properly shut down!") }
/// Creates a driver instance to create connections to a Redis.
/// - Important: Call `terminate()` before deinitializing to properly cleanup resources.
/// - Parameter ownershipModel: The model to use for handling connection resources.
public init(ownershipModel model: ThreadOwnershipModel) {
self.ownershipModel = model
switch model {
case .internal(let count):
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: count)
case .external(let group):
self.eventLoopGroup = group
}
}
/// Handles the proper shutdown of managed resources.
/// - Important: This method should always be called, even when running in `.eventLoopGroup` mode.
public func terminate() throws {
guard _isRunning.exchange(with: false) else { return }
switch ownershipModel {
case .internal: try self.eventLoopGroup.syncShutdownGracefully()
case .external: return
}
}
/// Creates a new `RedisConnection` with the parameters provided.
public func makeConnection(
hostname: String = "localhost",
port: Int = 6379,
password: String? = nil
) -> EventLoopFuture<NIORedisConnection> {
let bootstrap = ClientBootstrap.makeForRedis(using: eventLoopGroup)
return bootstrap.connect(host: hostname, port: port)
.map { return NIORedisConnection(channel: $0) }
.flatMap { connection in
guard let pw = password else {
return self.eventLoopGroup.next().makeSucceededFuture(connection)
}
return connection.authorize(with: pw).map { _ in return connection }
}
}
}
private extension ChannelPipeline {
func addHandlers(_ handlers: ChannelHandler...) -> EventLoopFuture<Void> {
return .andAllSucceed(handlers.map { addHandler($0) }, on: eventLoop)
}
}