-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
34 lines (28 loc) · 718 Bytes
/
handler.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
package main
import (
"net/http"
"github.com/seipan/mylb/serverpool"
"github.com/seipan/mylb/utils"
"go.uber.org/zap"
)
type LBHandler interface {
Serve(http.ResponseWriter, *http.Request)
}
type lbHandler struct {
serverPool serverpool.ServerPool
}
func (lb *lbHandler) Serve(w http.ResponseWriter, r *http.Request) {
peer := lb.serverPool.GetNextValidPeer()
if peer != nil {
utils.Info("access to endpoint",
zap.String("url", peer.GetURL()),
zap.Int("connections", peer.GetConnections()),
)
peer.Serve(w, r)
return
}
http.Error(w, "Service not available", http.StatusServiceUnavailable)
}
func NewLBHandler(serverPool serverpool.ServerPool) LBHandler {
return &lbHandler{serverPool}
}