From e0fcbbe09013b23b4f12cbc3622c3ec20f1aee64 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Wed, 21 Jun 2023 22:13:18 +0800 Subject: [PATCH] remove wg Signed-off-by: lhy1024 --- pkg/mcs/resourcemanager/server/server.go | 2 +- pkg/mcs/tso/server/server.go | 2 +- pkg/utils/etcdutil/etcdutil.go | 9 +++------ pkg/utils/etcdutil/etcdutil_test.go | 16 ++++++++-------- server/server.go | 4 ++-- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/pkg/mcs/resourcemanager/server/server.go b/pkg/mcs/resourcemanager/server/server.go index 59b32aaf506a..de3edd5fe5a8 100644 --- a/pkg/mcs/resourcemanager/server/server.go +++ b/pkg/mcs/resourcemanager/server/server.go @@ -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 } diff --git a/pkg/mcs/tso/server/server.go b/pkg/mcs/tso/server/server.go index 9f8aa3f7ff12..fa4fd7612996 100644 --- a/pkg/mcs/tso/server/server.go +++ b/pkg/mcs/tso/server/server.go @@ -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 } diff --git a/pkg/utils/etcdutil/etcdutil.go b/pkg/utils/etcdutil/etcdutil.go index 6ea809ef70f9..6b4fc16e6737 100644 --- a/pkg/utils/etcdutil/etcdutil.go +++ b/pkg/utils/etcdutil/etcdutil.go @@ -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()) @@ -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() @@ -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 { @@ -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() } diff --git a/pkg/utils/etcdutil/etcdutil_test.go b/pkg/utils/etcdutil/etcdutil_test.go index 3a0a9237d5bc..1e380fa86849 100644 --- a/pkg/utils/etcdutil/etcdutil_test.go +++ b/pkg/utils/etcdutil/etcdutil_test.go @@ -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() }() @@ -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() }() @@ -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() }() @@ -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() @@ -697,7 +697,7 @@ 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") @@ -705,7 +705,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() { // 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 @@ -713,7 +713,7 @@ func (suite *loopWatcherTestSuite) TestWatcherBreak() { // 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") diff --git a/server/server.go b/server/server.go index e2c068fbf818..80ebd15c34ed 100644 --- a/server/server.go +++ b/server/server.go @@ -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) { @@ -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.