diff --git a/pkg/cluster/etcd_service_discovery.go b/pkg/cluster/etcd_service_discovery.go index 912eca6d..c7247c76 100644 --- a/pkg/cluster/etcd_service_discovery.go +++ b/pkg/cluster/etcd_service_discovery.go @@ -24,17 +24,17 @@ import ( "context" "encoding/json" "fmt" - "strings" - "sync" - "time" "github.com/topfreegames/pitaya/v3/pkg/config" "github.com/topfreegames/pitaya/v3/pkg/constants" "github.com/topfreegames/pitaya/v3/pkg/logger" "github.com/topfreegames/pitaya/v3/pkg/util" - clientv3 "go.etcd.io/etcd/client/v3" logutil "go.etcd.io/etcd/client/pkg/v3/logutil" + clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/client/v3/namespace" "google.golang.org/grpc" + "strings" + "sync" + "time" ) type etcdServiceDiscovery struct { @@ -81,14 +81,14 @@ func NewEtcdServiceDiscovery( client = cli[0] } sd := &etcdServiceDiscovery{ - running: false, - server: server, - serverMapByType: make(map[string]map[string]*Server), - listeners: make([]SDListener, 0), - stopChan: make(chan bool), - stopLeaseChan: make(chan bool), - appDieChan: appDieChan, - cli: client, + running: false, + server: server, + serverMapByType: make(map[string]map[string]*Server), + listeners: make([]SDListener, 0), + stopChan: make(chan bool), + stopLeaseChan: make(chan bool), + appDieChan: appDieChan, + cli: client, syncServersRunning: make(chan bool), } @@ -300,7 +300,7 @@ func (sd *etcdServiceDiscovery) GetServersByType(serverType string) (map[string] // Create a new map to avoid concurrent read and write access to the // map, this also prevents accidental changes to the list of servers // kept by the service discovery. - ret := make(map[string]*Server,len(sd.serverMapByType[serverType])) + ret := make(map[string]*Server, len(sd.serverMapByType[serverType])) for k, v := range sd.serverMapByType[serverType] { ret[k] = v } @@ -615,16 +615,15 @@ func (sd *etcdServiceDiscovery) revoke() error { go func() { defer close(c) logger.Log.Debug("waiting for etcd revoke") - _, err := sd.cli.Revoke(context.TODO(), sd.leaseID) + ctx, cancel := context.WithTimeout(context.Background(), sd.revokeTimeout) + _, err := sd.cli.Revoke(ctx, sd.leaseID) + cancel() c <- err logger.Log.Debug("finished waiting for etcd revoke") }() select { case err := <-c: return err // completed normally - case <-time.After(sd.revokeTimeout): - logger.Log.Warn("timed out waiting for etcd revoke") - return nil // timed out } }