-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhttp.go
More file actions
173 lines (154 loc) · 4.53 KB
/
Copy pathhttp.go
File metadata and controls
173 lines (154 loc) · 4.53 KB
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
/*
* Copyright 2024 Function Stream Org.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package contube
import (
"github.com/functionstream/function-stream/common"
"io"
"net/http"
"sync"
"sync/atomic"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
type state int
const (
EndpointKey = "endpoint"
IncludeMetadata = "includeMetadata"
stateReady state = iota // TODO: Why do we need this? Maybe we can remove it.
stateClosed state = iota
)
var (
ErrEndpointNotFound = errors.New("endpoint not found")
ErrEndpointClosed = errors.New("endpoint closed")
ErrorEndpointAlreadyExists = errors.New("endpoint already exists")
)
type EndpointHandler func(ctx context.Context, endpoint string, payload []byte) error
type HttpHandler func(w http.ResponseWriter, r *http.Request, payload []byte) Record
func DefaultHttpHandler(_ http.ResponseWriter, _ *http.Request, payload []byte) Record {
return NewRecordImpl(payload, func() {})
}
type endpointHandler struct {
ctx context.Context
s atomic.Value
c chan Record
}
type HttpTubeFactory struct {
TubeFactory
ctx context.Context
mu sync.RWMutex
endpoints map[string]*endpointHandler
handler HttpHandler
}
func NewHttpTubeFactory(ctx context.Context) *HttpTubeFactory {
return NewHttpTubeFactoryWithIntercept(ctx, DefaultHttpHandler)
}
func NewHttpTubeFactoryWithIntercept(ctx context.Context, handler HttpHandler) *HttpTubeFactory {
return &HttpTubeFactory{
ctx: ctx,
endpoints: make(map[string]*endpointHandler),
handler: handler,
}
}
type httpSourceTubeConfig struct {
Endpoint string `json:"endpoint" validate:"required"`
}
func (f *HttpTubeFactory) getEndpoint(endpoint string) (*endpointHandler, bool) {
f.mu.RLock()
defer f.mu.RUnlock()
e, ok := f.endpoints[endpoint]
return e, ok
}
func (f *HttpTubeFactory) Handle(ctx context.Context, endpoint string, record Record) error {
e, ok := f.getEndpoint(endpoint)
if !ok {
return ErrEndpointNotFound
}
if e.s.Load() == stateClosed {
return ErrEndpointClosed
}
select {
case e.c <- record:
return nil
case <-ctx.Done():
return ctx.Err()
case <-e.ctx.Done():
return ErrEndpointClosed
}
}
func (f *HttpTubeFactory) NewSourceTube(ctx context.Context, config ConfigMap) (<-chan Record, error) {
c := httpSourceTubeConfig{}
if err := config.ToConfigStruct(&c); err != nil {
return nil, err
}
result := make(chan Record, 10)
f.mu.Lock()
defer f.mu.Unlock()
if _, ok := f.endpoints[c.Endpoint]; ok {
return nil, ErrorEndpointAlreadyExists
}
var s atomic.Value
s.Store(stateReady)
handlerCtx, cancel := context.WithCancel(f.ctx)
e := &endpointHandler{
c: result,
s: s,
ctx: handlerCtx,
}
f.endpoints[c.Endpoint] = e
go func() {
<-ctx.Done()
cancel()
close(result)
f.mu.Lock()
defer f.mu.Unlock()
delete(f.endpoints, c.Endpoint)
}()
return result, nil
}
func (f *HttpTubeFactory) NewSinkTube(_ context.Context, _ ConfigMap) (chan<- Record, error) {
return nil, ErrSinkTubeNotImplemented
}
func (f *HttpTubeFactory) GetHandleFunc(getEndpoint func(r *http.Request) (string, error),
logger *common.Logger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
endpoint, err := getEndpoint(r)
if err != nil {
logger.Error(err, "Failed to get endpoint")
http.Error(w, errors.Wrap(err, "Failed to get endpoint").Error(), http.StatusBadRequest)
return
}
log := logger.SubLogger("endpoint", endpoint, "component", "http-tube")
if log.DebugEnabled() {
log.Debug("Handle records from http request")
}
content, err := io.ReadAll(r.Body)
if err != nil {
log.Error(err, "Failed to read body")
http.Error(w, errors.Wrap(err, "Failed to read body").Error(), http.StatusBadRequest)
return
}
record := f.handler(w, r, content)
err = f.Handle(r.Context(), endpoint, record)
if err != nil {
log.Error(err, "Failed to handle record")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if log.DebugEnabled() {
log.Debug("Handled records from http request")
}
}
}