Skip to content

Commit

Permalink
remove wg
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Jun 21, 2023
1 parent 2ef12f1 commit e0fcbbe
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pkg/mcs/resourcemanager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (s *Server) initClient() error {
if err != nil {
return err
}
s.etcdClient, s.httpClient, err = etcdutil.CreateClients(s.ctx, &s.serverLoopWg, tlsConfig, []url.URL(u))
s.etcdClient, s.httpClient, err = etcdutil.CreateClients(tlsConfig, []url.URL(u))
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/tso/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (s *Server) initClient() error {
if err != nil {
return err
}
s.etcdClient, s.httpClient, err = etcdutil.CreateClients(s.ctx, &s.serverLoopWg, tlsConfig, s.backendUrls)
s.etcdClient, s.httpClient, err = etcdutil.CreateClients(s.ctx, tlsConfig, s.backendUrls)
return err
}

Expand Down
9 changes: 3 additions & 6 deletions pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func newClient(tlsConfig *tls.Config, acURL ...string) (*clientv3.Client, error)
}

// CreateEtcdClient creates etcd v3 client with detecting endpoints.
func CreateEtcdClient(ctx context.Context, wg *sync.WaitGroup, tlsConfig *tls.Config, acURLs []url.URL) (*clientv3.Client, error) {
func CreateEtcdClient(ctx context.Context, tlsConfig *tls.Config, acURLs []url.URL) (*clientv3.Client, error) {
urls := make([]string, 0, len(acURLs))
for _, u := range acURLs {
urls = append(urls, u.String())
Expand All @@ -249,10 +249,8 @@ func CreateEtcdClient(ctx context.Context, wg *sync.WaitGroup, tlsConfig *tls.Co
return client, err
}

wg.Add(2)
go func(ctx context.Context, client *clientv3.Client) {
defer logutil.LogPanic()
defer wg.Done()
ticker := time.NewTicker(tickerInterval)
defer ticker.Stop()
lastReset := time.Now()
Expand Down Expand Up @@ -292,7 +290,6 @@ func CreateEtcdClient(ctx context.Context, wg *sync.WaitGroup, tlsConfig *tls.Co
// Notes: use another goroutine to update endpoints to avoid blocking health check in the first goroutine.
go func(ctx context.Context, client *clientv3.Client) {
defer logutil.LogPanic()
defer wg.Done()
ticker := time.NewTicker(tickerInterval)
defer ticker.Stop()
for {
Expand Down Expand Up @@ -433,8 +430,8 @@ func isEqual(a, b []string) bool {
}

// CreateClients creates etcd v3 client and http client.
func CreateClients(ctx context.Context, wg *sync.WaitGroup, tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, *http.Client, error) {
client, err := CreateEtcdClient(ctx, wg, tlsConfig, acUrls)
func CreateClients(ctx context.Context, tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, *http.Client, error) {
client, err := CreateEtcdClient(ctx, tlsConfig, acUrls)
if err != nil {
return nil, nil, errs.ErrNewEtcdClient.Wrap(err).GenWithStackByCause()
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/utils/etcdutil/etcdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestEtcdClientSync(t *testing.T) {
ep1 := cfg1.LCUrls[0].String()
urls, err := types.NewURLs([]string{ep1})
re.NoError(err)
client1, err := CreateEtcdClient(ctx, &wg, nil, urls)
client1, err := CreateEtcdClient(ctx, nil, urls)
defer func() {
client1.Close()
}()
Expand Down Expand Up @@ -311,12 +311,12 @@ func TestEtcdScaleInAndOutWithoutMultiPoint(t *testing.T) {
// Create two etcd clients with etcd1 as endpoint.
urls, err := types.NewURLs([]string{ep1})
re.NoError(err)
client1, err := CreateEtcdClient(ctx, &wg, nil, urls) // execute member change operation with this client
client1, err := CreateEtcdClient(ctx,nil, urls) // execute member change operation with this client
defer func() {
client1.Close()
}()
re.NoError(err)
client2, err := CreateEtcdClient(ctx, &wg, nil, urls) // check member change with this client
client2, err := CreateEtcdClient(ctx,nil, urls) // check member change with this client
defer func() {
client2.Close()
}()
Expand Down Expand Up @@ -359,7 +359,7 @@ func checkEtcdWithHangLeader(t *testing.T) error {
// Create a etcd client with etcd1 as endpoint.
urls, err := types.NewURLs([]string{proxyAddr})
re.NoError(err)
client1, err := CreateEtcdClient(ctx, &wg, nil, urls)
client1, err := CreateEtcdClient(ctx, nil, urls)
defer func() {
client1.Close()
}()
Expand Down Expand Up @@ -494,7 +494,7 @@ func (suite *loopWatcherTestSuite) SetupSuite() {
ep1 := suite.config.LCUrls[0].String()
urls, err := types.NewURLs([]string{ep1})
suite.NoError(err)
suite.client, err = CreateEtcdClient(suite.ctx, &suite.wg, nil, urls)
suite.client, err = CreateEtcdClient(suite.ctx, nil, urls)
suite.NoError(err)
suite.cleans = append(suite.cleans, func() {
suite.client.Close()
Expand Down Expand Up @@ -697,23 +697,23 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() {

// Case2: close the etcd client and put a new value after watcher restarts
suite.client.Close()
suite.client, err = CreateEtcdClient(suite.ctx, &suite.wg, nil, suite.config.LCUrls)
suite.client, err = CreateEtcdClient(suite.ctx, nil, suite.config.LCUrls)
suite.NoError(err)
watcher.updateClientCh <- suite.client
suite.put("TestWatcherBreak", "2")
checkCache("2")

// Case3: close the etcd client and put a new value before watcher restarts
suite.client.Close()
suite.client, err = CreateEtcdClient(suite.ctx, &suite.wg, nil, suite.config.LCUrls)
suite.client, err = CreateEtcdClient(suite.ctx, nil, suite.config.LCUrls)
suite.NoError(err)
suite.put("TestWatcherBreak", "3")
watcher.updateClientCh <- suite.client
checkCache("3")

// Case4: close the etcd client and put a new value with compact
suite.client.Close()
suite.client, err = CreateEtcdClient(suite.ctx, &suite.wg, nil, suite.config.LCUrls)
suite.client, err = CreateEtcdClient(suite.ctx, nil, suite.config.LCUrls)
suite.NoError(err)
suite.put("TestWatcherBreak", "4")
resp, err := EtcdKVGet(suite.client, "TestWatcherBreak")
Expand Down
4 changes: 2 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (s *Server) startClient() (*clientv3.Client, *http.Client, error) {
if err != nil {
return nil, nil, err
}
return etcdutil.CreateClients(s.ctx, &s.serverLoopWg, tlsConfig, etcdCfg.ACUrls)
return etcdutil.CreateClients(s.ctx, tlsConfig, etcdCfg.ACUrls)
}

func (s *Server) startElectionClient() (*clientv3.Client, error) {
Expand All @@ -392,7 +392,7 @@ func (s *Server) startElectionClient() (*clientv3.Client, error) {
return nil, err
}

return etcdutil.CreateEtcdClient(s.ctx, &s.serverLoopWg, tlsConfig, etcdCfg.ACUrls)
return etcdutil.CreateEtcdClient(s.ctx, tlsConfig, etcdCfg.ACUrls)
}

// AddStartCallback adds a callback in the startServer phase.
Expand Down

0 comments on commit e0fcbbe

Please sign in to comment.