forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"http": { | ||
"address": "0.0.0.0:4195", | ||
"read_timeout_ms": 5000, | ||
"root_path": "/benthos", | ||
"debug_endpoints": false | ||
}, | ||
"input": { | ||
"type": "stdin", | ||
"stdin": { | ||
"delimiter": "", | ||
"max_buffer": 1000000, | ||
"multipart": false | ||
} | ||
}, | ||
"buffer": { | ||
"type": "none", | ||
"none": {} | ||
}, | ||
"pipeline": { | ||
"processors": [ | ||
{ | ||
"type": "throttle", | ||
"throttle": { | ||
"period": "100us" | ||
} | ||
} | ||
], | ||
"threads": 1 | ||
}, | ||
"output": { | ||
"type": "stdout", | ||
"stdout": { | ||
"delimiter": "" | ||
} | ||
}, | ||
"resources": { | ||
"caches": {}, | ||
"conditions": {} | ||
}, | ||
"logger": { | ||
"prefix": "benthos", | ||
"level": "INFO", | ||
"add_timestamp": true, | ||
"json_format": true | ||
}, | ||
"metrics": { | ||
"type": "http_server", | ||
"prefix": "benthos", | ||
"http_server": {}, | ||
"prometheus": {}, | ||
"statsd": { | ||
"address": "localhost:4040", | ||
"flush_period": "100ms", | ||
"max_packet_size": 1440, | ||
"network": "udp" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This file was auto generated by benthos_config_gen. | ||
http: | ||
address: 0.0.0.0:4195 | ||
read_timeout_ms: 5000 | ||
root_path: /benthos | ||
debug_endpoints: false | ||
input: | ||
type: stdin | ||
stdin: | ||
delimiter: "" | ||
max_buffer: 1e+06 | ||
multipart: false | ||
buffer: | ||
type: none | ||
none: {} | ||
pipeline: | ||
processors: | ||
- type: throttle | ||
throttle: | ||
period: 100us | ||
threads: 1 | ||
output: | ||
type: stdout | ||
stdout: | ||
delimiter: "" | ||
resources: | ||
caches: {} | ||
conditions: {} | ||
logger: | ||
prefix: benthos | ||
level: INFO | ||
add_timestamp: true | ||
json_format: true | ||
metrics: | ||
type: http_server | ||
prefix: benthos | ||
http_server: {} | ||
prometheus: {} | ||
statsd: | ||
address: localhost:4040 | ||
flush_period: 100ms | ||
max_packet_size: 1440 | ||
network: udp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright (c) 2018 Ashley Jeffs | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package processor | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Jeffail/benthos/lib/log" | ||
"github.com/Jeffail/benthos/lib/metrics" | ||
"github.com/Jeffail/benthos/lib/types" | ||
) | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
func init() { | ||
Constructors[TypeThrottle] = TypeSpec{ | ||
constructor: NewThrottle, | ||
description: ` | ||
Throttles the throughput of a pipeline to a maximum of one message batch per | ||
period. This throttle is per processing pipeline, and therefore four threads | ||
each with a throttle would result in four times the rate specified. | ||
The period should be specified as a time duration string. For example, '1s' | ||
would be 1 second, '10ms' would be 10 milliseconds, etc.`, | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
// ThrottleConfig contains configuration fields for the Throttle processor. | ||
type ThrottleConfig struct { | ||
Period string `json:"period" yaml:"period"` | ||
} | ||
|
||
// NewThrottleConfig returns a ThrottleConfig with default values. | ||
func NewThrottleConfig() ThrottleConfig { | ||
return ThrottleConfig{ | ||
Period: "100us", | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
// Throttle is a processor that limits the stream of a pipeline to one message | ||
// batch per period specified. | ||
type Throttle struct { | ||
conf Config | ||
log log.Modular | ||
stats metrics.Type | ||
|
||
duration time.Duration | ||
lastBatch time.Time | ||
|
||
mCount metrics.StatCounter | ||
mSent metrics.StatCounter | ||
mSentParts metrics.StatCounter | ||
} | ||
|
||
// NewThrottle returns a Throttle processor. | ||
func NewThrottle( | ||
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type, | ||
) (Type, error) { | ||
t := &Throttle{ | ||
conf: conf, | ||
log: log.NewModule(".processor.throttle"), | ||
stats: stats, | ||
|
||
mCount: stats.GetCounter("processor.throttle.count"), | ||
mSent: stats.GetCounter("processor.throttle.sent"), | ||
mSentParts: stats.GetCounter("processor.throttle.parts.sent"), | ||
} | ||
|
||
var err error | ||
if t.duration, err = time.ParseDuration(conf.Throttle.Period); err != nil { | ||
return nil, fmt.Errorf("failed to parse period: %v", err) | ||
} | ||
|
||
return t, 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 (m *Throttle) ProcessMessage(msg types.Message) ([]types.Message, types.Response) { | ||
m.mCount.Incr(1) | ||
|
||
if since := time.Since(m.lastBatch); m.duration > since { | ||
time.Sleep(m.duration - since) | ||
} | ||
|
||
m.lastBatch = time.Now() | ||
|
||
m.mSent.Incr(1) | ||
m.mSentParts.Incr(int64(msg.Len())) | ||
msgs := [1]types.Message{msg} | ||
return msgs[:], nil | ||
} | ||
|
||
//------------------------------------------------------------------------------ |
Oops, something went wrong.