Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modify backends #256

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/api/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/yyyar/gobetween/config"
"github.com/yyyar/gobetween/core"
"github.com/yyyar/gobetween/manager"
"github.com/yyyar/gobetween/stats"
)
Expand Down Expand Up @@ -65,6 +66,28 @@ func attachServers(app *gin.RouterGroup) {
c.IndentedJSON(http.StatusOK, nil)
})

/**
* Update backends with name :name
* pass in []core.Backend
*/
app.POST("/servers/:name/backends", func(c *gin.Context) {

name := c.Param("name")

cfg := []core.Backend{}
if err := c.BindJSON(&cfg); err != nil {
c.IndentedJSON(http.StatusBadRequest, err.Error())
return
}

if err := manager.UpdateBackends(name, &cfg); err != nil {
c.IndentedJSON(http.StatusConflict, err.Error())
return
}

c.IndentedJSON(http.StatusOK, nil)
})

/**
* Get server stats
*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type Server interface {
*/
Stop()

/**
* UpdateBackends allows you to set a new backend config
*/
UpdateBackends(backends *[]Backend)

/**
* Get server configuration
*/
Expand Down
7 changes: 6 additions & 1 deletion src/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ func (this *Discovery) send() bool {
case <-this.stop:
return false
default:
this.out <- *this.backends
this.SendBackends(this.backends)
return true
}
}

// SendBackends updates the backends
func (this *Discovery) SendBackends(backends *[]core.Backend) {
this.out <- *backends
}

/**
* wait waits for interval or stop
* returns true if waiting was successfull
Expand Down
14 changes: 14 additions & 0 deletions src/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ func Create(name string, cfg config.Server) error {
return nil
}

// UpdateBackends allows you to update the backends of an existing server
func UpdateBackends(name string, backends *[]core.Backend) error {
servers.RLock()
server, ok := servers.m[name]
servers.RUnlock()
if !ok {
return errors.New("Server with this name does not exist: " + name)
}

server.UpdateBackends(backends)

return nil
}

/**
* Delete server stopping all active connections
*/
Expand Down
5 changes: 5 additions & 0 deletions src/server/tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ func (this *Server) Start() error {
return nil
}

// UpdateBackends updates the backends
func (this *Server) UpdateBackends(backends *[]core.Backend) {
this.scheduler.Discovery.SendBackends(backends)
}

/**
* Handle client disconnection
*/
Expand Down
5 changes: 5 additions & 0 deletions src/server/udp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,8 @@ func (this *Server) fireAndForget(pool *connPool, clientAddr *net.UDPAddr, buf [
func (this *Server) Stop() {
this.stop <- true
}

// UpdateBackends updates the backends
func (this *Server) UpdateBackends(backends *[]core.Backend) {
this.scheduler.Discovery.SendBackends(backends)
}