-
Notifications
You must be signed in to change notification settings - Fork 50
/
middleware.go
49 lines (39 loc) · 1013 Bytes
/
middleware.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
package limit
import "golang.org/x/net/context"
type Middleware interface {
OnTake(ctx context.Context, i Invocation)
OnRelease(ctx context.Context, i Invocation)
OnError(ctx context.Context, i Invocation)
OnThrottle(ctx context.Context, i Invocation)
}
type MiddlewareFactory func() Middleware
type chainedMiddleware struct {
m []Middleware
}
func (c chainedMiddleware) OnTake(ctx context.Context, i Invocation) {
for _, m := range c.m {
m.OnTake(ctx, i)
}
}
func (c chainedMiddleware) OnRelease(ctx context.Context, i Invocation) {
for _, m := range c.m {
m.OnRelease(ctx, i)
}
}
func (c chainedMiddleware) OnError(ctx context.Context, i Invocation) {
for _, m := range c.m {
m.OnError(ctx, i)
}
}
func (c chainedMiddleware) OnThrottle(ctx context.Context, i Invocation) {
for _, m := range c.m {
m.OnThrottle(ctx, i)
}
}
func ChainMiddleware(fs ...MiddlewareFactory) Middleware {
var m []Middleware
for _, f := range fs {
m = append(m, f())
}
return chainedMiddleware{m}
}