Skip to content

Commit 3001e41

Browse files
authored
Remove Context, use Logger everywhere instead (#70)
We have an internal `typealias Context = Logging.Logger`. This is quite confusing. Remove the typealias, use logger everywhere instead.
1 parent 018a9b9 commit 3001e41

File tree

7 files changed

+77
-95
lines changed

7 files changed

+77
-95
lines changed

Sources/RediStack/ConnectionPool/ConnectionPool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2020 RediStack project authors
5+
// Copyright (c) 2020-2023 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -103,13 +103,13 @@ internal final class ConnectionPool {
103103
minimumConnectionCount: Int,
104104
leaky: Bool,
105105
loop: EventLoop,
106-
systemContext: Context,
106+
backgroundLogger: Logger,
107107
connectionBackoffFactor: Float32 = 2,
108108
initialConnectionBackoffDelay: TimeAmount = .milliseconds(100),
109109
connectionFactory: @escaping (EventLoop) -> EventLoopFuture<RedisConnection>
110110
) {
111111
guard minimumConnectionCount <= maximumConnectionCount else {
112-
systemContext.critical("pool's minimum connection count is higher than the maximum")
112+
backgroundLogger.critical("pool's minimum connection count is higher than the maximum")
113113
preconditionFailure("Minimum connection count must not exceed maximum")
114114
}
115115

Sources/RediStack/RedisConnection.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2019-2021 RediStack project authors
5+
// Copyright (c) 2019-2023 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -59,7 +59,7 @@ extension RedisConnection {
5959

6060
var future = client
6161
.connect(to: config.address)
62-
.map { return RedisConnection(configuredRESPChannel: $0, context: config.defaultLogger) }
62+
.map { return RedisConnection(configuredRESPChannel: $0, backgroundLogger: config.defaultLogger) }
6363

6464
// if a password is specified, use it to authenticate before further operations happen
6565
if let password = config.password {
@@ -147,8 +147,8 @@ public final class RedisConnection: RedisClient, RedisClientWithUserContext {
147147
public var onUnexpectedClosure: (() -> Void)?
148148

149149
internal let channel: Channel
150-
private let systemContext: Context
151-
private var logger: Logger { self.systemContext }
150+
private let backgroundLogger: Logger
151+
private var logger: Logger { self.backgroundLogger }
152152

153153
private let autoflush = ManagedAtomic<Bool>(true)
154154
private let allowPubSub = ManagedAtomic<Bool>(true)
@@ -166,14 +166,14 @@ public final class RedisConnection: RedisClient, RedisClientWithUserContext {
166166
}
167167
}
168168

169-
internal init(configuredRESPChannel: Channel, context: Context) {
169+
internal init(configuredRESPChannel: Channel, backgroundLogger: Logger) {
170170
self.channel = configuredRESPChannel
171171
// there is a mix of verbiage here as the API is forward thinking towards "baggage context"
172172
// while right now it's just an alias of a 'Logging.logger'
173173
// in the future this will probably be a property _on_ the context
174-
var logger = context
174+
var logger = backgroundLogger
175175
logger[metadataKey: RedisLogging.MetadataKeys.connectionID] = "\(self.id.description)"
176-
self.systemContext = logger
176+
self.backgroundLogger = logger
177177

178178
RedisMetrics.activeConnectionCount.increment()
179179
RedisMetrics.totalConnectionCount.increment()
@@ -224,18 +224,18 @@ extension RedisConnection {
224224
/// If a `RedisError` is returned, the future will be failed instead.
225225
public func send(command: String, with arguments: [RESPValue]) -> EventLoopFuture<RESPValue> {
226226
self.eventLoop.flatSubmit {
227-
return self.send(command: command, with: arguments, context: nil)
227+
return self.send(command: command, with: arguments, logger: nil)
228228
}
229229
}
230230

231231
internal func send(
232232
command: String,
233233
with arguments: [RESPValue],
234-
context: Context?
234+
logger: Logger?
235235
) -> EventLoopFuture<RESPValue> {
236236
self.eventLoop.preconditionInEventLoop()
237237

238-
let logger = self.prepareLoggerForUse(context)
238+
let logger = self.prepareLoggerForUse(logger)
239239

240240
guard self.isConnected else {
241241
let error = RedisClientError.connectionClosed
@@ -375,7 +375,7 @@ extension RedisConnection {
375375

376376
extension RedisConnection {
377377
public func logging(to logger: Logger) -> RedisClient {
378-
return UserContextRedisClient(client: self, context: self.prepareLoggerForUse(logger))
378+
return UserContextRedisClient(client: self, logger: self.prepareLoggerForUse(logger))
379379
}
380380

381381
private func prepareLoggerForUse(_ logger: Logger?) -> Logger {
@@ -411,19 +411,19 @@ extension RedisConnection {
411411
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
412412
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
413413
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?,
414-
context: Context?
414+
logger: Logger?
415415
) -> EventLoopFuture<Void> {
416-
return self._subscribe(.channels(channels), receiver, subscribeHandler, unsubscribeHandler, context)
416+
return self._subscribe(.channels(channels), receiver, subscribeHandler, unsubscribeHandler, logger)
417417
}
418418

419419
internal func psubscribe(
420420
to patterns: [String],
421421
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
422422
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
423423
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?,
424-
context: Context?
424+
logger: Logger?
425425
) -> EventLoopFuture<Void> {
426-
return self._subscribe(.patterns(patterns), receiver, subscribeHandler, unsubscribeHandler, context)
426+
return self._subscribe(.patterns(patterns), receiver, subscribeHandler, unsubscribeHandler, logger)
427427
}
428428

429429
private func _subscribe(
@@ -503,12 +503,12 @@ extension RedisConnection {
503503
return self._unsubscribe(.patterns(patterns), nil)
504504
}
505505

506-
internal func unsubscribe(from channels: [RedisChannelName], context: Context?) -> EventLoopFuture<Void> {
507-
return self._unsubscribe(.channels(channels), context)
506+
internal func unsubscribe(from channels: [RedisChannelName], logger: Logger?) -> EventLoopFuture<Void> {
507+
return self._unsubscribe(.channels(channels), logger)
508508
}
509509

510-
internal func punsubscribe(from patterns: [String], context: Context?) -> EventLoopFuture<Void> {
511-
return self._unsubscribe(.patterns(patterns), context)
510+
internal func punsubscribe(from patterns: [String], logger: Logger?) -> EventLoopFuture<Void> {
511+
return self._unsubscribe(.patterns(patterns), logger)
512512
}
513513

514514
private func _unsubscribe(_ target: RedisSubscriptionTarget, _ logger: Logger?) -> EventLoopFuture<Void> {

Sources/RediStack/RedisConnectionPool.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the RediStack open source project
44
//
5-
// Copyright (c) 2020 RediStack project authors
5+
// Copyright (c) 2020-2023 RediStack project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -80,7 +80,7 @@ public class RedisConnectionPool {
8080
minimumConnectionCount: config.minimumConnectionCount,
8181
leaky: config.maximumConnectionCount.leaky,
8282
loop: boundEventLoop,
83-
systemContext: config.poolDefaultLogger,
83+
backgroundLogger: config.poolDefaultLogger,
8484
connectionBackoffFactor: config.connectionRetryConfiguration.backoff.factor,
8585
initialConnectionBackoffDelay: config.connectionRetryConfiguration.backoff.initialDelay,
8686
connectionFactory: self.connectionFactory(_:)
@@ -257,11 +257,11 @@ extension RedisConnectionPool: RedisClient {
257257
public var eventLoop: EventLoop { self.loop }
258258

259259
public func logging(to logger: Logger) -> RedisClient {
260-
return UserContextRedisClient(client: self, context: self.prepareLoggerForUse(logger))
260+
return UserContextRedisClient(client: self, logger: self.prepareLoggerForUse(logger))
261261
}
262262

263263
public func send(command: String, with arguments: [RESPValue]) -> EventLoopFuture<RESPValue> {
264-
return self.send(command: command, with: arguments, context: nil)
264+
return self.send(command: command, with: arguments, logger: nil)
265265
}
266266

267267
public func subscribe(
@@ -275,7 +275,7 @@ extension RedisConnectionPool: RedisClient {
275275
messageReceiver: receiver,
276276
onSubscribe: subscribeHandler,
277277
onUnsubscribe: unsubscribeHandler,
278-
context: nil
278+
logger: nil
279279
)
280280
}
281281

@@ -290,33 +290,33 @@ extension RedisConnectionPool: RedisClient {
290290
messageReceiver: receiver,
291291
onSubscribe: subscribeHandler,
292292
onUnsubscribe: unsubscribeHandler,
293-
context: nil
293+
logger: nil
294294
)
295295
}
296296

297297
public func unsubscribe(from channels: [RedisChannelName]) -> EventLoopFuture<Void> {
298-
return self.unsubscribe(from: channels, context: nil)
298+
return self.unsubscribe(from: channels, logger: nil)
299299
}
300300

301301
public func punsubscribe(from patterns: [String]) -> EventLoopFuture<Void> {
302-
return self.punsubscribe(from: patterns, context: nil)
302+
return self.punsubscribe(from: patterns, logger: nil)
303303
}
304304
}
305305

306306
// MARK: RedisClientWithUserContext conformance
307307
extension RedisConnectionPool: RedisClientWithUserContext {
308-
internal func send(command: String, with arguments: [RESPValue], context: Logger?) -> EventLoopFuture<RESPValue> {
308+
internal func send(command: String, with arguments: [RESPValue], logger: Logger?) -> EventLoopFuture<RESPValue> {
309309
return self.forwardOperationToConnection(
310310
{ (connection, returnConnection, context) in
311311

312312
connection.sendCommandsImmediately = true
313313

314314
return connection
315-
.send(command: command, with: arguments, context: context)
315+
.send(command: command, with: arguments, logger: context)
316316
.always { _ in returnConnection(connection, context) }
317317
},
318318
preferredConnection: nil,
319-
context: context
319+
context: logger
320320
)
321321
}
322322

@@ -325,7 +325,7 @@ extension RedisConnectionPool: RedisClientWithUserContext {
325325
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
326326
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
327327
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?,
328-
context: Context?
328+
logger: Logger?
329329
) -> EventLoopFuture<Void> {
330330
return self.subscribe(
331331
using: {
@@ -334,24 +334,24 @@ extension RedisConnectionPool: RedisClientWithUserContext {
334334
messageReceiver: receiver,
335335
onSubscribe: subscribeHandler,
336336
onUnsubscribe: $1,
337-
context: $2
337+
logger: $2
338338
)
339339
},
340340
onUnsubscribe: unsubscribeHandler,
341-
context: context
341+
context: logger
342342
)
343343
}
344344

345-
internal func unsubscribe(from channels: [RedisChannelName], context: Context?) -> EventLoopFuture<Void> {
346-
return self.unsubscribe(using: { $0.unsubscribe(from: channels, context: $1) }, context: context)
345+
internal func unsubscribe(from channels: [RedisChannelName], logger: Logger?) -> EventLoopFuture<Void> {
346+
return self.unsubscribe(using: { $0.unsubscribe(from: channels, logger: $1) }, context: logger)
347347
}
348348

349349
internal func psubscribe(
350350
to patterns: [String],
351351
messageReceiver receiver: @escaping RedisSubscriptionMessageReceiver,
352352
onSubscribe subscribeHandler: RedisSubscriptionChangeHandler?,
353353
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?,
354-
context: Context?
354+
logger: Logger?
355355
) -> EventLoopFuture<Void> {
356356
return self.subscribe(
357357
using: {
@@ -360,22 +360,22 @@ extension RedisConnectionPool: RedisClientWithUserContext {
360360
messageReceiver: receiver,
361361
onSubscribe: subscribeHandler,
362362
onUnsubscribe: $1,
363-
context: $2
363+
logger: $2
364364
)
365365
},
366366
onUnsubscribe: unsubscribeHandler,
367-
context: context
367+
context: logger
368368
)
369369
}
370370

371-
internal func punsubscribe(from patterns: [String], context: Context?) -> EventLoopFuture<Void> {
372-
return self.unsubscribe(using: { $0.punsubscribe(from: patterns, context: $1) }, context: context)
371+
internal func punsubscribe(from patterns: [String], logger: Logger?) -> EventLoopFuture<Void> {
372+
return self.unsubscribe(using: { $0.punsubscribe(from: patterns, logger: $1) }, context: logger)
373373
}
374374

375375
private func subscribe(
376-
using operation: @escaping (RedisConnection, @escaping RedisSubscriptionChangeHandler, Context) -> EventLoopFuture<Void>,
376+
using operation: @escaping (RedisConnection, @escaping RedisSubscriptionChangeHandler, Logger) -> EventLoopFuture<Void>,
377377
onUnsubscribe unsubscribeHandler: RedisSubscriptionChangeHandler?,
378-
context: Context?
378+
context: Logger?
379379
) -> EventLoopFuture<Void> {
380380
return self.forwardOperationToConnection(
381381
{ (connection, returnConnection, context) in
@@ -406,8 +406,8 @@ extension RedisConnectionPool: RedisClientWithUserContext {
406406
}
407407

408408
private func unsubscribe(
409-
using operation: @escaping (RedisConnection, Context) -> EventLoopFuture<Void>,
410-
context: Context?
409+
using operation: @escaping (RedisConnection, Logger) -> EventLoopFuture<Void>,
410+
context: Logger?
411411
) -> EventLoopFuture<Void> {
412412
return self.forwardOperationToConnection(
413413
{ (connection, returnConnection, context) in
@@ -430,9 +430,9 @@ extension RedisConnectionPool: RedisClientWithUserContext {
430430

431431
@usableFromInline
432432
internal func forwardOperationToConnection<T>(
433-
_ operation: @escaping (RedisConnection, @escaping (RedisConnection, Context) -> Void, Context) -> EventLoopFuture<T>,
433+
_ operation: @escaping (RedisConnection, @escaping (RedisConnection, Logger) -> Void, Logger) -> EventLoopFuture<T>,
434434
preferredConnection: RedisConnection?,
435-
context: Context?
435+
context: Logger?
436436
) -> EventLoopFuture<T> {
437437
// Establish event loop context then jump to the in-loop version.
438438
guard self.loop.inEventLoop else {

Sources/RediStack/RedisContext.swift

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)