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 channel that signals server is completed #263

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion src/api/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions src/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 6 additions & 2 deletions src/server/tcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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")

Expand All @@ -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 */
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -270,6 +273,7 @@ func (this *Server) Listen() (err error) {
sniEnabled := this.cfg.Sni != nil

go func() {
defer func() { this.completed <- struct{}{} }()
for {
conn, err := this.listener.Accept()

Expand Down
8 changes: 6 additions & 2 deletions src/server/udp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -116,7 +119,7 @@ func (cp *connPool) close() {
/**
* 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{
Expand All @@ -131,6 +134,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),
}

Expand Down Expand Up @@ -158,7 +162,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)
Expand Down Expand Up @@ -241,6 +244,7 @@ func (this *Server) serve() {

// Main loop goroutine - reads incoming data and decides what to do
go func() {
defer func() { this.completed <- struct{}{} }()

defer cp.close()

Expand Down