Skip to content

Commit

Permalink
增加部分注释
Browse files Browse the repository at this point in the history
  • Loading branch information
mingcheng committed Jul 15, 2022
1 parent a97904c commit 022b359
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 20 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* File: http.go
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Saturday, July 9th 2022, 7:42:02 pm
* Last Modified: Friday, July 15th 2022, 5:33:53 pm
*
* http://www.opensource.org/licenses/MIT
*/

package socks5lb

import (
Expand All @@ -23,10 +33,14 @@ func init() {
gin.DisableConsoleColor()
}

// setupAPIRouter to handle the APIRouter
func (s *Server) setupAPIRouter(apiGroup *gin.RouterGroup) (err error) {

// to show all backends
apiGroup.GET("all", func(c *gin.Context) {
backends := s.Pool.All()

// if shows healthy backends only
printHealthy, _ := strconv.ParseBool(c.Query("healthy"))
if printHealthy {
backends = s.Pool.AllHealthy()
Expand All @@ -35,6 +49,7 @@ func (s *Server) setupAPIRouter(apiGroup *gin.RouterGroup) (err error) {
c.JSON(http.StatusOK, backends)
})

// to delete a sinle backend
apiGroup.DELETE("delete", func(c *gin.Context) {
addr := c.Query("addr")
if addr == "" {
Expand All @@ -52,6 +67,7 @@ func (s *Server) setupAPIRouter(apiGroup *gin.RouterGroup) (err error) {
c.String(http.StatusOK, fmt.Sprintf("server %s is removed", addr))
})

// batch add backends
apiGroup.PUT("add", func(c *gin.Context) {
var backends []Backend

Expand All @@ -61,9 +77,6 @@ func (s *Server) setupAPIRouter(apiGroup *gin.RouterGroup) (err error) {
}

for _, backend := range backends {
//if backend.CheckConfig.CheckURL == "" {
// backend.CheckConfig.CheckURL = "https://www.taobao.com"
//}
err = s.Pool.Add(&backend)
if err != nil {
c.String(http.StatusServiceUnavailable, err.Error())
Expand All @@ -77,17 +90,20 @@ func (s *Server) setupAPIRouter(apiGroup *gin.RouterGroup) (err error) {
return
}

// setupRouter to setup the http server routers
func (s *Server) setupRouter() (err error) {
if engine != nil {
return fmt.Errorf("the Gin engine is alreay instanced, maybe is running")
}

// gin default config
engine = gin.New()
engine.Use(ginlogrus.Logger(log.New(), "http", false, true, os.Stdout, log.TraceLevel))
engine.Use(gin.Recovery())

err = s.setupAPIRouter(engine.Group("/api"))

// show basic information
engine.GET("/version", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"name": AppName,
Expand All @@ -109,6 +125,7 @@ func (s *Server) ListenHTTPAdmin(addr string) (err error) {
return engine.Run(addr)
}

// Engine returns the main http engine for testing purposes
func (s *Server) Engine() *gin.Engine {
return engine
}
8 changes: 6 additions & 2 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Author: Ming Cheng<mingcheng@outlook.com>
*
* Created Date: Tuesday, June 21st 2022, 6:03:26 pm
* Last Modified: Thursday, July 7th 2022, 6:47:39 pm
* Last Modified: Friday, July 15th 2022, 5:35:23 pm
*
* http://www.opensource.org/licenses/MIT
*/
Expand All @@ -12,9 +12,10 @@ package socks5lb

import (
"fmt"
log "github.com/sirupsen/logrus"
"sync"
"sync/atomic"

log "github.com/sirupsen/logrus"
)

type Pool struct {
Expand All @@ -23,6 +24,7 @@ type Pool struct {
lock sync.Mutex
}

// Add add a backend to the pool
func (b *Pool) Add(backend *Backend) (err error) {
b.lock.Lock()
defer b.lock.Unlock()
Expand All @@ -34,6 +36,7 @@ func (b *Pool) Add(backend *Backend) (err error) {
return
}

// Remove remove a backend from the pool
func (b *Pool) Remove(addr string) (err error) {
b.lock.Lock()
defer b.lock.Unlock()
Expand Down Expand Up @@ -64,6 +67,7 @@ func (b *Pool) AllHealthy() (backends []*Backend) {
return
}

// NextIndex returns the next index for loadbalancer interface
func (b *Pool) NextIndex() int {
return int(atomic.AddUint64(&b.current, uint64(1)) % uint64(len(b.backends)))
}
Expand Down

0 comments on commit 022b359

Please sign in to comment.