forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
185 lines (156 loc) · 5.61 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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 main
import (
"context"
_ "net/http/pprof"
"runtime/pprof"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Jeffail/benthos/lib/api"
"github.com/Jeffail/benthos/lib/input"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util"
"github.com/Jeffail/benthos/lib/util/service"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
"github.com/Jeffail/benthos/lib/util/test"
)
//------------------------------------------------------------------------------
// Config is the benthos configuration struct.
type Config struct {
ReportPeriodMS int `json:"report_period_ms" yaml:"report_period_ms"`
HTTP api.Config `json:"http" yaml:"http"`
Input input.Config `json:"input" yaml:"input"`
Logger log.LoggerConfig `json:"logger" yaml:"logger"`
Metrics metrics.Config `json:"metrics" yaml:"metrics"`
SystemCloseTimeoutMS int `json:"sys_exit_timeout_ms" yaml:"sys_exit_timeout_ms"`
}
// NewConfig returns a new configuration with default values.
func NewConfig() Config {
metricsConf := metrics.NewConfig()
metricsConf.Prefix = "benthos"
return Config{
ReportPeriodMS: 60000,
HTTP: api.NewConfig(),
Input: input.NewConfig(),
Logger: log.NewLoggerConfig(),
Metrics: metricsConf,
SystemCloseTimeoutMS: 20000,
}
}
//------------------------------------------------------------------------------
// bootstrap reads cmd args and either parses and config file or prints helper
// text and exits.
func bootstrap() Config {
config := NewConfig()
// Load configuration etc
if !service.Bootstrap(&config) {
os.Exit(0)
}
return config
}
// createPipeline creates a pipeline based on the supplied configuration file,
// and return a closable pool of pipeline objects, a channel indicating that all
// inputs and outputs have seized, or an error.
func createPipeline(
config Config, mgr types.Manager, logger log.Modular, stats metrics.Type,
) (*util.ClosablePool, error) {
pool := util.NewClosablePool()
// Create our input pipe
inputPipe, err := input.New(config.Input, mgr, logger, stats)
if err != nil {
logger.Errorf("Input error (%s): %v\n", config.Input.Type, err)
return nil, err
}
pool.Add(1, inputPipe)
// Create our benchmarking output pipe
outputPipe := test.NewBenchOutput(
time.Duration(config.ReportPeriodMS)*time.Millisecond, logger, stats,
)
pool.Add(10, outputPipe)
outputPipe.StartReceiving(inputPipe.TransactionChan())
return pool, nil
}
func main() {
// Bootstrap by reading cmd flags and configuration file
config := bootstrap()
// Logging and stats aggregation
logger := log.NewLogger(os.Stderr, config.Logger)
logger.Infoln("Launching a benthos_bench instance, use CTRL+C to close.")
// Create our metrics type
stats, err := metrics.New(config.Metrics)
if err != nil {
logger.Errorf("Metrics error: %v\n", err)
return
}
defer stats.Close()
httpServer := api.New(service.Version, service.DateBuilt, config.HTTP, config, logger, stats)
pool, err := createPipeline(config, httpServer, logger, stats)
if err != nil {
logger.Errorf("Service closing due to: %v\n", err)
return
}
httpServerClosedChan := make(chan struct{})
go func() {
logger.Infof(
"Listening for HTTP requests at: %v\n",
"http://"+config.HTTP.Address,
)
httpErr := httpServer.ListenAndServe()
if httpErr != nil && httpErr != http.ErrServerClosed {
logger.Errorf("HTTP Server error: %v\n", httpErr)
}
close(httpServerClosedChan)
}()
// Defer ordered pool clean up.
defer func() {
tout := time.Millisecond * time.Duration(config.SystemCloseTimeoutMS)
go func() {
httpServer.Shutdown(context.Background())
select {
case <-httpServerClosedChan:
case <-time.After(tout / 2):
logger.Warnln("Service failed to close HTTP server gracefully in time.")
}
}()
if err := pool.Close(tout); err != nil {
logger.Warnln(
"Service failed to close using ordered tiers, you may receive a duplicate " +
"message on the next service start.",
)
if config.Logger.LogLevel == "DEBUG" {
pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
}
os.Exit(1)
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
// Wait for termination signal
select {
case <-sigChan:
logger.Infoln("Received SIGTERM, the service is closing.")
}
}
//------------------------------------------------------------------------------