Skip to content

Commit

Permalink
fix: remove LB onError #31
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed May 5, 2021
1 parent 2be264a commit 6ee3a16
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package constant
import "time"

const (
Version = "1.0.4"
Version = "1.0.5"

MaxMWSSStreamCnt = 10
DialTimeOut = 3 * time.Second
Expand Down
5 changes: 0 additions & 5 deletions internal/lb/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,3 @@ func (lp *LBNodes) PickMin() *LBNode {
func (lp *LBNodes) DeferPick(node *LBNode) {
lp.IncrUserCnt(node, -1)
}

func (lp *LBNodes) OnError(node *LBNode) {
// NOTE 遇到错误的时候降低这个节点的权重
lp.IncrUserCnt(node, 100)
}
1 change: 0 additions & 1 deletion internal/transporter/mwss.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (s *Mwss) HandleTCPConn(c *net.TCPConn) error {

wsc, err := s.mtp.Dial(node.Remote + "/mwss/")
if err != nil {
s.raw.TCPNodes.OnError(node)
return err
}
defer wsc.Close()
Expand Down
5 changes: 0 additions & 5 deletions internal/transporter/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (raw *Raw) HandleUDPConn(uaddr *net.UDPAddr, local *net.UDPConn) {
rc, err := net.Dial("udp", node.Remote)
if err != nil {
logger.Info(err)
raw.UDPNodes.OnError(node)
return
}
defer func() {
Expand Down Expand Up @@ -99,7 +98,6 @@ func (raw *Raw) HandleTCPConn(c *net.TCPConn) error {

rc, err := net.Dial("tcp", node.Remote)
if err != nil {
raw.TCPNodes.OnError(node)
return err
}
logger.Infof("[raw] HandleTCPConn from %s to %s", c.LocalAddr().String(), node.Remote)
Expand All @@ -122,7 +120,6 @@ func (raw *Raw) HandleWsRequset(w http.ResponseWriter, req *http.Request) {
rc, err := net.Dial("tcp", node.Remote)
if err != nil {
logger.Infof("dial error: %s", err)
raw.TCPNodes.OnError(node)
return
}
defer rc.Close()
Expand All @@ -147,7 +144,6 @@ func (raw *Raw) HandleWssRequset(w http.ResponseWriter, req *http.Request) {
rc, err := net.Dial("tcp", node.Remote)
if err != nil {
logger.Infof("dial error: %s", err)
raw.TCPNodes.OnError(node)
return
}
defer rc.Close()
Expand All @@ -168,7 +164,6 @@ func (raw *Raw) HandleMWssRequset(c net.Conn) {
rc, err := net.Dial("tcp", node.Remote)
if err != nil {
logger.Infof("dial error: %s", err)
raw.TCPNodes.OnError(node)
return
}
defer rc.Close()
Expand Down
1 change: 0 additions & 1 deletion internal/transporter/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (s *Ws) HandleTCPConn(c *net.TCPConn) error {

wsc, _, _, err := ws.Dial(context.TODO(), node.Remote+"/ws/")
if err != nil {
s.raw.TCPNodes.OnError(node)
return err
}
defer wsc.Close()
Expand Down
34 changes: 17 additions & 17 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,7 @@ func registerMetrics(cfg *config.Config) {
go pg.Run()
}

func StartWebServer(port, token string, cfg *config.Config) {
time.Sleep(time.Second)
addr := "0.0.0.0:" + port
logger.Infof("[web] Start Web Server at http://%s/", addr)
r := mux.NewRouter()
AttachProfiler(r)
registerMetrics(cfg)
r.Handle("/", http.HandlerFunc(Welcome))
r.Handle("/metrics/", promhttp.Handler())
if token != "" {
logger.Fatal(http.ListenAndServe(addr, SimpleTokenAuthMiddleware(token, r)))
} else {
logger.Fatal(http.ListenAndServe(addr, r))
}
}

func SimpleTokenAuthMiddleware(token string, h http.Handler) http.Handler {
func simpleTokenAuthMiddleware(token string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if t := r.URL.Query().Get("token"); t != token {
msg := fmt.Sprintf("[web] unauthorsied from %s", r.RemoteAddr)
Expand All @@ -83,3 +67,19 @@ func SimpleTokenAuthMiddleware(token string, h http.Handler) http.Handler {
h.ServeHTTP(w, r)
})
}

func StartWebServer(port, token string, cfg *config.Config) {
time.Sleep(time.Second)
addr := "0.0.0.0:" + port
logger.Infof("[web] Start Web Server at http://%s/", addr)
r := mux.NewRouter()
AttachProfiler(r)
registerMetrics(cfg)
r.Handle("/", http.HandlerFunc(Welcome))
r.Handle("/metrics/", promhttp.Handler())
if token != "" {
logger.Fatal(http.ListenAndServe(addr, simpleTokenAuthMiddleware(token, r)))
} else {
logger.Fatal(http.ListenAndServe(addr, r))
}
}

0 comments on commit 6ee3a16

Please sign in to comment.