-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
289 lines (278 loc) · 6.21 KB
/
runtime.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package faas
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/google/martian/cors"
"github.com/nats-io/nats.go"
"github.com/oklog/ulid/v2"
"github.com/synyi/faas.go/proto"
)
var nc *nats.Conn
var gwUrl url.URL
var clientId = ulid.Make().String()
type heartBeat struct {
Uptime uint32
Name string
Error string
Status string
Type string
ID string
}
func init() {
g := os.Getenv("FAAS_GATEWAY")
gu, err := url.Parse(g)
if err != nil {
log.Panicln("invalid FAAS_GATEWAY url: ", err)
}
gwUrl = *gu
}
func prodInit(handler func(ctx context.Context, eventCtx *EventCtx) (interface{}, error)) {
target := os.Getenv("FAAS_TARGET")
natsUrl := os.Getenv("NATS_URL")
timeout := os.Getenv("FAAS_TIMEOUT")
concurrent := os.Getenv("FAAS_CONCURRENT")
start := time.Now()
if target == "" {
log.Fatalln("env FAAS_TARGET missing")
}
if natsUrl == "" {
log.Fatalln("env NATS_URL missing")
}
timeoutDuration := time.Second * 120
if timeout != "" {
t, _ := strconv.Atoi(timeout)
if t > 0 {
timeoutDuration = time.Duration(t) * time.Second
}
}
var concurrenti int
if concurrent != "" {
concurrenti, _ = strconv.Atoi(concurrent)
}
if concurrenti < 1 {
concurrenti = 1
}
var err error
nc, err = nats.Connect(natsUrl)
if err != nil {
log.Panicln("cannot connect to nats, ", err)
}
// heartbeat
go func() {
for t := range time.NewTicker(10 * time.Second).C {
d, _ := json.Marshal(heartBeat{
Uptime: uint32(t.Sub(start).Seconds()),
Name: target,
Status: "ok",
Type: "function",
ID: clientId,
})
_ = nc.Publish("sie-heartbeat", d)
}
}()
js, _ := nc.JetStream()
stream := "faas.event." + target
msgCh := make(chan *nats.Msg, concurrenti*100)
sub, err := js.ChanQueueSubscribe(stream, "dg."+stream, msgCh, nats.Durable(strings.ReplaceAll(stream, ".", "_")))
if err != nil {
log.Panicln(err)
}
stopCtx, cancel := context.WithCancel(context.Background())
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGTERM)
<-c
cancel()
_ = sub.Drain()
os.Exit(0)
}()
wg := sync.WaitGroup{}
wg.Add(concurrenti)
for i := 0; i < concurrenti; i++ {
go func() {
for {
var msg *nats.Msg
select {
case <-stopCtx.Done():
wg.Done()
return
case msg = <-msgCh:
}
err = func() (err error) {
var senderr error
var sent bool
//宽松一秒
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration+time.Second)
defer func() {
cancel()
e := recover()
if e != nil {
err = fmt.Errorf("%s", e)
}
log.Println(senderr)
}()
c := ctxFromEventData(msg.Data)
if c.Ttl <= 0 {
log.Println("drop message due to TTL=0")
_ = msg.Term()
cancel()
return
}
var handlerStart time.Time
c.send = func() {
t := time.Now()
c.resp.Headers = buildHeaders(c.respHeaders)
c.resp.Time = t.UnixMilli()
c.resp.UsedTime = uint32(t.Sub(handlerStart).Milliseconds())
d, _ := c.resp.Marshal()
senderr = nc.Publish("faas.response."+c.SenderId, d)
sent = true
}
c.retry = func(reason string) {
_ = msg.Nak()
c.resp.Retry = true
c.resp.Body = []byte(reason)
c.resp.Time = time.Now().UnixMilli()
c.resp.Status = 500
d, _ := c.resp.Marshal()
senderr = nc.Publish("faas.response."+c.SenderId, d)
sent = true
}
go func() {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
_ = msg.Term()
log.Println("handler not finish in time, exit!")
os.Exit(1)
}
}()
ctx2, cancel2 := context.WithTimeout(ctx, timeoutDuration)
handlerStart = time.Now()
resp, err := handler(ctx2, c)
cancel2()
cancel()
if !sent {
if err != nil {
_ = c.Response(500, err)
} else {
err = c.Response(200, resp)
if err != nil {
_ = c.Response(500, err)
}
}
}
_ = msg.Ack()
return
}()
if err != nil {
log.Println("panic:", err)
_ = msg.Term()
}
}
}()
}
wg.Wait()
}
func testInit(handler func(ctx context.Context, eventCtx *EventCtx) (interface{}, error)) {
}
func buildHeaders(h http.Header) []string {
var ret []string
for k, v := range h {
for _, vv := range v {
ret = append(ret, k, vv)
}
}
return ret
}
func localInit(handler func(ctx context.Context, eventCtx *EventCtx) (interface{}, error)) {
listen := os.Getenv("LISTEN")
if listen == "" {
listen = ":30000"
}
h := http.HandlerFunc(func(wr http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/metrics" { // prometheus扫描屏蔽
return
}
body, _ := io.ReadAll(r.Body)
event := &proto.Event{
EventId: ulid.Make().String(),
SenderId: "LOCAL",
Type: proto.HTTP,
Method: r.Method,
Url: r.RequestURI,
Body: body,
BodyType: proto.Raw,
ContentType: "",
Headers: buildHeaders(r.Header),
Source: r.RemoteAddr,
}
c := ctxFromEvent(event)
sended := false
c.send = func() {
h := wr.Header()
for k, v := range c.respHeaders {
for _, vv := range v {
h.Add(k, vv)
}
}
if c.resp.Status == 0 {
c.resp.Status = 200
}
wr.WriteHeader(int(c.resp.Status))
wr.Write(c.resp.Body)
sended = true
}
c.retry = func(reason string) {
h := wr.Header()
for k, v := range c.respHeaders {
for _, vv := range v {
h.Add(k, vv)
}
}
wr.WriteHeader(500)
wr.Write([]byte(reason))
sended = true
}
resp, err := handler(context.Background(), c)
if !sended {
if err != nil {
_ = c.Response(500, err)
} else {
err = c.Response(200, resp)
if err != nil {
_ = c.Response(500, err)
}
}
}
if err != nil {
http.Error(wr, err.Error(), 500)
} else {
c.send()
}
})
log.Println("test handler in " + listen)
log.Fatalln(http.ListenAndServe(listen, cors.NewHandler(h)))
}
func Init(handler func(ctx context.Context, eventCtx *EventCtx) (interface{}, error)) {
mode := os.Getenv("MODE")
switch mode {
case "prod":
prodInit(handler)
case "test":
testInit(handler)
default:
localInit(handler)
}
}