-
Notifications
You must be signed in to change notification settings - Fork 153
/
from.go
214 lines (181 loc) · 5.47 KB
/
from.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Package socket implements a source that gets input from a socket connection and produces tables given a decoder.
// This is a good candidate for streaming use cases. For now, it produces a single table for everything
// that it receives from the start to the end of the connection.
package socket
import (
"context"
"io"
"net"
neturl "net/url"
"strings"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/csv"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/line"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
)
const FromSocketKind = "fromSocket"
type FromSocketOpSpec struct {
URL string `json:"url"`
Decoder string `json:"decoder"`
}
func init() {
fromSocketSignature := runtime.MustLookupBuiltinType("socket", "from")
runtime.RegisterPackageValue("socket", "from", flux.MustValue(flux.FunctionValue(FromSocketKind, createFromSocketOpSpec, fromSocketSignature)))
flux.RegisterOpSpec(FromSocketKind, newFromSocketOp)
plan.RegisterProcedureSpec(FromSocketKind, newFromSocketProcedure, FromSocketKind)
execute.RegisterSource(FromSocketKind, createFromSocketSource)
}
// nowTimeProvider provides wall clock time.
type nowTimeProvider struct{}
func (a *nowTimeProvider) CurrentTime() values.Time {
return values.ConvertTime(time.Now())
}
var (
decoders = []string{"csv", "line"}
schemes = []string{"tcp", "unix"}
)
func contains(ss []string, s string) bool {
for _, st := range ss {
if st == s {
return true
}
}
return false
}
func createFromSocketOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
spec := new(FromSocketOpSpec)
if url, err := args.GetRequiredString("url"); err != nil {
return nil, err
} else {
spec.URL = url
}
if d, ok, err := args.GetString("decoder"); err != nil {
return nil, err
} else if ok {
spec.Decoder = d
} else {
spec.Decoder = decoders[0]
}
if !contains(decoders, spec.Decoder) {
return nil, errors.Newf(codes.Invalid, "invalid decoder %s, must be one of %v", spec.Decoder, decoders)
}
return spec, nil
}
func newFromSocketOp() flux.OperationSpec {
return new(FromSocketOpSpec)
}
func (s *FromSocketOpSpec) Kind() flux.OperationKind {
return FromSocketKind
}
type FromSocketProcedureSpec struct {
plan.DefaultCost
URL string
Decoder string
}
func newFromSocketProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*FromSocketOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &FromSocketProcedureSpec{
URL: spec.URL,
Decoder: spec.Decoder,
}, nil
}
func (s *FromSocketProcedureSpec) Kind() plan.ProcedureKind {
return FromSocketKind
}
func (s *FromSocketProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(FromSocketProcedureSpec)
ns.URL = s.URL
ns.Decoder = s.Decoder
return ns
}
func createFromSocketSource(s plan.ProcedureSpec, dsid execute.DatasetID, a execute.Administration) (execute.Source, error) {
spec, ok := s.(*FromSocketProcedureSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", s)
}
// known issue with url.Parse for detecting the presence of a scheme: https://github.com/golang/go/issues/19779
var scheme, address string
if !strings.Contains(spec.URL, "://") {
// no scheme specified, use default scheme and use the entire url as address
spec.URL = schemes[0] + "://" + spec.URL
}
url, err := neturl.Parse(spec.URL)
if err != nil {
return nil, errors.Newf(codes.Invalid, "invalid url: %v", err)
}
deps := flux.GetDependencies(a.Context())
validator, err := deps.URLValidator()
if err != nil {
return nil, err
}
if err := validator.Validate(url); err != nil {
return nil, errors.Newf(codes.Invalid, "url did not pass validation: %v", err)
}
scheme = url.Scheme
address = url.Host
if !contains(schemes, scheme) {
return nil, errors.Newf(codes.Invalid, "invalid scheme %s, must be one of %v", scheme, schemes)
}
conn, err := net.Dial(scheme, address)
if err != nil {
return nil, errors.Wrap(err, codes.Inherit, "error in creating socket source")
}
return NewSocketSource(spec, conn, &nowTimeProvider{}, dsid)
}
func NewSocketSource(spec *FromSocketProcedureSpec, rc io.ReadCloser, tp line.TimeProvider, dsid execute.DatasetID) (execute.Source, error) {
var decoder flux.ResultDecoder
switch spec.Decoder {
case "csv":
decoder = csv.NewResultDecoder(csv.ResultDecoderConfig{})
case "line":
decoder = line.NewResultDecoder(&line.ResultDecoderConfig{
Separator: '\n',
TimeProvider: tp,
})
}
if decoder == nil {
return nil, errors.Newf(codes.Invalid, "unknown decoder type: %v", spec.Decoder)
}
return &socketSource{
d: dsid,
rc: rc,
decoder: decoder,
}, nil
}
type socketSource struct {
d execute.DatasetID
rc io.ReadCloser
decoder flux.ResultDecoder
ts []execute.Transformation
}
func (ss *socketSource) AddTransformation(t execute.Transformation) {
ss.ts = append(ss.ts, t)
}
func (ss *socketSource) Run(ctx context.Context) {
defer ss.rc.Close()
result, err := ss.decoder.Decode(ss.rc)
if err != nil {
err = errors.Wrap(err, codes.Inherit, "decode error")
} else {
err = result.Tables().Do(func(tbl flux.Table) error {
for _, t := range ss.ts {
if err := t.Process(ss.d, tbl); err != nil {
return err
}
}
return nil
})
}
for _, t := range ss.ts {
t.Finish(ss.d, err)
}
}