-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
655 lines (555 loc) · 15 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"flag"
"log"
"net/http"
"net/url"
"path"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/moonrhythm/parapet"
"github.com/moonrhythm/parapet/pkg/location"
"github.com/moonrhythm/parapet/pkg/logger"
"github.com/moonrhythm/parapet/pkg/prom"
"github.com/moonrhythm/parapet/pkg/stripprefix"
"github.com/moonrhythm/parapet/pkg/upstream"
"github.com/prometheus/client_golang/prometheus"
)
var (
healthyDuration time.Duration
lbUpstream lb
lbFallback lb
lbWs lb
maxRetry = 3
retryBackoff = 10 * time.Millisecond
)
func main() {
var (
addr = flag.String("addr", ":80", "HTTP address")
logEnable = flag.Bool("log", true, "Enable request log")
tlsAddr = flag.String("tls.addr", "", "HTTPS address")
tlsKey = flag.String("tls.key", "", "TLS private key file")
tlsCert = flag.String("tls.cert", "", "TLS certificate file")
upstreamList = flag.String("upstream", "", "Upstream list")
upstreamOverrideHost = flag.Bool("upstream.override-host", false, "override http host header to upstream")
gethHealthyDuration = flag.Duration("healthy-duration", time.Minute, "duration from last block that mark as healthy")
healthCheckDeadline = flag.Duration("health-check.deadline", 4*time.Second, "deadline when run health check")
healthCheckInterval = flag.Duration("health-check.interval", 2*time.Second, "health check interval")
fallbackUpstreamList = flag.String("fallback.upstream", "", "fallback upstream list")
fallbackHealthCheckDeadline = flag.Duration("fallback.health-check.deadline", 5*time.Second, "fallback deadline when run health check")
fallbackHealthCheckInterval = flag.Duration("fallback.health-check.interval", 5*time.Second, "fallback health check interval")
requestMaxBodySize = flag.Int64("request.max-body-size", 0, "max request body size")
retryMax = flag.Int("retry.max", maxRetry, "max retry")
retryBackoffDuration = flag.Duration("retry.backoff", retryBackoff, "retry backoff")
)
flag.Parse()
log.Printf("geth-gateway")
log.Printf("HTTP address: %s", *addr)
log.Printf("HTTPS address: %s", *tlsAddr)
log.Printf("Upstream: %s", *upstreamList)
log.Printf("Upstream Override Host: %t", *upstreamOverrideHost)
log.Printf("Healthy Duration: %s", *gethHealthyDuration)
log.Printf("Health Check Deadline: %s", *healthCheckDeadline)
log.Printf("Health Check Interval: %s", *healthCheckInterval)
log.Printf("Fallback Upstream: %s", *fallbackUpstreamList)
log.Printf("Fallback Health Check Deadline: %s", *fallbackHealthCheckDeadline)
log.Printf("Fallback Health Check Interval: %s", *fallbackHealthCheckInterval)
log.Printf("Request Max Body Size: %d", *requestMaxBodySize)
log.Printf("Retry Max: %d", *retryMax)
log.Printf("Retry Backoff: %s", *retryBackoffDuration)
healthyDuration = *gethHealthyDuration
lbUpstream.fallback = &lbFallback
maxRetry = *retryMax
if maxRetry < 0 {
maxRetry = 0
}
retryBackoff = *retryBackoffDuration
prom.Registry().MustRegister(headDuration, headNumber)
go prom.Start(":6060")
for _, addr := range strings.Split(*upstreamList, ",") {
addr = strings.TrimSpace(addr)
if addr == "" {
continue
}
u, err := url.Parse(addr)
if err != nil {
log.Fatalf("can not parse url; %v", err)
}
if u.Scheme == "ws" || u.Scheme == "wss" {
u.Scheme = "http" + strings.TrimPrefix(u.Scheme, "ws") // http or https
lbWs.addr = append(lbWs.addr, addr)
lbWs.urls = append(lbWs.urls, u)
lbWs.blocks = append(lbWs.blocks, lastBlock{})
} else {
lbUpstream.addr = append(lbUpstream.addr, addr)
lbUpstream.urls = append(lbUpstream.urls, u)
lbUpstream.blocks = append(lbUpstream.blocks, lastBlock{})
}
}
for _, addr := range strings.Split(*fallbackUpstreamList, ",") {
addr = strings.TrimSpace(addr)
if addr == "" {
continue
}
u, err := url.Parse(addr)
if err != nil {
log.Fatalf("can not parse url; %v", err)
}
lbFallback.addr = append(lbFallback.addr, addr)
lbFallback.urls = append(lbFallback.urls, u)
lbFallback.blocks = append(lbFallback.blocks, lastBlock{})
}
{
lbs := []*lb{&lbUpstream, &lbWs, &lbFallback}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
var wg sync.WaitGroup
for _, x := range lbs {
x := x
if len(x.addr) == 0 {
continue
}
wg.Add(1)
go func() {
defer wg.Done()
x.updateLastBlock(ctx)
}()
}
wg.Wait()
cancel()
}
go lbUpstream.runUpdateLoop(*healthCheckInterval, *healthCheckDeadline)
go lbWs.runUpdateLoop(*healthCheckInterval, *healthCheckDeadline)
go lbFallback.runUpdateLoop(*fallbackHealthCheckInterval, *fallbackHealthCheckDeadline)
var s parapet.Middlewares
if *logEnable {
s.Use(logger.Stdout())
}
s.Use(prom.Requests())
// healthz
{
l := location.Exact("/healthz")
l.Use(parapet.Handler(healthz))
s.Use(l)
}
// upstreams
{
l := location.Exact("/upstreams")
l.Use(parapet.Handler(lbUpstream.upstreamsHandler))
s.Use(l)
}
// ws upstreams
{
l := location.Exact("/wsupstreams")
l.Use(parapet.Handler(lbWs.upstreamsHandler))
s.Use(l)
}
{
l := location.Exact("/ws")
l.Use(stripprefix.New("/ws"))
l.Use(upstream.New(&lbWs))
s.Use(l)
}
// http
s.Use(bufferRequestBody(*requestMaxBodySize))
s.Use(upstream.New(&lbUpstream))
var wg sync.WaitGroup
if *addr != "" {
wg.Add(1)
srv := parapet.NewBackend()
srv.Addr = *addr
srv.GraceTimeout = 3 * time.Second
srv.WaitBeforeShutdown = 0
srv.Use(s)
prom.Connections(srv)
prom.Networks(srv)
go func() {
defer wg.Done()
err := srv.ListenAndServe()
if err != nil {
log.Fatalf("can not start server; %v", err)
}
}()
}
if *tlsAddr != "" {
wg.Add(1)
srv := parapet.NewBackend()
srv.Addr = *tlsAddr
srv.GraceTimeout = 3 * time.Second
srv.WaitBeforeShutdown = 0
srv.TLSConfig = &tls.Config{}
if *tlsKey == "" || *tlsCert == "" {
cert, err := parapet.GenerateSelfSignCertificate(parapet.SelfSign{
CommonName: "geth-gateway",
Hosts: []string{"geth-gateway"},
NotBefore: time.Now().Add(-5 * time.Minute),
NotAfter: time.Now().AddDate(10, 0, 0),
})
if err != nil {
log.Fatalf("can not generate self signed cert; %v", err)
}
srv.TLSConfig.Certificates = append(srv.TLSConfig.Certificates, cert)
} else {
cert, err := tls.LoadX509KeyPair(*tlsCert, *tlsKey)
if err != nil {
log.Fatalf("can not load x509 key pair; %v", err)
}
srv.TLSConfig.Certificates = append(srv.TLSConfig.Certificates, cert)
}
srv.Use(s)
prom.Connections(srv)
prom.Networks(srv)
go func() {
defer wg.Done()
err := srv.ListenAndServe()
if err != nil {
log.Fatalf("can not start server; %v", err)
}
}()
}
wg.Wait()
}
func isReady() bool {
lbUpstream.mu.RLock()
block := lbUpstream.block
lbUpstream.mu.RUnlock()
t := time.Unix(int64(block.Time), 0)
return time.Since(t) <= healthyDuration
}
func isLive() bool {
lbUpstream.mu.RLock()
ok := len(lbUpstream.bestURLs) > 0
updated := lbUpstream.updated
lbUpstream.mu.RUnlock()
return ok && time.Since(updated) <= 10*time.Second
}
func healthz(w http.ResponseWriter, r *http.Request) {
if r.FormValue("ready") == "1" {
if !isReady() {
http.Error(w, "not ready", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("ready"))
return
}
if !isLive() {
http.Error(w, "not ok", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
}
type lb struct {
mu sync.RWMutex
addr []string
urls []*url.URL
bestURLs []*url.URL
block *types.Header
updated time.Time
blocks []lastBlock
overrideHost bool
fallback *lb
// tr
i uint32
}
type lastBlock struct {
mu sync.Mutex
Block *types.Header
UpdatedAt time.Time
}
func getLastBlock(ctx context.Context, client *ethclient.Client, b *lastBlock, force bool) (*types.Header, error) {
b.mu.Lock()
defer b.mu.Unlock()
if !force && time.Since(b.UpdatedAt) < time.Second {
return b.Block, nil
}
block, err := client.HeaderByNumber(ctx, nil)
if err != nil {
return b.Block, err
}
b.Block = block
b.UpdatedAt = time.Now()
return b.Block, nil
}
const promNamespace = "geth_gateway"
var headDuration = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: promNamespace,
Name: "head_duration_seconds",
}, []string{"upstream"})
var headNumber = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: promNamespace,
Name: "head_number",
}, []string{"upstream"})
func (lb *lb) updateLastBlock(ctx context.Context) {
blockNumbers := make([]uint64, len(lb.blocks))
blockData := make([]*types.Header, len(lb.blocks))
var wg sync.WaitGroup
for i := range lb.blocks {
i := i
wg.Add(1)
go func() {
defer wg.Done()
client, err := getClient(ctx, lb.addr[i])
if err != nil {
return
}
block, _ := getLastBlock(ctx, client, &lb.blocks[i], true)
if block == nil {
return
}
blockNumbers[i] = block.Number.Uint64()
blockData[i] = block
t := time.Unix(int64(block.Time), 0)
diff := time.Since(t)
g, err := headDuration.GetMetricWith(prometheus.Labels{
"upstream": lb.addr[i],
})
if err == nil {
g.Set(float64(diff) / float64(time.Second))
}
g, err = headNumber.GetMetricWith(prometheus.Labels{
"upstream": lb.addr[i],
})
if err == nil {
g.Set(float64(block.Number.Uint64()))
}
}()
}
wg.Wait()
h := highestBlock(blockNumbers)
// collect all best block rpc
best := make([]*url.URL, 0, len(blockNumbers))
var bestIndex int
for i, b := range blockNumbers {
if b == h {
best = append(best, lb.urls[i])
bestIndex = i
}
}
lb.mu.Lock()
lb.bestURLs = best
lb.block = blockData[bestIndex]
lb.updated = time.Now()
lb.mu.Unlock()
}
func (lb *lb) runUpdateLoop(interval, deadline time.Duration) {
if len(lb.addr) == 0 {
return
}
for {
ctx, cancel := context.WithTimeout(context.Background(), deadline)
lb.updateLastBlock(ctx)
cancel()
time.Sleep(interval)
}
}
func highestBlock(blockNumbers []uint64) uint64 {
max := blockNumbers[0]
for _, x := range blockNumbers {
if x > max {
max = x
}
}
return max
}
var (
trs = map[string]http.RoundTripper{
"http": &upstream.HTTPTransport{
MaxIdleConns: 500,
},
"https": &upstream.HTTPSTransport{
MaxIdleConns: 500,
MaxConn: 2000,
IdleConnTimeout: 30 * time.Second,
},
}
)
func (lb *lb) roundTrip(r *http.Request) (*http.Response, error) {
lb.mu.RLock()
targets := lb.bestURLs
lb.mu.RUnlock()
if len(targets) == 0 {
return nil, upstream.ErrUnavailable
}
i := atomic.AddUint32(&lb.i, 1) - 1
i %= uint32(len(targets))
t := targets[i]
r.URL.Scheme = t.Scheme
r.URL.Host = t.Host
r.URL.Path = path.Join(t.Path, r.URL.Path)
r.Host = t.Host
r.Header.Del("X-Forwarded-For")
r.Header.Del("X-Forwarded-Proto")
r.Header.Del("X-Real-Ip")
if b, ok := r.Body.(*retryableBody); ok {
b.Reset()
}
resp, err := trs[t.Scheme].RoundTrip(r)
if isResponseRetryable(resp) {
var status int
if resp != nil {
status = resp.StatusCode
}
log.Printf("lb: retryable upstream=%s, status=%d", t, status)
}
return resp, err
}
func (lb *lb) roundTripWithFallback(r *http.Request) (*http.Response, error) {
if lb.isFallback() {
return lb.fallback.roundTrip(r)
}
return lb.roundTrip(r)
}
func isResponseRetryable(resp *http.Response) bool {
if resp == nil {
return true
}
if resp.StatusCode >= 500 || resp.StatusCode == http.StatusTooManyRequests {
return true
}
return false
}
func (lb *lb) RoundTrip(r *http.Request) (resp *http.Response, err error) {
for i := 0; i <= maxRetry; i++ {
resp, err = lb.roundTripWithFallback(r)
if err == nil && !isResponseRetryable(resp) {
return
}
time.Sleep(retryBackoff)
}
return
}
func (lb *lb) isFallback() bool {
if lb.fallback == nil {
return false
}
lb.mu.RLock()
block := lb.block
lb.mu.RUnlock()
if block == nil {
// lb don't have any block
return true
}
t := time.Unix(int64(block.Time), 0)
ready := time.Since(t) <= healthyDuration
// our lb is not ready but let check is the fallback is faster than us
if !ready {
lb.fallback.mu.RLock()
fallbackBlock := lb.fallback.block
lb.fallback.mu.RUnlock()
if fallbackBlock == nil {
// fallback not available
return false
}
ready = block.Time >= fallbackBlock.Time
}
return !ready
}
type upstreamsResult struct {
Upstreams []string `json:"upstreams"`
Block struct {
Number uint64 `json:"number"`
Duration string `json:"duration"`
} `json:"block"`
Fallback *upstreamsResult `json:"fallback"`
Forward string `json:"forward"`
}
func (lb *lb) upstreams() upstreamsResult {
lb.mu.RLock()
block := lb.block
list := lb.bestURLs
lb.mu.RUnlock()
var resp upstreamsResult
resp.Forward = "upstream"
for _, x := range list {
resp.Upstreams = append(resp.Upstreams, x.String())
}
if block != nil {
resp.Block.Number = block.Number.Uint64()
resp.Block.Duration = time.Since(time.Unix(int64(block.Time), 0)).String()
}
if lb.fallback != nil {
fb := lb.fallback.upstreams()
resp.Fallback = &fb
}
if lb.isFallback() {
resp.Forward = "fallback"
}
return resp
}
func (lb *lb) upstreamsHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(lb.upstreams())
}
var (
clients = map[string]*ethclient.Client{}
muClients sync.RWMutex
)
func getClient(ctx context.Context, addr string) (*ethclient.Client, error) {
muClients.RLock()
c := clients[addr]
muClients.RUnlock()
if c != nil {
return c, nil
}
var err error
c, err = ethclient.DialContext(ctx, addr)
if err != nil {
return nil, err
}
muClients.Lock()
defer muClients.Unlock()
if clients[addr] == nil {
clients[addr] = c
} else {
c.Close()
}
return clients[addr], nil
}
func bufferRequestBody(size int64) parapet.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet || r.Method == http.MethodHead || r.ContentLength == 0 {
h.ServeHTTP(w, r)
return
}
if r.ContentLength < 0 {
http.Error(w, "invalid content length", http.StatusBadRequest)
return
}
if size > 0 && r.ContentLength > size {
http.Error(w, "content length too large", http.StatusBadRequest)
return
}
var buf bytes.Buffer
_, err := buf.ReadFrom(r.Body)
if err != nil {
http.Error(w, "failed to read body", http.StatusBadRequest)
return
}
r.Body = &retryableBody{
raw: buf.Bytes(),
buf: &buf,
}
h.ServeHTTP(w, r)
})
}
}
type retryableBody struct {
raw []byte
buf *bytes.Buffer
}
func (b *retryableBody) Read(p []byte) (n int, err error) { return b.buf.Read(p) }
func (*retryableBody) Close() error { return nil }
func (b *retryableBody) Reset() {
b.buf = bytes.NewBuffer(b.raw)
}