Skip to content

Commit

Permalink
Make RedisConnection.Configuration.defaultPort thread safe (#81)
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
fabianfett authored Jul 8, 2023
1 parent 5c8a788 commit 43ea6af
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/RediStack/RedisConnection+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@
//
//===----------------------------------------------------------------------===//

import Atomics
import NIOCore
import NIOConcurrencyHelpers
import NIOPosix
import Logging
import struct Foundation.URL
import protocol Foundation.LocalizedError

extension RedisConnection {
/// A configuration object for creating a single connection to Redis.
public struct Configuration {
public struct Configuration: Sendable {
private static let _defaultPortAtomic = ManagedAtomic(6379)

/// The default port that Redis uses.
///
/// See [https://redis.io/topics/quickstart](https://redis.io/topics/quickstart)
public static var defaultPort = 6379
public static var defaultPort: Int {
get {
self._defaultPortAtomic.load(ordering: .acquiring)
}
@available(*, deprecated, message: "Setting the default Redis port will be removed in the next major release")
set {
self._defaultPortAtomic.store(newValue, ordering: .releasing)
}
}

internal static let defaultLogger = Logger.redisBaseConnectionLogger

Expand Down
34 changes: 34 additions & 0 deletions Tests/RediStackTests/RedisConnection+ConfigurationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the RediStack open source project
//
// Copyright (c) 2023 RediStack project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of RediStack project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import RediStack
import XCTest

final class RedisConnection_ConfigurationTest: XCTestCase {

func testGetDefaultRedisPort() {
XCTAssertEqual(RedisConnection.Configuration.defaultPort, 6379)
}

@available(*, deprecated, message: "Testing deprecated functionality")
func testGetAndSetTheDefaultRedisPort() {
XCTAssertEqual(RedisConnection.Configuration.defaultPort, 6379)
RedisConnection.Configuration.defaultPort = 1234
XCTAssertEqual(RedisConnection.Configuration.defaultPort, 1234)

// reset the default port
RedisConnection.Configuration.defaultPort = 6379
}
}

0 comments on commit 43ea6af

Please sign in to comment.