From 5494a4a5e13c95161b18bad8d5e1efca94e52dd0 Mon Sep 17 00:00:00 2001 From: Knic Knic Date: Thu, 23 Jan 2020 22:39:50 -0800 Subject: [PATCH 1/2] Add channel that signals server is completed --- src/api/servers.go | 2 +- src/manager/manager.go | 6 +++--- src/server/server.go | 6 +++--- src/server/tcp/server.go | 8 ++++++-- src/server/udp/server.go | 10 +++++++--- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/api/servers.go b/src/api/servers.go index 9060f426..d92e27be 100644 --- a/src/api/servers.go +++ b/src/api/servers.go @@ -57,7 +57,7 @@ func attachServers(app *gin.RouterGroup) { return } - if err := manager.Create(name, cfg); err != nil { + if err := manager.Create(name, cfg, make(chan struct{}, 1)); err != nil { c.IndentedJSON(http.StatusConflict, err.Error()) return } diff --git a/src/manager/manager.go b/src/manager/manager.go index a203a6dd..39852128 100644 --- a/src/manager/manager.go +++ b/src/manager/manager.go @@ -61,7 +61,7 @@ func Initialize(cfg config.Config) { // Go through config and start servers for each server for name, serverCfg := range cfg.Servers { - err := Create(name, serverCfg) + err := Create(name, serverCfg, make(chan struct{}, 1)) if err != nil { log.Fatal(err) } @@ -181,7 +181,7 @@ func Get(name string) interface{} { /** * Create new server and launch it */ -func Create(name string, cfg config.Server) error { +func Create(name string, cfg config.Server, completed chan <- struct{}) error { servers.Lock() defer servers.Unlock() @@ -195,7 +195,7 @@ func Create(name string, cfg config.Server) error { return err } - server, err := server.New(name, c) + server, err := server.New(name, c, completed) if err != nil { return err diff --git a/src/server/server.go b/src/server/server.go index 4845e95c..dccc9a88 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -19,12 +19,12 @@ import ( /** * Creates new Server based on cfg.Protocol */ -func New(name string, cfg config.Server) (core.Server, error) { +func New(name string, cfg config.Server, completed chan<- struct{}) (core.Server, error) { switch cfg.Protocol { case "tls", "tcp": - return tcp.New(name, cfg) + return tcp.New(name, cfg, completed) case "udp": - return udp.New(name, cfg) + return udp.New(name, cfg, completed) default: return nil, errors.New("Can't create server for protocol " + cfg.Protocol) } diff --git a/src/server/tcp/server.go b/src/server/tcp/server.go index 6c936771..675e0374 100644 --- a/src/server/tcp/server.go +++ b/src/server/tcp/server.go @@ -70,6 +70,9 @@ type Server struct { /* Get certificate filled by external service */ GetCertificate func(*tls.ClientHelloInfo) (*tls.Certificate, error) + /* A channel that helps to determine when the server has completed */ + completed chan<- struct{} + /* ----- modules ----- */ /* Access module checks if client is allowed to connect */ @@ -79,7 +82,7 @@ type Server struct { /** * Creates new server instance */ -func New(name string, cfg config.Server) (*Server, error) { +func New(name string, cfg config.Server, completed chan<- struct{}) (*Server, error) { log := logging.For("server") @@ -101,6 +104,7 @@ func New(name string, cfg config.Server) (*Server, error) { Healthcheck: healthcheck.New(cfg.Healthcheck.Kind, *cfg.Healthcheck), StatsHandler: statsHandler, }, + completed: completed, } /* Add access if needed */ @@ -134,7 +138,6 @@ func (this *Server) Cfg() config.Server { * Start server */ func (this *Server) Start() error { - var err error this.tlsConfig, err = tlsutil.MakeTlsConfig(this.cfg.Tls, this.GetCertificate) if err != nil { @@ -275,6 +278,7 @@ func (this *Server) Listen() (err error) { if err != nil { log.Error(err) + this.completed <- struct{}{} return } diff --git a/src/server/udp/server.go b/src/server/udp/server.go index 9483c464..0cc6c2db 100644 --- a/src/server/udp/server.go +++ b/src/server/udp/server.go @@ -13,6 +13,7 @@ import ( "sync/atomic" "time" + "github.com/eric-lindau/udpfacade" "github.com/yyyar/gobetween/balance" "github.com/yyyar/gobetween/config" "github.com/yyyar/gobetween/core" @@ -24,7 +25,6 @@ import ( "github.com/yyyar/gobetween/server/udp/session" "github.com/yyyar/gobetween/stats" "github.com/yyyar/gobetween/utils" - "github.com/eric-lindau/udpfacade" ) const UDP_PACKET_SIZE = 65507 @@ -54,6 +54,9 @@ type Server struct { /* Stop channel */ stop chan bool + /* A channel that helps to determine when the server has completed */ + completed chan<- struct{} + /* ----- modules ----- */ /* Access module checks if client is allowed to connect */ @@ -67,7 +70,7 @@ type Server struct { /** * Creates new UDP server */ -func New(name string, cfg config.Server) (*Server, error) { +func New(name string, cfg config.Server, completed chan <- struct{}) (*Server, error) { statsHandler := stats.NewHandler(name) scheduler := &scheduler.Scheduler{ @@ -82,6 +85,7 @@ func New(name string, cfg config.Server) (*Server, error) { cfg: cfg, scheduler: scheduler, stop: make(chan bool), + completed: completed, sessions: make(map[string]*session.Session), } @@ -109,7 +113,6 @@ func (this *Server) Cfg() config.Server { * Starts server */ func (this *Server) Start() error { - // Start listening if err := this.listen(); err != nil { return fmt.Errorf("Could not start listening UDP: %v", err) @@ -194,6 +197,7 @@ func (this *Server) serve() { if err != nil { if atomic.LoadUint32(&this.stopped) == 1 { + this.completed<- struct{}{} return } From fab03d963dd27d77f2ab4678237add53e434d162 Mon Sep 17 00:00:00 2001 From: Knic Knic Date: Thu, 23 Jan 2020 23:49:52 -0800 Subject: [PATCH 2/2] push to completed in defer --- go.sum | 5 +++++ src/server/tcp/server.go | 2 +- src/server/udp/server.go | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index 5834ab5c..3811da17 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -44,6 +48,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= diff --git a/src/server/tcp/server.go b/src/server/tcp/server.go index 675e0374..8368e9e1 100644 --- a/src/server/tcp/server.go +++ b/src/server/tcp/server.go @@ -273,12 +273,12 @@ func (this *Server) Listen() (err error) { sniEnabled := this.cfg.Sni != nil go func() { + defer func() { this.completed <- struct{}{} }() for { conn, err := this.listener.Accept() if err != nil { log.Error(err) - this.completed <- struct{}{} return } diff --git a/src/server/udp/server.go b/src/server/udp/server.go index 0cc6c2db..4cad1d61 100644 --- a/src/server/udp/server.go +++ b/src/server/udp/server.go @@ -190,6 +190,7 @@ func (this *Server) serve() { // Main loop goroutine - reads incoming data and decides what to do go func() { + defer func() { this.completed <- struct{}{} }() buf := make([]byte, UDP_PACKET_SIZE) for { @@ -197,7 +198,6 @@ func (this *Server) serve() { if err != nil { if atomic.LoadUint32(&this.stopped) == 1 { - this.completed<- struct{}{} return }