forked from benthosdev/benthos
/
rate_limit.go
131 lines (111 loc) · 3.58 KB
/
rate_limit.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
package processor
import (
"fmt"
"sync"
"time"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
"github.com/Jeffail/benthos/v3/lib/x/docs"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeRateLimit] = TypeSpec{
constructor: NewRateLimit,
Summary: `
Throttles the throughput of a pipeline according to a specified
` + "[`rate_limit`](/docs/components/rate_limits/about)" + ` resource. Rate limits are
shared across components and therefore apply globally to all processing
pipelines.`,
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("resource", "The target [`rate_limit` resource](/docs/components/rate_limits/about)."),
},
}
}
//------------------------------------------------------------------------------
// RateLimitConfig contains configuration fields for the RateLimit processor.
type RateLimitConfig struct {
Resource string `json:"resource" yaml:"resource"`
}
// NewRateLimitConfig returns a RateLimitConfig with default values.
func NewRateLimitConfig() RateLimitConfig {
return RateLimitConfig{
Resource: "",
}
}
//------------------------------------------------------------------------------
// RateLimit is a processor that performs an RateLimit request using the message as the
// request body, and returns the response.
type RateLimit struct {
rl types.RateLimit
log log.Modular
mCount metrics.StatCounter
mRateLimited metrics.StatCounter
mErr metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
closeChan chan struct{}
closeOnce sync.Once
}
// NewRateLimit returns a RateLimit processor.
func NewRateLimit(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
rl, err := mgr.GetRateLimit(conf.RateLimit.Resource)
if err != nil {
return nil, fmt.Errorf("failed to obtain rate limit resource '%v': %v", conf.RateLimit.Resource, err)
}
r := &RateLimit{
rl: rl,
log: log,
mCount: stats.GetCounter("count"),
mRateLimited: stats.GetCounter("rate.limited"),
mErr: stats.GetCounter("error"),
mSent: stats.GetCounter("sent"),
mBatchSent: stats.GetCounter("batch.sent"),
closeChan: make(chan struct{}),
}
return r, nil
}
//------------------------------------------------------------------------------
// ProcessMessage applies the processor to a message, either creating >0
// resulting messages or a response to be sent back to the message source.
func (r *RateLimit) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
r.mCount.Incr(1)
msg.Iter(func(i int, p types.Part) error {
waitFor, err := r.rl.Access()
for err != nil || waitFor > 0 {
if err == types.ErrTypeClosed {
return err
}
if err != nil {
r.mErr.Incr(1)
r.log.Errorf("Failed to access rate limit: %v\n", err)
waitFor = time.Second
} else {
r.mRateLimited.Incr(1)
}
select {
case <-time.After(waitFor):
case <-r.closeChan:
return types.ErrTypeClosed
}
waitFor, err = r.rl.Access()
}
return err
})
r.mBatchSent.Incr(1)
r.mSent.Incr(int64(msg.Len()))
return []types.Message{msg}, nil
}
// CloseAsync shuts down the processor and stops processing requests.
func (r *RateLimit) CloseAsync() {
r.closeOnce.Do(func() {
close(r.closeChan)
})
}
// WaitForClose blocks until the processor has closed down.
func (r *RateLimit) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------