Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: move client function to util #5964

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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