-
Notifications
You must be signed in to change notification settings - Fork 24
/
limiter.go
47 lines (38 loc) · 1.42 KB
/
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
package iface
import (
"context"
"github.com/prometheus/client_golang/prometheus"
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"
)
//go:generate mockgen -source=limiter.go -destination=../../mocks/mock_limiter.go -package=mocks
// LimiterID is the ID of the Limiter.
type LimiterID struct {
PolicyName string
PolicyHash string
ComponentID string
}
// String function returns the LimiterID as a string.
func (limiterID LimiterID) String() string {
return "policy_name-" + limiterID.PolicyName + "-component_id-" + limiterID.ComponentID + "-policy_hash-" + limiterID.PolicyHash
}
// Limiter interface.
// Lifetime of this interface is per policy/component.
type Limiter interface {
GetPolicyName() string
GetFlowSelector() *policylangv1.FlowSelector
RunLimiter(ctx context.Context, labels map[string]string) *flowcontrolv1.LimiterDecision
GetLimiterID() LimiterID
}
// RateLimiter interface.
type RateLimiter interface {
Limiter
TakeN(labels map[string]string, count int) (label string, ok bool, remaining int, current int)
GetRequestCounter(labels map[string]string) prometheus.Counter
}
// ConcurrencyLimiter interface.
type ConcurrencyLimiter interface {
Limiter
GetLatencyObserver(labels map[string]string) prometheus.Observer
GetRequestCounter(labels map[string]string) prometheus.Counter
}