-
Notifications
You must be signed in to change notification settings - Fork 50
/
limiter.go
44 lines (33 loc) · 1.11 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
// Package limit implements various rate limiters. In the current stage (2023-03-13) limiters can only be used when
// for waiting until request limits permit operations. In the future, we might add support for limiters that can be
// used to limit incoming traffic for services.
//
// Because of limitations in the testing process of the rate limiters be aware that the limiter package is currently
// BETA and can change anytime.
package limit
import "context"
type limitPkgCtxKey string
type Factory func() (Limiter, error)
type Limiter interface {
Wait(ctx context.Context, prefix string) error
}
type LimiterWithMiddleware interface {
Limiter
WithMiddleware(...MiddlewareFactory)
}
func newMiddlewareEmbeddable() *middlewareEmbeddable {
return &middlewareEmbeddable{middleware: ChainMiddleware()}
}
type middlewareEmbeddable struct {
middleware Middleware
}
func (d *middlewareEmbeddable) WithMiddleware(m ...MiddlewareFactory) {
d.middleware = ChainMiddleware(m...)
}
type unlimited struct{}
func NewUnlimited() Limiter {
return &unlimited{}
}
func (u unlimited) Wait(context.Context, string) error {
return nil
}