forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.go
179 lines (159 loc) · 5.06 KB
/
dynamic.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
package input
import (
"encoding/json"
"fmt"
"path"
"sync"
"time"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/internal/interop"
"github.com/dafanshu/benthos/v3/lib/api"
"github.com/dafanshu/benthos/v3/lib/broker"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/types"
"gopkg.in/yaml.v3"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeDynamic] = TypeSpec{
constructor: func(
hasBatchProc bool,
conf Config,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
pipelines ...types.PipelineConstructorFunc,
) (Type, error) {
_, pipelines = appendProcessorsFromConfigBatchAware(hasBatchProc, conf, mgr, log, stats, pipelines...)
return NewDynamic(conf, mgr, log, stats, pipelines...)
},
Summary: `
A special broker type where the inputs are identified by unique labels and can
be created, changed and removed during runtime via a REST HTTP interface.`,
Description: `
To GET a JSON map of input identifiers with their current uptimes use the
` + "`/inputs`" + ` endpoint.
To perform CRUD actions on the inputs themselves use POST, DELETE, and GET
methods on the ` + "`/inputs/{input_id}`" + ` endpoint. When using POST the body
of the request should be a YAML configuration for the input, if the input
already exists it will be changed.`,
Categories: []Category{
CategoryUtility,
},
FieldSpecs: docs.FieldSpecs{
docs.FieldCommon("inputs", "A map of inputs to statically create.").Map().HasType(docs.FieldTypeInput),
docs.FieldCommon("prefix", "A path prefix for HTTP endpoints that are registered."),
docs.FieldCommon("timeout", "The server side timeout of HTTP requests."),
},
}
}
//------------------------------------------------------------------------------
// DynamicConfig contains configuration for the Dynamic input type.
type DynamicConfig struct {
Inputs map[string]Config `json:"inputs" yaml:"inputs"`
Prefix string `json:"prefix" yaml:"prefix"`
Timeout string `json:"timeout" yaml:"timeout"`
}
// NewDynamicConfig creates a new DynamicConfig with default values.
func NewDynamicConfig() DynamicConfig {
return DynamicConfig{
Inputs: map[string]Config{},
Prefix: "",
Timeout: "5s",
}
}
//------------------------------------------------------------------------------
// NewDynamic creates a new Dynamic input type.
func NewDynamic(
conf Config,
mgr types.Manager,
log log.Modular,
stats metrics.Type,
pipelines ...types.PipelineConstructorFunc,
) (Type, error) {
dynAPI := api.NewDynamic()
inputs := map[string]broker.DynamicInput{}
for k, v := range conf.Dynamic.Inputs {
newInput, err := New(v, mgr, log, stats, pipelines...)
if err != nil {
return nil, err
}
inputs[k] = newInput
}
var timeout time.Duration
if tout := conf.Dynamic.Timeout; len(tout) > 0 {
var err error
if timeout, err = time.ParseDuration(tout); err != nil {
return nil, fmt.Errorf("failed to parse timeout string: %v", err)
}
}
inputConfigs := conf.Dynamic.Inputs
inputConfigsMut := sync.RWMutex{}
fanIn, err := broker.NewDynamicFanIn(
inputs, log, stats,
broker.OptDynamicFanInSetOnAdd(func(l string) {
inputConfigsMut.Lock()
defer inputConfigsMut.Unlock()
uConf, exists := inputConfigs[l]
if !exists {
return
}
sConf, bErr := SanitiseConfig(uConf)
if bErr != nil {
log.Errorf("Failed to sanitise config: %v\n", bErr)
}
confBytes, _ := json.Marshal(sConf)
dynAPI.Started(l, confBytes)
delete(inputConfigs, l)
}),
broker.OptDynamicFanInSetOnRemove(func(l string) {
dynAPI.Stopped(l)
}),
)
if err != nil {
return nil, err
}
dynAPI.OnUpdate(func(id string, c []byte) error {
newConf := NewConfig()
if err := yaml.Unmarshal(c, &newConf); err != nil {
return err
}
iMgr, iLog, iStats := interop.LabelChild(fmt.Sprintf("dynamic.inputs.%v", id), mgr, log, stats)
iStats = metrics.Combine(stats, iStats)
newInput, err := New(newConf, iMgr, iLog, iStats, pipelines...)
if err != nil {
return err
}
inputConfigsMut.Lock()
inputConfigs[id] = newConf
inputConfigsMut.Unlock()
if err = fanIn.SetInput(id, newInput, timeout); err != nil {
log.Errorf("Failed to set input '%v': %v", id, err)
inputConfigsMut.Lock()
delete(inputConfigs, id)
inputConfigsMut.Unlock()
}
return err
})
dynAPI.OnDelete(func(id string) error {
err := fanIn.SetInput(id, nil, timeout)
if err != nil {
log.Errorf("Failed to close input '%v': %v", id, err)
}
return err
})
mgr.RegisterEndpoint(
path.Join(conf.Dynamic.Prefix, "/inputs/{id}"),
"Perform CRUD operations on the configuration of dynamic inputs. For"+
" more information read the `dynamic` input type documentation.",
dynAPI.HandleCRUD,
)
mgr.RegisterEndpoint(
path.Join(conf.Dynamic.Prefix, "/inputs"),
"Get a map of running input identifiers with their current uptimes.",
dynAPI.HandleList,
)
return fanIn, nil
}
//------------------------------------------------------------------------------