1313//===----------------------------------------------------------------------===//
1414
1515import Metrics
16+ import NIOConcurrencyHelpers
1617
1718/// The system funnel for all `Metrics` interactions from the Redis library.
1819///
@@ -38,14 +39,8 @@ public struct RedisMetrics {
3839 }
3940 }
4041
41- private static let activeConnectionCountGauge = Gauge ( label: . activeConnectionCount)
42- /// The current number of connections this library has active.
43- /// - Note: Changing this number will update the `Metrics.Gauge` stored for recording the new value.
44- public static var activeConnectionCount : Int = 0 {
45- didSet {
46- activeConnectionCountGauge. record ( activeConnectionCount)
47- }
48- }
42+ /// The wrapped `Metrics.Gauge` maintaining the current number of connections this library has active.
43+ public static var activeConnectionCount = ActiveConnectionGauge ( )
4944 /// The `Metrics.Counter` that retains the number of connections made since application startup.
5045 public static let totalConnectionCount = Counter ( label: . totalConnectionCount)
5146 /// The `Metrics.Counter` that retains the number of commands that successfully returned from Redis
@@ -61,6 +56,31 @@ public struct RedisMetrics {
6156 private init ( ) { }
6257}
6358
59+ /// A specialized wrapper class for working with `Metrics.Gauge` objects for the purpose of an incrementing or decrementing count of active Redis connections.
60+ public class ActiveConnectionGauge {
61+ private let gauge = Gauge ( label: . activeConnectionCount)
62+ private let count = Atomic < Int > ( value: 0 )
63+
64+ /// The number of the connections that are currently reported as active.
65+ var currentCount : Int { return count. load ( ) }
66+
67+ internal init ( ) { }
68+
69+ /// Increments the current count by the amount specified.
70+ /// - Parameter amount: The number to increase the current count by. Default is `1`.
71+ public func increment( by amount: Int = 1 ) {
72+ _ = self . count. add ( amount)
73+ self . gauge. record ( self . count. load ( ) )
74+ }
75+
76+ /// Decrements the current count by the amount specified.
77+ /// - Parameter amount: The number to decrease the current count by. Default is `1`.
78+ public func decrement( by amount: Int = 1 ) {
79+ _ = self . count. sub ( amount)
80+ self . gauge. record ( self . count. load ( ) )
81+ }
82+ }
83+
6484extension Metrics . Counter {
6585 @inline ( __always)
6686 convenience init ( label: RedisMetrics . Label ) {
0 commit comments