-
Notifications
You must be signed in to change notification settings - Fork 24
/
concurrency-limiter.go
605 lines (536 loc) · 20.7 KB
/
concurrency-limiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package concurrency
import (
"context"
"fmt"
"math"
"path"
"strconv"
"time"
"github.com/jonboulle/clockwork"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"go.uber.org/multierr"
flowcontrolv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/flowcontrol/check/v1"
policylangv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/language/v1"
policysyncv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/policy/sync/v1"
"github.com/fluxninja/aperture/pkg/agentinfo"
"github.com/fluxninja/aperture/pkg/config"
etcdclient "github.com/fluxninja/aperture/pkg/etcd/client"
etcdwatcher "github.com/fluxninja/aperture/pkg/etcd/watcher"
"github.com/fluxninja/aperture/pkg/log"
"github.com/fluxninja/aperture/pkg/metrics"
"github.com/fluxninja/aperture/pkg/multimatcher"
"github.com/fluxninja/aperture/pkg/notifiers"
"github.com/fluxninja/aperture/pkg/policies/flowcontrol/actuators/concurrency/scheduler"
"github.com/fluxninja/aperture/pkg/policies/flowcontrol/iface"
"github.com/fluxninja/aperture/pkg/policies/flowcontrol/selectors"
"github.com/fluxninja/aperture/pkg/policies/paths"
"github.com/fluxninja/aperture/pkg/status"
)
var (
// FxNameTag is Concurrency Limiter Watcher's Fx Tag.
fxNameTag = config.NameTag("concurrency_limiter_watcher")
// Array of Label Keys for WFQ and Token Bucket Metrics.
metricLabelKeys = []string{metrics.PolicyNameLabel, metrics.PolicyHashLabel, metrics.ComponentIndexLabel}
)
// concurrencyLimiterModule returns the fx options for flowcontrol side pieces of concurrency limiter in the main fx app.
func concurrencyLimiterModule() fx.Option {
return fx.Options(
// Tag the watcher so that other modules can find it.
fx.Provide(
fx.Annotate(
provideWatcher,
fx.ResultTags(fxNameTag),
),
),
fx.Invoke(
fx.Annotate(
setupConcurrencyLimiterFactory,
fx.ParamTags(fxNameTag),
),
),
)
}
// provideWatcher provides pointer to concurrency limiter watcher.
func provideWatcher(
etcdClient *etcdclient.Client,
ai *agentinfo.AgentInfo,
) (notifiers.Watcher, error) {
// Get Agent Group from host info gatherer
agentGroupName := ai.GetAgentGroup()
// Scope the sync to the agent group.
etcdPath := path.Join(paths.ConcurrencyLimiterConfigPath,
paths.AgentGroupPrefix(agentGroupName))
watcher, err := etcdwatcher.NewWatcher(etcdClient, etcdPath)
if err != nil {
return nil, err
}
return watcher, nil
}
type concurrencyLimiterFactory struct {
engineAPI iface.Engine
registry status.Registry
autoTokensFactory *autoTokensFactory
loadActuatorFactory *loadActuatorFactory
// WFQ Metrics.
wfqFlowsGaugeVec *prometheus.GaugeVec
wfqRequestsGaugeVec *prometheus.GaugeVec
// TODO: following will be moved to scheduler.
incomingConcurrencyCounterVec *prometheus.CounterVec
acceptedConcurrencyCounterVec *prometheus.CounterVec
workloadLatencySummaryVec *prometheus.SummaryVec
workloadCounterVec *prometheus.CounterVec
}
// setupConcurrencyLimiterFactory sets up the concurrency limiter module in the main fx app.
func setupConcurrencyLimiterFactory(
watcher notifiers.Watcher,
lifecycle fx.Lifecycle,
e iface.Engine,
statusRegistry status.Registry,
prometheusRegistry *prometheus.Registry,
etcdClient *etcdclient.Client,
ai *agentinfo.AgentInfo,
) error {
agentGroup := ai.GetAgentGroup()
// Create factories
loadActuatorFactory, err := newLoadActuatorFactory(lifecycle, etcdClient, agentGroup, prometheusRegistry)
if err != nil {
return err
}
autoTokensFactory, err := newAutoTokensFactory(lifecycle, etcdClient, agentGroup)
if err != nil {
return err
}
reg := statusRegistry.Child("concurrency_limiter")
conLimiterFactory := &concurrencyLimiterFactory{
engineAPI: e,
autoTokensFactory: autoTokensFactory,
loadActuatorFactory: loadActuatorFactory,
registry: reg,
}
conLimiterFactory.wfqFlowsGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metrics.WFQFlowsMetricName,
Help: "A gauge that tracks the number of flows in the WFQScheduler",
},
metricLabelKeys,
)
conLimiterFactory.wfqRequestsGaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: metrics.WFQRequestsMetricName,
Help: "A gauge that tracks the number of queued requests in the WFQScheduler",
},
metricLabelKeys,
)
conLimiterFactory.incomingConcurrencyCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: metrics.IncomingConcurrencyMetricName,
Help: "A counter measuring incoming concurrency into Scheduler",
},
metricLabelKeys,
)
conLimiterFactory.acceptedConcurrencyCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: metrics.AcceptedConcurrencyMetricName,
Help: "A counter measuring the concurrency admitted by Scheduler",
},
metricLabelKeys,
)
conLimiterFactory.workloadLatencySummaryVec = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: metrics.WorkloadLatencyMetricName,
Help: "Latency summary of workload",
}, []string{
metrics.PolicyNameLabel, metrics.PolicyHashLabel, metrics.ComponentIndexLabel,
metrics.WorkloadIndexLabel,
})
conLimiterFactory.workloadCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: metrics.WorkloadCounterMetricName,
Help: "Counter of workload requests",
}, []string{
metrics.PolicyNameLabel, metrics.PolicyHashLabel, metrics.ComponentIndexLabel,
metrics.DecisionTypeLabel,
metrics.WorkloadIndexLabel,
})
fxDriver := ¬ifiers.FxDriver{
FxOptionsFuncs: []notifiers.FxOptionsFunc{conLimiterFactory.newConcurrencyLimiterOptions},
UnmarshalPrefixNotifier: notifiers.UnmarshalPrefixNotifier{
GetUnmarshallerFunc: config.NewProtobufUnmarshaller,
},
StatusRegistry: reg,
PrometheusRegistry: prometheusRegistry,
}
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
var merr error
err := prometheusRegistry.Register(conLimiterFactory.wfqFlowsGaugeVec)
if err != nil {
merr = multierr.Append(merr, err)
}
err = prometheusRegistry.Register(conLimiterFactory.wfqRequestsGaugeVec)
if err != nil {
merr = multierr.Append(merr, err)
}
err = prometheusRegistry.Register(conLimiterFactory.incomingConcurrencyCounterVec)
if err != nil {
merr = multierr.Append(merr, err)
}
err = prometheusRegistry.Register(conLimiterFactory.acceptedConcurrencyCounterVec)
if err != nil {
merr = multierr.Append(merr, err)
}
err = prometheusRegistry.Register(conLimiterFactory.workloadLatencySummaryVec)
if err != nil {
merr = multierr.Append(merr, err)
}
err = prometheusRegistry.Register(conLimiterFactory.workloadCounterVec)
if err != nil {
merr = multierr.Append(merr, err)
}
return merr
},
OnStop: func(_ context.Context) error {
var merr error
if !prometheusRegistry.Unregister(conLimiterFactory.wfqFlowsGaugeVec) {
err := fmt.Errorf("failed to unregister wfq_flows metric")
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(conLimiterFactory.wfqRequestsGaugeVec) {
err := fmt.Errorf("failed to unregister wfq_requests metric")
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(conLimiterFactory.incomingConcurrencyCounterVec) {
err := fmt.Errorf("failed to unregister incoming_concurrency metric")
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(conLimiterFactory.acceptedConcurrencyCounterVec) {
err := fmt.Errorf("failed to unregister accepted_concurrency metric")
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(conLimiterFactory.workloadLatencySummaryVec) {
err := fmt.Errorf("failed to unregister workload_latency_ms metric")
merr = multierr.Append(merr, err)
}
if !prometheusRegistry.Unregister(conLimiterFactory.workloadCounterVec) {
err := fmt.Errorf("failed to unregister workload_counter metric")
merr = multierr.Append(merr, err)
}
return merr
},
})
notifiers.WatcherLifecycle(lifecycle, watcher, []notifiers.PrefixNotifier{fxDriver})
return nil
}
// multiMatchResult is used as return value of PolicyConfigAPI.GetMatches.
type multiMatchResult struct {
matchedWorkloads map[int]*policylangv1.Scheduler_WorkloadParameters
}
// multiMatcher is MultiMatcher instantiation used in this package.
type multiMatcher = multimatcher.MultiMatcher[int, multiMatchResult]
// newConcurrencyLimiterOptions returns fx options for the concurrency limiter fx app.
func (conLimiterFactory *concurrencyLimiterFactory) newConcurrencyLimiterOptions(
key notifiers.Key,
unmarshaller config.Unmarshaller,
reg status.Registry,
) (fx.Option, error) {
logger := conLimiterFactory.registry.GetLogger()
wrapperMessage := &policysyncv1.ConcurrencyLimiterWrapper{}
err := unmarshaller.Unmarshal(wrapperMessage)
concurrencyLimiterMessage := wrapperMessage.ConcurrencyLimiter
if err != nil || concurrencyLimiterMessage == nil {
reg.SetStatus(status.NewStatus(nil, err))
logger.Warn().Err(err).Msg("Failed to unmarshal concurrency limiter config wrapper")
return fx.Options(), err
}
// Scheduler config
schedulerMsg := concurrencyLimiterMessage.Scheduler
if schedulerMsg == nil {
err = fmt.Errorf("no scheduler specified")
reg.SetStatus(status.NewStatus(nil, err))
logger.Warn().Err(err).Msg("Failed to unmarshal scheduler")
return fx.Options(), err
}
mm := multimatcher.New[int, multiMatchResult]()
// Loop through the workloads
for workloadIndex, workloadProto := range schedulerMsg.Workloads {
labelMatcher, err := selectors.MMExprFromLabelMatcher(workloadProto.GetLabelMatcher())
if err != nil {
return fx.Options(), err
}
wm := &workloadMatcher{
workloadIndex: workloadIndex,
workloadProto: workloadProto,
}
err = mm.AddEntry(workloadIndex, labelMatcher, wm.matchCallback)
if err != nil {
return fx.Options(), err
}
}
conLimiter := &concurrencyLimiter{
Component: wrapperMessage.GetCommonAttributes(),
concurrencyLimiterMsg: concurrencyLimiterMessage,
registry: reg,
concurrencyLimiterFactory: conLimiterFactory,
workloadMultiMatcher: mm,
defaultWorkloadParametersMsg: schedulerMsg.DefaultWorkloadParameters,
schedulerMsg: schedulerMsg,
}
return fx.Options(
fx.Invoke(
conLimiter.setup,
),
), nil
}
type workloadMatcher struct {
workloadProto *policylangv1.Scheduler_Workload
workloadIndex int
}
func (wm *workloadMatcher) matchCallback(mmr multiMatchResult) multiMatchResult {
// mmr.matchedWorkloads is nil on first match.
if mmr.matchedWorkloads == nil {
mmr.matchedWorkloads = make(map[int]*policylangv1.Scheduler_WorkloadParameters)
}
mmr.matchedWorkloads[wm.workloadIndex] = wm.workloadProto.GetWorkloadParameters()
return mmr
}
// concurrencyLimiter implements concurrency limiter on the flowcontrol side.
type concurrencyLimiter struct {
iface.Component
scheduler scheduler.Scheduler
registry status.Registry
incomingConcurrencyCounter prometheus.Counter
acceptedConcurrencyCounter prometheus.Counter
concurrencyLimiterMsg *policylangv1.ConcurrencyLimiter
concurrencyLimiterFactory *concurrencyLimiterFactory
autoTokens *autoTokens
workloadMultiMatcher *multiMatcher
defaultWorkloadParametersMsg *policylangv1.Scheduler_WorkloadParameters
schedulerMsg *policylangv1.Scheduler
}
// Make sure ConcurrencyLimiter implements the iface.ConcurrencyLimiter.
var _ iface.Limiter = &concurrencyLimiter{}
func (conLimiter *concurrencyLimiter) setup(lifecycle fx.Lifecycle) error {
// Factories
conLimiterFactory := conLimiter.concurrencyLimiterFactory
loadActuatorFactory := conLimiterFactory.loadActuatorFactory
autoTokensFactory := conLimiterFactory.autoTokensFactory
// Form metric labels
metricLabels := make(prometheus.Labels)
metricLabels[metrics.PolicyNameLabel] = conLimiter.GetPolicyName()
metricLabels[metrics.PolicyHashLabel] = conLimiter.GetPolicyHash()
metricLabels[metrics.ComponentIndexLabel] = strconv.FormatInt(conLimiter.GetComponentIndex(), 10)
// Create sub components.
clock := clockwork.NewRealClock()
loadActuator, err := loadActuatorFactory.newLoadActuator(conLimiter.concurrencyLimiterMsg.GetLoadActuator(), conLimiter, conLimiter.registry, clock, lifecycle, metricLabels)
if err != nil {
return err
}
if conLimiter.schedulerMsg.AutoTokens {
autoTokens, err := autoTokensFactory.newAutoTokens(
conLimiter.GetPolicyName(), conLimiter.GetPolicyHash(),
lifecycle, conLimiter.GetComponentIndex(), conLimiter.registry)
if err != nil {
return err
}
conLimiter.autoTokens = autoTokens
}
engineAPI := conLimiterFactory.engineAPI
wfqFlowsGaugeVec := conLimiterFactory.wfqFlowsGaugeVec
wfqRequestsGaugeVec := conLimiterFactory.wfqRequestsGaugeVec
incomingConcurrencyCounterVec := conLimiterFactory.incomingConcurrencyCounterVec
acceptedConcurrencyCounterVec := conLimiterFactory.acceptedConcurrencyCounterVec
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
retErr := func(err error) error {
conLimiter.registry.SetStatus(status.NewStatus(nil, err))
return err
}
wfqFlowsGauge, err := wfqFlowsGaugeVec.GetMetricWith(metricLabels)
if err != nil {
return errors.Wrap(err, "failed to get wfq flows gauge")
}
wfqRequestsGauge, err := wfqRequestsGaugeVec.GetMetricWith(metricLabels)
if err != nil {
return errors.Wrap(err, "failed to get wfq requests gauge")
}
wfqMetrics := &scheduler.WFQMetrics{
FlowsGauge: wfqFlowsGauge,
HeapRequestsGauge: wfqRequestsGauge,
}
// setup scheduler
conLimiter.scheduler = scheduler.NewWFQScheduler(loadActuator.tokenBucketLoadMultiplier, clock, wfqMetrics)
incomingConcurrencyCounter, err := incomingConcurrencyCounterVec.GetMetricWith(metricLabels)
if err != nil {
return err
}
conLimiter.incomingConcurrencyCounter = incomingConcurrencyCounter
acceptedConcurrencyCounter, err := acceptedConcurrencyCounterVec.GetMetricWith(metricLabels)
if err != nil {
return err
}
conLimiter.acceptedConcurrencyCounter = acceptedConcurrencyCounter
err = engineAPI.RegisterConcurrencyLimiter(conLimiter)
if err != nil {
return retErr(err)
}
return retErr(nil)
},
OnStop: func(context.Context) error {
var errMulti error
err := engineAPI.UnregisterConcurrencyLimiter(conLimiter)
if err != nil {
errMulti = multierr.Append(errMulti, err)
}
// Remove metrics from metric vectors
deleted := wfqFlowsGaugeVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete wfq_flows gauge from its metric vector"))
}
deleted = wfqRequestsGaugeVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete wfq_requests gauge from its metric vector"))
}
deleted = incomingConcurrencyCounterVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete incoming_concurrency counter from its metric vector"))
}
deleted = acceptedConcurrencyCounterVec.Delete(metricLabels)
if !deleted {
errMulti = multierr.Append(errMulti, errors.New("failed to delete accepted_concurrency counter from its metric vector"))
}
deletedCount := conLimiter.concurrencyLimiterFactory.workloadLatencySummaryVec.DeletePartialMatch(metricLabels)
if deletedCount == 0 {
errMulti = multierr.Append(errMulti, errors.New("failed to delete workload_latency_ms summary from its metric vector"))
}
deletedCount = conLimiter.concurrencyLimiterFactory.workloadCounterVec.DeletePartialMatch(metricLabels)
if deletedCount == 0 {
errMulti = multierr.Append(errMulti, errors.New("failed to delete workload_requests_total counter from its metric vector"))
}
conLimiter.registry.SetStatus(status.NewStatus(nil, errMulti))
return errMulti
},
})
return nil
}
// GetSelector returns selector.
func (conLimiter *concurrencyLimiter) GetSelector() *policylangv1.Selector {
return conLimiter.concurrencyLimiterMsg.GetSelector()
}
// RunLimiter processes a single flow by concurrency limiter in a blocking manner.
//
// Context is used to ensure that requests are not scheduled for longer than its deadline allows.
func (conLimiter *concurrencyLimiter) RunLimiter(ctx context.Context, labels map[string]string) *flowcontrolv1.LimiterDecision {
var matchedWorkloadProto *policylangv1.Scheduler_WorkloadParameters
var matchedWorkloadIndex string
// match labels against conLimiter.workloadMultiMatcher
mmr := conLimiter.workloadMultiMatcher.Match(multimatcher.Labels(labels))
// if at least one match, return workload with lowest index
if len(mmr.matchedWorkloads) > 0 {
// select the smallest workloadIndex
smallestWorkloadIndex := math.MaxInt32
for workloadIndex := range mmr.matchedWorkloads {
if workloadIndex < smallestWorkloadIndex {
smallestWorkloadIndex = workloadIndex
}
}
matchedWorkloadProto = mmr.matchedWorkloads[smallestWorkloadIndex]
matchedWorkloadIndex = strconv.Itoa(smallestWorkloadIndex)
} else {
// no match, return default workload
matchedWorkloadProto = conLimiter.defaultWorkloadParametersMsg
matchedWorkloadIndex = metrics.DefaultWorkloadIndex
}
fairnessLabel := "workload:" + matchedWorkloadIndex
if val, ok := labels[matchedWorkloadProto.FairnessKey]; ok {
fairnessLabel = fairnessLabel + "," + val
}
// Lookup tokens for the workload
var tokens uint64
if conLimiter.schedulerMsg.AutoTokens {
tokensAuto, ok := conLimiter.autoTokens.GetTokensForWorkload(matchedWorkloadIndex)
if !ok {
// default to 1 if auto tokens not found
tokens = 1
} else {
tokens = tokensAuto
}
} else {
tokens = matchedWorkloadProto.Tokens
}
// timeout is tokens(which is in milliseconds) * conLimiter.schedulerProto.TimeoutFactor(float64)
timeout := time.Duration(float64(tokens)*conLimiter.schedulerMsg.TimeoutFactor) * time.Millisecond
if timeout > conLimiter.schedulerMsg.MaxTimeout.AsDuration() {
timeout = conLimiter.schedulerMsg.MaxTimeout.AsDuration()
}
if clientDeadline, hasDeadline := ctx.Deadline(); hasDeadline {
// The clientDeadline is calculated based on client's timeout, passed
// as grpc-timeout. Our goal is for the response to be received by the
// client before its deadline passes (otherwise we risk fail-open on
// timeout). To allow some headroom for transmitting the response to
// the client, we set an "internal" deadline to a bit before client's
// deadline, subtracting:
// * 2 * 1ms to account for deadline inaccuracies (observed
// that Deadline() - Now() delta might end up longer than
// grpc-timeout (!), usually within 1ms),
// * 1ms for response overhead,
// * 7ms so that we don't always operate on the edge of the time budget.
clientTimeout := time.Until(clientDeadline)
internalTimeout := clientTimeout - 10*time.Millisecond
if internalTimeout < timeout {
timeout = internalTimeout
}
if timeout < 0 {
timeout = 0
}
}
reqContext := scheduler.RequestContext{
FairnessLabel: fairnessLabel,
Priority: uint8(matchedWorkloadProto.Priority),
Timeout: timeout,
Tokens: tokens,
}
accepted := conLimiter.scheduler.Schedule(reqContext)
// update concurrency metrics and decisionType
conLimiter.incomingConcurrencyCounter.Add(float64(reqContext.Tokens))
if accepted {
conLimiter.acceptedConcurrencyCounter.Add(float64(reqContext.Tokens))
}
return &flowcontrolv1.LimiterDecision{
PolicyName: conLimiter.GetPolicyName(),
PolicyHash: conLimiter.GetPolicyHash(),
ComponentIndex: conLimiter.GetComponentIndex(),
Dropped: !accepted,
Details: &flowcontrolv1.LimiterDecision_ConcurrencyLimiterInfo_{
ConcurrencyLimiterInfo: &flowcontrolv1.LimiterDecision_ConcurrencyLimiterInfo{
WorkloadIndex: matchedWorkloadIndex,
},
},
}
}
// GetLimiterID returns the limiter ID.
func (conLimiter *concurrencyLimiter) GetLimiterID() iface.LimiterID {
// TODO: move this to limiter base.
return iface.LimiterID{
PolicyName: conLimiter.GetPolicyName(),
PolicyHash: conLimiter.GetPolicyHash(),
ComponentIndex: conLimiter.GetComponentIndex(),
}
}
// GetLatencyObserver returns histogram for specific workload.
func (conLimiter *concurrencyLimiter) GetLatencyObserver(labels map[string]string) prometheus.Observer {
latencySummary, err := conLimiter.concurrencyLimiterFactory.workloadLatencySummaryVec.GetMetricWith(labels)
if err != nil {
log.Warn().Err(err).Msg("Getting latency histogram")
return nil
}
return latencySummary
}
// GetRequestCounter returns request counter for specific workload.
func (conLimiter *concurrencyLimiter) GetRequestCounter(labels map[string]string) prometheus.Counter {
counter, err := conLimiter.concurrencyLimiterFactory.workloadCounterVec.GetMetricWith(labels)
if err != nil {
log.Warn().Err(err).Msg("Getting counter")
return nil
}
return counter
}