Skip to content

Commit

Permalink
Merge branch 'thread-sanitizer' into 'master'
Browse files Browse the repository at this point in the history
Run thread sanitizer on 5.1 test CI jobs

See merge request Mordil/swift-redi-stack!84
  • Loading branch information
Mordil committed Sep 24, 2019
2 parents 2a27a59 + 939e41b commit 237f049
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,20 @@ build 5.1ubuntu-bionic:
variables:
REDIS_URL: 'redis'
REDIS_PW: 'password'
SANITIZER_ARG: '--sanitize=thread'
services:
- name: redis:5
alias: 'redis'
command: ["redis-server", "--requirepass", "password"]
script:
- swift build -v
- swift test
- swift test $SANITIZER_ARG

test 5.0:ubuntu-xenial:
extends: .test
image: swift:5.0-xenial
variables:
SANITIZER_ARG: ''
test 5.1:ubuntu-xenial:
extends: .test
image: swift:5.1-xenial
Expand All @@ -89,6 +92,8 @@ test latest:ubuntu-xenial:
test 5.0:ubuntu-bionic:
extends: .test
image: swift:5.0-bionic
variables:
SANITIZER_ARG: ''
test 5.1:ubuntu-bionic:
extends: .test
image: swift:5.1-bionic
6 changes: 3 additions & 3 deletions Sources/RediStack/RedisConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public final class RedisConnection: RedisClient {
self.logger[metadataKey: loggingKeyID] = "\(UUID())"
self._state = .open
self.logger.debug("Connection created.")
RedisMetrics.activeConnectionCount += 1
RedisMetrics.activeConnectionCount.increment()
RedisMetrics.totalConnectionCount.increment()

// attach a callback to the channel to capture situations where the channel might be closed out from under
Expand All @@ -160,7 +160,7 @@ public final class RedisConnection: RedisClient {

self.state = .closed
self.logger.warning("Channel was closed unexpectedly.")
RedisMetrics.activeConnectionCount -= 1
RedisMetrics.activeConnectionCount.decrement()
}
}

Expand Down Expand Up @@ -243,7 +243,7 @@ extension RedisConnection {
notification.whenSuccess {
self.state = .closed
self.logger.debug("Connection closed.")
RedisMetrics.activeConnectionCount -= 1
RedisMetrics.activeConnectionCount.decrement()
}

return notification
Expand Down
36 changes: 28 additions & 8 deletions Sources/RediStack/RedisMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

import Metrics
import NIOConcurrencyHelpers

/// The system funnel for all `Metrics` interactions from the Redis library.
///
Expand All @@ -38,14 +39,8 @@ public struct RedisMetrics {
}
}

private static let activeConnectionCountGauge = Gauge(label: .activeConnectionCount)
/// The current number of connections this library has active.
/// - Note: Changing this number will update the `Metrics.Gauge` stored for recording the new value.
public static var activeConnectionCount: Int = 0 {
didSet {
activeConnectionCountGauge.record(activeConnectionCount)
}
}
/// The wrapped `Metrics.Gauge` maintaining the current number of connections this library has active.
public static var activeConnectionCount = ActiveConnectionGauge()
/// The `Metrics.Counter` that retains the number of connections made since application startup.
public static let totalConnectionCount = Counter(label: .totalConnectionCount)
/// The `Metrics.Counter` that retains the number of commands that successfully returned from Redis
Expand All @@ -61,6 +56,31 @@ public struct RedisMetrics {
private init() { }
}

/// A specialized wrapper class for working with `Metrics.Gauge` objects for the purpose of an incrementing or decrementing count of active Redis connections.
public class ActiveConnectionGauge {
private let gauge = Gauge(label: .activeConnectionCount)
private let count = Atomic<Int>(value: 0)

/// The number of the connections that are currently reported as active.
var currentCount: Int { return count.load() }

internal init() { }

/// Increments the current count by the amount specified.
/// - Parameter amount: The number to increase the current count by. Default is `1`.
public func increment(by amount: Int = 1) {
_ = self.count.add(amount)
self.gauge.record(self.count.load())
}

/// Decrements the current count by the amount specified.
/// - Parameter amount: The number to decrease the current count by. Default is `1`.
public func decrement(by amount: Int = 1) {
_ = self.count.sub(amount)
self.gauge.record(self.count.load())
}
}

extension Metrics.Counter {
@inline(__always)
convenience init(label: RedisMetrics.Label) {
Expand Down

0 comments on commit 237f049

Please sign in to comment.