Skip to content

Commit

Permalink
Fix possible server startup concurrency (topfreegames#108)
Browse files Browse the repository at this point in the history
The fix is done by initializing the service discovery
module as the last one, ensuring other modules have already
initialized correctly
  • Loading branch information
andrehp authored Aug 26, 2019
1 parent b07f08b commit d7bd48f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
16 changes: 8 additions & 8 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,18 @@ func Start() {
app.serviceDiscovery.AddListener(app.rpcClient.(*cluster.GRPCClient))
}

err := RegisterModuleBefore(app.serviceDiscovery, "serviceDiscovery")
if err != nil {
logger.Log.Fatal("failed to register service discovery module: %s", err.Error())
}
err = RegisterModuleBefore(app.rpcServer, "rpcServer")
if err != nil {
if err := RegisterModuleBefore(app.rpcServer, "rpcServer"); err != nil {
logger.Log.Fatal("failed to register rpc server module: %s", err.Error())
}
err = RegisterModuleBefore(app.rpcClient, "rpcClient")
if err != nil {
if err := RegisterModuleBefore(app.rpcClient, "rpcClient"); err != nil {
logger.Log.Fatal("failed to register rpc client module: %s", err.Error())
}
// set the service discovery as the last module to be started to ensure
// all modules have been properly initialized before the server starts
// receiving requests from other pitaya servers
if err := RegisterModuleAfter(app.serviceDiscovery, "serviceDiscovery"); err != nil {
logger.Log.Fatal("failed to register service discovery module: %s", err.Error())
}

app.router.SetServiceDiscovery(app.serviceDiscovery)

Expand Down
4 changes: 1 addition & 3 deletions cluster/etcd_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func (sd *etcdServiceDiscovery) bootstrapServer(server *Server) error {
}

sd.SyncServers()

return nil
}

Expand Down Expand Up @@ -346,8 +345,7 @@ func (sd *etcdServiceDiscovery) Init() error {
sd.cli.Watcher = namespace.NewWatcher(sd.cli.Watcher, sd.etcdPrefix)
sd.cli.Lease = namespace.NewLease(sd.cli.Lease, sd.etcdPrefix)

err = sd.bootstrap()
if err != nil {
if err = sd.bootstrap(); err != nil {
return err
}

Expand Down

0 comments on commit d7bd48f

Please sign in to comment.