Skip to content

Commit

Permalink
server: move client function to util (#5964)
Browse files Browse the repository at this point in the history
close #5837

Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
lhy1024 and ti-chi-bot authored Feb 13, 2023
1 parent 7944000 commit 0ed822c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
28 changes: 28 additions & 0 deletions pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import (
)

const (
// defaultEtcdClientTimeout is the default timeout for etcd client.
defaultEtcdClientTimeout = 3 * time.Second

// DefaultDialTimeout is the maximum amount of time a dial will wait for a
// connection to setup. 30s is long enough for most of the network conditions.
DefaultDialTimeout = 30 * time.Second
Expand Down Expand Up @@ -202,3 +205,28 @@ func NewTestSingleConfig(t *testing.T) *embed.Config {
cfg.ClusterState = embed.ClusterStateFlagNew
return cfg
}

// CreateClients creates etcd v3 client and http client.
func CreateClients(tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, *http.Client, error) {
endpoints := []string{acUrls[0].String()}
lgc := zap.NewProductionConfig()
lgc.Encoding = log.ZapEncodingName
client, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: defaultEtcdClientTimeout,
TLS: tlsConfig,
LogConfig: &lgc,
})
if err != nil {
return nil, nil, errs.ErrNewEtcdClient.Wrap(err).GenWithStackByCause()
}

httpClient := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
log.Info("create etcd v3 client", zap.Strings("endpoints", endpoints))
return client, httpClient, nil
}
25 changes: 1 addition & 24 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import (
)

const (
etcdTimeout = time.Second * 3
serverMetricsInterval = time.Minute
leaderTickInterval = 50 * time.Millisecond
// pdRootPath for all pd servers.
Expand Down Expand Up @@ -321,29 +320,7 @@ func startClient(cfg *config.Config) (*clientv3.Client, *http.Client, error) {
if err != nil {
return nil, nil, err
}

endpoints := []string{etcdCfg.ACUrls[0].String()}
log.Info("create etcd v3 client", zap.Strings("endpoints", endpoints), zap.Reflect("cert", cfg.Security))

lgc := zap.NewProductionConfig()
lgc.Encoding = log.ZapEncodingName
client, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: etcdTimeout,
TLS: tlsConfig,
LogConfig: &lgc,
})
if err != nil {
return nil, nil, errs.ErrNewEtcdClient.Wrap(err).GenWithStackByCause()
}

httpClient := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
return client, httpClient, nil
return etcdutil.CreateClients(tlsConfig, etcdCfg.ACUrls)
}

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

0 comments on commit 0ed822c

Please sign in to comment.