Skip to content

Commit 606ed3f

Browse files
Merge pull request #73 from marcoferrer/add-lifo-queue-metrics
implement lifo queue gauge metrics
2 parents cc089ff + 4ff499b commit 606ed3f

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

core/dep.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ const (
1919
MetricMinRTT = "min_rtt"
2020
// MetricWindowMinRTT is the name of the metric for the Window's Minimum Round Trip Time
2121
MetricWindowMinRTT = "window.min_rtt"
22-
// MetricWindowQueueSize represents the name of hte metric for the Window's Queue Size
22+
// MetricWindowQueueSize represents the name of the metric for the Window's Queue Size
2323
MetricWindowQueueSize = "window.queue_size"
24+
// MetricLifoQueueSize represents the name of the metric for the size of a lifo queue
25+
MetricLifoQueueSize = "lifo.queue_size"
26+
// MetricLifoQueueLimit represents the name of the metric for the max size of a lifo queue
27+
MetricLifoQueueLimit = "lifo.queue_limit"
2428
)
2529

2630
// PrefixMetricWithName will prefix a given name with the metric name in the form "<name>.<metric>"

limiter/lifo_blocking.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,46 @@ func NewLifoBlockingLimiter(
186186
delegate core.Limiter,
187187
maxBacklogSize int,
188188
maxBacklogTimeout time.Duration,
189+
registry core.MetricRegistry,
190+
tags ...string,
189191
) *LifoBlockingLimiter {
190192
if maxBacklogSize <= 0 {
191193
maxBacklogSize = 100
192194
}
193195
if maxBacklogTimeout == 0 {
194196
maxBacklogTimeout = time.Millisecond * 1000
195197
}
196-
return &LifoBlockingLimiter{
198+
if registry == nil {
199+
registry = &core.EmptyMetricRegistry{}
200+
}
201+
202+
l := &LifoBlockingLimiter{
197203
delegate: delegate,
198204
maxBacklogSize: uint64(maxBacklogSize),
199205
maxBacklogTimeout: maxBacklogTimeout,
200206
backlog: lifoQueue{},
201207
}
208+
209+
registry.RegisterGauge(core.MetricLifoQueueLimit, func() (value float64, ok bool) {
210+
return float64(maxBacklogSize), true
211+
}, tags...)
212+
213+
registry.RegisterGauge(core.MetricLifoQueueSize, func() (value float64, ok bool) {
214+
return float64(l.backlog.len()), true
215+
}, tags...)
216+
217+
return l
202218
}
203219

204220
// NewLifoBlockingLimiterWithDefaults will create a new LifoBlockingLimiter with default values.
205221
func NewLifoBlockingLimiterWithDefaults(
206222
delegate core.Limiter,
207223
) *LifoBlockingLimiter {
208-
return NewLifoBlockingLimiter(delegate, 100, time.Millisecond*1000)
224+
return NewLifoBlockingLimiter(
225+
delegate,
226+
100, time.Millisecond*1000,
227+
&core.EmptyMetricRegistry{},
228+
)
209229
}
210230

211231
func (l *LifoBlockingLimiter) tryAcquire(ctx context.Context) core.Listener {

limiter/lifo_blocking_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func TestLifoBlockingLimiter(t *testing.T) {
168168
limit.NoopLimitLogger{},
169169
core.EmptyMetricRegistryInstance,
170170
)
171-
limiter := NewLifoBlockingLimiter(delegateLimiter, -1, 0)
171+
limiter := NewLifoBlockingLimiter(delegateLimiter, -1, 0, nil)
172172
asrt.NotNil(limiter)
173173
asrt.True(strings.Contains(limiter.String(), "LifoBlockingLimiter{delegate=DefaultLimiter{"))
174174
})
@@ -214,8 +214,8 @@ func TestLifoBlockingLimiter(t *testing.T) {
214214
go func(j int) {
215215
startupReady <- true
216216
listener, ok := limiter.Acquire(context.Background())
217-
asrt.True(ok)
218-
asrt.NotNil(listener)
217+
asrt.True(ok, "must be true for j %d", j)
218+
asrt.NotNil(listener, "must be not be nil for j %d", j)
219219
mu.Lock()
220220
waitingListeners = append(waitingListeners, acquiredListenerLifo{id: j, listener: listener})
221221
mu.Unlock()

patterns/pool/fixed_pool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func NewFixedPool(
8686
case OrderingLIFO:
8787
fp = FixedPool{
8888
limit: fixedLimit,
89-
limiter: limiter.NewLifoBlockingLimiter(defaultLimiter, maxBacklog, timeout),
89+
limiter: limiter.NewLifoBlockingLimiter(defaultLimiter, maxBacklog, timeout, metricRegistry),
9090
ordering: ordering,
9191
}
9292
default:

patterns/pool/generic_pool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func NewPool(
5858
}
5959
case OrderingLIFO:
6060
p = Pool{
61-
limiter: limiter.NewLifoBlockingLimiter(delegateLimiter, maxBacklog, timeout),
61+
limiter: limiter.NewLifoBlockingLimiter(delegateLimiter, maxBacklog, timeout, metricRegistry),
6262
}
6363
default:
6464
p = Pool{

0 commit comments

Comments
 (0)