-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproxy_router.go
More file actions
61 lines (55 loc) · 1.55 KB
/
proxy_router.go
File metadata and controls
61 lines (55 loc) · 1.55 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
package main
import (
"sync"
"time"
"github.com/admpub/log"
"github.com/webx-top/reverseproxy"
rlog "github.com/webx-top/reverseproxy/log"
)
type ProxyRouter struct {
*Proxy
dst string //目标网址
resultHost string //最终操作的主机
resultReqData *reverseproxy.RequestData
resultIsDead bool
logEntry *rlog.LogEntry
}
func (r *ProxyRouter) ChooseBackend(host string) (*reverseproxy.RequestData, error) {
this := r.Proxy
app := this.App
var err error
if app.SwitchToNewPort {
this.FirstRequest.Do(func() {
log.Info(`== Switch port: `, this.appOldPort, ` => `, app.Port)
app.SwitchToNewPort = false
this.upgraded = time.Now().Unix()
go this.App.Clean()
r.dst = "http://localhost:" + app.Port
log.Info("== Listening to " + r.dst)
this.FirstRequest = &sync.Once{}
})
} else if !app.IsRunning() && !this.Watcher.compiling.Load() {
this.FirstRequest.Do(func() {
err = app.Restart(this.ctx)
this.FirstRequest = &sync.Once{}
})
}
r.resultHost = host
return &reverseproxy.RequestData{
Backend: r.dst,
BackendIdx: 0,
BackendKey: host,
BackendLen: 1,
Host: host,
StartTime: time.Now(),
}, err
}
func (r *ProxyRouter) EndRequest(reqData *reverseproxy.RequestData, isDead bool, fn func() *rlog.LogEntry) error {
r.resultReqData = reqData
r.logEntry = fn()
r.resultIsDead = isDead
if !r.Proxy.App.DisabledLogRequest {
log.Infof("== Request: %7s %s => Completed %d in %vs", r.logEntry.Method, r.logEntry.Path, r.logEntry.StatusCode, r.logEntry.TotalDuration.Seconds())
}
return nil
}