|
2 | 2 | // |
3 | 3 | // This source file is part of the RediStack open source project |
4 | 4 | // |
5 | | -// Copyright (c) 2020 RediStack project authors |
| 5 | +// Copyright (c) 2020-2021 RediStack project authors |
6 | 6 | // Licensed under Apache License v2.0 |
7 | 7 | // |
8 | 8 | // See LICENSE.txt for license information |
|
11 | 11 | // SPDX-License-Identifier: Apache-2.0 |
12 | 12 | // |
13 | 13 | //===----------------------------------------------------------------------===// |
14 | | - |
15 | | -import Logging |
16 | | -import NIO |
17 | | - |
18 | | -extension RedisConnection { |
19 | | - /// The documented default port that Redis connects through. |
20 | | - /// |
21 | | - /// See [https://redis.io/topics/quickstart](https://redis.io/topics/quickstart) |
22 | | - @available(*, deprecated, message: "Use RedisConnection.Configuration.defaultPort") |
23 | | - public static var defaultPort: Int { Configuration.defaultPort } |
24 | | - |
25 | | - /// Creates a new connection to a Redis instance. |
26 | | - /// |
27 | | - /// If you would like to specialize the `NIO.ClientBootstrap` that the connection communicates on, override the default by passing it in as `tcpClient`. |
28 | | - /// |
29 | | - /// let eventLoopGroup: EventLoopGroup = ... |
30 | | - /// var customTCPClient = ClientBootstrap.makeRedisTCPClient(group: eventLoopGroup) |
31 | | - /// customTCPClient.channelInitializer { channel in |
32 | | - /// // channel customizations |
33 | | - /// } |
34 | | - /// let connection = RedisConnection.connect( |
35 | | - /// to: ..., |
36 | | - /// on: eventLoopGroup.next(), |
37 | | - /// password: ..., |
38 | | - /// tcpClient: customTCPClient |
39 | | - /// ).wait() |
40 | | - /// |
41 | | - /// It is recommended that you be familiar with `ClientBootstrap.makeRedisTCPClient(group:)` and `NIO.ClientBootstrap` in general before doing so. |
42 | | - /// |
43 | | - /// Note: Use of `wait()` in the example is for simplicity. Never call `wait()` on an event loop. |
44 | | - /// |
45 | | - /// - Important: Call `close()` on the connection before letting the instance deinit to properly cleanup resources. |
46 | | - /// - Note: If a `password` is provided, the connection will send an "AUTH" command to Redis as soon as it has been opened. |
47 | | - /// |
48 | | - /// - Parameters: |
49 | | - /// - socket: The `NIO.SocketAddress` information of the Redis instance to connect to. |
50 | | - /// - eventLoop: The `NIO.EventLoop` that this connection will execute all tasks on. |
51 | | - /// - password: The optional password to use for authorizing the connection with Redis. |
52 | | - /// - logger: The `Logging.Logger` instance to use for all client logging purposes. If one is not provided, one will be created. |
53 | | - /// A `Foundation.UUID` will be attached to the metadata to uniquely identify this connection instance's logs. |
54 | | - /// - tcpClient: If you have chosen to configure a `NIO.ClientBootstrap` yourself, this will be used instead of the `makeRedisTCPClient` instance. |
55 | | - /// - Returns: A `NIO.EventLoopFuture` that resolves with the new connection after it has been opened, and if a `password` is provided, authenticated. |
56 | | - @available(*, deprecated, message: "Use make(configuration:boundEventLoop:configuredTCPClient:) instead") |
57 | | - public static func connect( |
58 | | - to socket: SocketAddress, |
59 | | - on eventLoop: EventLoop, |
60 | | - password: String? = nil, |
61 | | - logger: Logger = .redisBaseConnectionLogger, |
62 | | - tcpClient: ClientBootstrap? = nil |
63 | | - ) -> EventLoopFuture<RedisConnection> { |
64 | | - let config: Configuration |
65 | | - do { |
66 | | - config = try .init( |
67 | | - address: socket, |
68 | | - password: password, |
69 | | - defaultLogger: logger |
70 | | - ) |
71 | | - } catch { |
72 | | - return eventLoop.makeFailedFuture(error) |
73 | | - } |
74 | | - |
75 | | - return self.make(configuration: config, boundEventLoop: eventLoop, configuredTCPClient: tcpClient) |
76 | | - } |
77 | | -} |
78 | | - |
79 | | -extension RedisConnectionPool { |
80 | | - /// Create a new `RedisConnectionPool`. |
81 | | - /// |
82 | | - /// - parameters: |
83 | | - /// - serverConnectionAddresses: The set of Redis servers to which this pool is initially willing to connect. |
84 | | - /// This set can be updated over time. |
85 | | - /// - loop: The event loop to which this pooled client is tied. |
86 | | - /// - maximumConnectionCount: The maximum number of connections to for this pool, either to be preserved or as a hard limit. |
87 | | - /// - minimumConnectionCount: The minimum number of connections to preserve in the pool. If the pool is mostly idle |
88 | | - /// and the Redis servers close these idle connections, the `RedisConnectionPool` will initiate new outbound |
89 | | - /// connections proactively to avoid the number of available connections dropping below this number. Defaults to `1`. |
90 | | - /// - connectionPassword: The password to use to connect to the Redis servers in this pool. |
91 | | - /// - connectionLogger: The `Logger` to pass to each connection in the pool. |
92 | | - /// - connectionTCPClient: The base `ClientBootstrap` to use to create pool connections, if a custom one is in use. |
93 | | - /// - poolLogger: The `Logger` used by the connection pool itself. |
94 | | - /// - connectionBackoffFactor: Used when connection attempts fail to control the exponential backoff. This is a multiplicative |
95 | | - /// factor, each connection attempt will be delayed by this amount times the previous delay. |
96 | | - /// - initialConnectionBackoffDelay: If a TCP connection attempt fails, this is the first backoff value on the reconnection attempt. |
97 | | - /// Subsequent backoffs are computed by compounding this value by `connectionBackoffFactor`. |
98 | | - /// - connectionRetryTimeout: The max time to wait for a connection to be available before failing a particular command or connection operation. |
99 | | - /// The default is 60 seconds. |
100 | | - @available(*, deprecated, message: "Use .init(configuration:boundEventLoop:) instead.") |
101 | | - public convenience init( |
102 | | - serverConnectionAddresses: [SocketAddress], |
103 | | - loop: EventLoop, |
104 | | - maximumConnectionCount: RedisConnectionPoolSize, |
105 | | - minimumConnectionCount: Int = 1, |
106 | | - connectionPassword: String? = nil, // config |
107 | | - connectionLogger: Logger = .redisBaseConnectionLogger, // config |
108 | | - connectionTCPClient: ClientBootstrap? = nil, |
109 | | - poolLogger: Logger = .redisBaseConnectionPoolLogger, |
110 | | - connectionBackoffFactor: Float32 = 2, |
111 | | - initialConnectionBackoffDelay: TimeAmount = .milliseconds(100), |
112 | | - connectionRetryTimeout: TimeAmount? = .seconds(60) |
113 | | - ) { |
114 | | - self.init( |
115 | | - configuration: Configuration( |
116 | | - initialServerConnectionAddresses: serverConnectionAddresses, |
117 | | - maximumConnectionCount: maximumConnectionCount, |
118 | | - connectionFactoryConfiguration: ConnectionFactoryConfiguration( |
119 | | - connectionPassword: connectionPassword, |
120 | | - connectionDefaultLogger: connectionLogger, |
121 | | - tcpClient: connectionTCPClient |
122 | | - ), |
123 | | - minimumConnectionCount: minimumConnectionCount, |
124 | | - connectionBackoffFactor: connectionBackoffFactor, |
125 | | - initialConnectionBackoffDelay: initialConnectionBackoffDelay, |
126 | | - connectionRetryTimeout: connectionRetryTimeout, |
127 | | - poolDefaultLogger: poolLogger |
128 | | - ), |
129 | | - boundEventLoop: loop |
130 | | - ) |
131 | | - } |
132 | | -} |
133 | | - |
134 | | -// MARK: - RedisKeyLifetime |
135 | | -@available(*, deprecated, message: "renamed to RedisKey.Lifetime") |
136 | | -public typealias RedisKeyLifetime = RedisKey.Lifetime |
137 | | - |
138 | | -extension RedisKey.Lifetime { |
139 | | - @available(*, deprecated, message: "renamed to Duration") |
140 | | - public typealias Lifetime = Duration |
141 | | -} |
0 commit comments