forked from tsuru/planb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseproxy.go
72 lines (62 loc) · 1.68 KB
/
reverseproxy.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
// Copyright 2016 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reverseproxy
import (
"crypto/tls"
"errors"
"net"
"time"
"github.com/opencoff/planb/log"
vrfy "github.com/opencoff/cloudly/authverify"
)
var (
noRouteResponseContent = []byte("no such route")
allBackendsDeadContent = []byte("all backends are dead")
okResponse = []byte("OK")
websocketUpgrade = []byte("websocket")
ErrAllBackendsDead = errors.New(string(allBackendsDeadContent))
ErrNoRegisteredBackends = errors.New("no backends registered for host")
)
type Router interface {
Healthcheck() error
ChooseBackend(host string) (*RequestData, error)
EndRequest(reqData *RequestData, isDead bool, fn func() *log.LogEntry) error
}
type ReverseProxy interface {
Initialize(rpConfig ReverseProxyConfig) error
Listen(net.Listener, *tls.Config)
Stop()
}
type ReverseProxyConfig struct {
Router Router
FlushInterval time.Duration
DialTimeout time.Duration
RequestTimeout time.Duration
ReadTimeout time.Duration
ReadHeaderTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
HeaderPrefix string
AuthVerify vrfy.AuthVerifier
}
type RequestData struct {
BackendLen int
Backend string
BackendIdx int
BackendKey string
Host string
StartTime time.Time
AllDead bool
}
func (r *RequestData) logError(path string, rid string, err error) {
log.ErrorLogger.MessageRaw(&log.LogEntry{
Err: &log.ErrEntry{
Backend: r.Backend,
Host: r.Host,
Path: path,
Rid: rid,
Err: err.Error(),
},
})
}