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 3 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
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ github.com/elgs/gosplitargs v0.0.0-20161028071935-a491c5eeb3c8 h1:bD2/rCXwgXJm2v
github.com/elgs/gosplitargs v0.0.0-20161028071935-a491c5eeb3c8/go.mod h1:o4DgpccPNAQAlPSxo7I4L/LWNh2oyr/BBGSynrLTmZM=
github.com/eric-lindau/udpfacade v0.0.0-20190425230512-031998cc71fa h1:vcabBLB5KsNFFvLiz58J/0/Y1sP9vIMu5CO9tjNN4tU=
github.com/eric-lindau/udpfacade v0.0.0-20190425230512-031998cc71fa/go.mod h1:FgXLvXZ5hgyfdG0KcbEt7dihoivzIk9puoGiP+2isNE=
github.com/eric-lindau/udpfacade v0.0.0-20190621043444-d8c1c27add16 h1:vXNN2aHN8uRUEJYdSVhXhjZ2lIsrakx7J0j3nC1sIsE=
github.com/eric-lindau/udpfacade v0.0.0-20190621043444-d8c1c27add16/go.mod h1:FgXLvXZ5hgyfdG0KcbEt7dihoivzIk9puoGiP+2isNE=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flosch/pongo2 v0.0.0-20181225140029-79872a7b2769 h1:XToLChWPMXLomJ2InnkrmUkddaXfevrmomMTFL+MaKU=
github.com/flosch/pongo2 v0.0.0-20181225140029-79872a7b2769/go.mod h1:tbAXHifHQWNSpWbiJHpJTZH5fi3XHhDMdP//vuz9WS4=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 h1:GY1+t5Dr9OKADM64SYnQjw/w99HMYvQ0A8/JoUkxVmc=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
github.com/frankban/quicktest v1.0.0/go.mod h1:R98jIehRai+d1/3Hv2//jOVCTJhW1VBavT6B6CuGq2k=
github.com/frankban/quicktest v1.1.0 h1:Fw/voXLo2r0Tvu5uy/GV/W5XpT7LYfbrqottX3kz8YE=
github.com/frankban/quicktest v1.1.0/go.mod h1:R98jIehRai+d1/3Hv2//jOVCTJhW1VBavT6B6CuGq2k=
Expand All @@ -44,6 +48,8 @@ github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cou
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127 h1:0gkP6mzaMqkmpcJYCFOLkIBwI7xFExG03bbkOkCvUPI=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-check/check v1.0.0-20180628173108-788fd7840127 h1:3dbHpVjNKf7Myfit4Xmw4BA0JbCt47OJPhMQ5w8O3E8=
github.com/go-check/check v1.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/gogo/protobuf v1.2.0 h1:xU6/SpYbvkNYiptHJYEDRseDLvYE7wSqhYYNy0QSUzI=
Expand Down
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
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/elgs/gojq v0.0.0-20160421194050-81fa9a608a13
github.com/elgs/gosplitargs v0.0.0-20161028071935-a491c5eeb3c8 // indirect
github.com/eric-lindau/udpfacade v0.0.0-20190621043444-d8c1c27add16
github.com/flosch/pongo2 v0.0.0-20181225140029-79872a7b2769 // indirect
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 // indirect
github.com/fsouza/go-dockerclient v1.3.6
github.com/gin-contrib/cors v0.0.0-20190301062745-f9e10995c85a
github.com/gin-gonic/gin v1.3.0
Expand Down
3 changes: 3 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/eric-lindau/udpfacade v0.0.0-20190621043444-d8c1c27add16/go.mod h1:Fg
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flosch/pongo2 v0.0.0-20181225140029-79872a7b2769 h1:XToLChWPMXLomJ2InnkrmUkddaXfevrmomMTFL+MaKU=
github.com/flosch/pongo2 v0.0.0-20181225140029-79872a7b2769/go.mod h1:tbAXHifHQWNSpWbiJHpJTZH5fi3XHhDMdP//vuz9WS4=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 h1:GY1+t5Dr9OKADM64SYnQjw/w99HMYvQ0A8/JoUkxVmc=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
github.com/frankban/quicktest v1.0.0/go.mod h1:R98jIehRai+d1/3Hv2//jOVCTJhW1VBavT6B6CuGq2k=
github.com/frankban/quicktest v1.1.0 h1:Fw/voXLo2r0Tvu5uy/GV/W5XpT7LYfbrqottX3kz8YE=
github.com/frankban/quicktest v1.1.0/go.mod h1:R98jIehRai+d1/3Hv2//jOVCTJhW1VBavT6B6CuGq2k=
Expand All @@ -44,6 +46,7 @@ github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cou
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-check/check v1.0.0-20180628173108-788fd7840127 h1:3dbHpVjNKf7Myfit4Xmw4BA0JbCt47OJPhMQ5w8O3E8=
github.com/go-check/check v1.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/gogo/protobuf v1.2.0 h1:xU6/SpYbvkNYiptHJYEDRseDLvYE7wSqhYYNy0QSUzI=
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 @@ -369,3 +369,8 @@ func (this *Server) fireAndForget(clientAddr *net.UDPAddr, buf []byte) error {
func (this *Server) Stop() {
this.stop <- true
}

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