13
13
//===----------------------------------------------------------------------===//
14
14
15
15
import Metrics
16
+ import NIOConcurrencyHelpers
16
17
17
18
/// The system funnel for all `Metrics` interactions from the Redis library.
18
19
///
@@ -38,14 +39,8 @@ public struct RedisMetrics {
38
39
}
39
40
}
40
41
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 ( )
49
44
/// The `Metrics.Counter` that retains the number of connections made since application startup.
50
45
public static let totalConnectionCount = Counter ( label: . totalConnectionCount)
51
46
/// The `Metrics.Counter` that retains the number of commands that successfully returned from Redis
@@ -61,6 +56,31 @@ public struct RedisMetrics {
61
56
private init ( ) { }
62
57
}
63
58
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
+
64
84
extension Metrics . Counter {
65
85
@inline ( __always)
66
86
convenience init ( label: RedisMetrics . Label ) {
0 commit comments