Skip to content

Commit 43ea6af

Browse files
authored
Make RedisConnection.Configuration.defaultPort thread safe (#81)
`RedisConnection.Configuration.defaultPort` is currently unprotected shared mutable state. To ensure thread safety this patch adds an atomic to back this property. Since setting the `defaultPort` doesn't make much sense for adopters, we deprecate the setter. Lastly we mark `RedisConnection.Configuration` as `Sendable`.
1 parent 5c8a788 commit 43ea6af

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Sources/RediStack/RedisConnection+Configuration.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,31 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Atomics
1516
import NIOCore
17+
import NIOConcurrencyHelpers
1618
import NIOPosix
1719
import Logging
1820
import struct Foundation.URL
1921
import protocol Foundation.LocalizedError
2022

2123
extension RedisConnection {
2224
/// A configuration object for creating a single connection to Redis.
23-
public struct Configuration {
25+
public struct Configuration: Sendable {
26+
private static let _defaultPortAtomic = ManagedAtomic(6379)
27+
2428
/// The default port that Redis uses.
2529
///
2630
/// See [https://redis.io/topics/quickstart](https://redis.io/topics/quickstart)
27-
public static var defaultPort = 6379
31+
public static var defaultPort: Int {
32+
get {
33+
self._defaultPortAtomic.load(ordering: .acquiring)
34+
}
35+
@available(*, deprecated, message: "Setting the default Redis port will be removed in the next major release")
36+
set {
37+
self._defaultPortAtomic.store(newValue, ordering: .releasing)
38+
}
39+
}
2840

2941
internal static let defaultLogger = Logger.redisBaseConnectionLogger
3042

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the RediStack open source project
4+
//
5+
// Copyright (c) 2023 RediStack project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of RediStack project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import RediStack
16+
import XCTest
17+
18+
final class RedisConnection_ConfigurationTest: XCTestCase {
19+
20+
func testGetDefaultRedisPort() {
21+
XCTAssertEqual(RedisConnection.Configuration.defaultPort, 6379)
22+
}
23+
24+
@available(*, deprecated, message: "Testing deprecated functionality")
25+
func testGetAndSetTheDefaultRedisPort() {
26+
XCTAssertEqual(RedisConnection.Configuration.defaultPort, 6379)
27+
RedisConnection.Configuration.defaultPort = 1234
28+
XCTAssertEqual(RedisConnection.Configuration.defaultPort, 1234)
29+
30+
// reset the default port
31+
RedisConnection.Configuration.defaultPort = 6379
32+
}
33+
}
34+

0 commit comments

Comments
 (0)