fix closure starting all servers

This commit is contained in:
Jimmy Zelinskie 2015-10-28 16:28:09 -04:00
parent 386e0d2abc
commit 7730cd9a16

View file

@ -38,6 +38,11 @@ func init() {
flag.StringVar(&configPath, "config", "", "path to the configuration file") flag.StringVar(&configPath, "config", "", "path to the configuration file")
} }
type server interface {
Serve()
Stop()
}
// Boot starts Chihaya. By exporting this function, anyone can import their own // Boot starts Chihaya. By exporting this function, anyone can import their own
// custom drivers into their own package main and then call chihaya.Boot. // custom drivers into their own package main and then call chihaya.Boot.
func Boot() { func Boot() {
@ -69,33 +74,30 @@ func Boot() {
glog.Fatal("New: ", err) glog.Fatal("New: ", err)
} }
var servers []interface { var servers []server
Serve()
Stop()
}
if cfg.APIConfig.ListenAddr != "" { if cfg.APIConfig.ListenAddr != "" {
srv := api.NewServer(cfg, tkr) servers = append(servers, api.NewServer(cfg, tkr))
servers = append(servers, srv)
} }
if cfg.HTTPConfig.ListenAddr != "" { if cfg.HTTPConfig.ListenAddr != "" {
srv := http.NewServer(cfg, tkr) servers = append(servers, http.NewServer(cfg, tkr))
servers = append(servers, srv)
} }
if cfg.UDPConfig.ListenAddr != "" { if cfg.UDPConfig.ListenAddr != "" {
srv := udp.NewServer(cfg, tkr) servers = append(servers, udp.NewServer(cfg, tkr))
servers = append(servers, srv)
} }
var wg sync.WaitGroup var wg sync.WaitGroup
for _, srv := range servers { for _, srv := range servers {
wg.Add(1) wg.Add(1)
go func() {
// If you don't explicitly pass the server, every goroutine captures the
// last server in the list.
go func(srv server) {
defer wg.Done() defer wg.Done()
srv.Serve() srv.Serve()
}() }(srv)
} }
shutdown := make(chan os.Signal) shutdown := make(chan os.Signal)