Browse Source

etcdserver: add timeout param on getClusterFromRemotePeers

It sets 10s timeout for public GetClusterFromRemotePeers.

This helps the following cases to work well in high latency scenario:

1. proxy sync members from the cluster
2. newly-joined member sync members from the cluster

Besides 10s request timeout, the request is also controlled by dial
timeout and read connection timeout.
Yicheng Qin 10 years ago
parent
commit
f3bfcb9dee
1 changed files with 6 additions and 4 deletions
  1. 6 4
      etcdserver/cluster_util.go

+ 6 - 4
etcdserver/cluster_util.go

@@ -30,7 +30,7 @@ import (
 // isMemberBootstrapped tries to check if the given member has been bootstrapped
 // isMemberBootstrapped tries to check if the given member has been bootstrapped
 // in the given cluster.
 // in the given cluster.
 func isMemberBootstrapped(cl *cluster, member string, tr *http.Transport) bool {
 func isMemberBootstrapped(cl *cluster, member string, tr *http.Transport) bool {
-	rcl, err := getClusterFromRemotePeers(getRemotePeerURLs(cl, member), false, tr)
+	rcl, err := getClusterFromRemotePeers(getRemotePeerURLs(cl, member), time.Second, false, tr)
 	if err != nil {
 	if err != nil {
 		return false
 		return false
 	}
 	}
@@ -50,15 +50,17 @@ func isMemberBootstrapped(cl *cluster, member string, tr *http.Transport) bool {
 // these URLs. The first URL to provide a response is used. If no URLs provide
 // these URLs. The first URL to provide a response is used. If no URLs provide
 // a response, or a Cluster cannot be successfully created from a received
 // a response, or a Cluster cannot be successfully created from a received
 // response, an error is returned.
 // response, an error is returned.
+// Each request has a 10-second timeout. Because the upper limit of TTL is 5s,
+// 10 second is enough for building connection and finishing request.
 func GetClusterFromRemotePeers(urls []string, tr *http.Transport) (*cluster, error) {
 func GetClusterFromRemotePeers(urls []string, tr *http.Transport) (*cluster, error) {
-	return getClusterFromRemotePeers(urls, true, tr)
+	return getClusterFromRemotePeers(urls, 10*time.Second, true, tr)
 }
 }
 
 
 // If logerr is true, it prints out more error messages.
 // If logerr is true, it prints out more error messages.
-func getClusterFromRemotePeers(urls []string, logerr bool, tr *http.Transport) (*cluster, error) {
+func getClusterFromRemotePeers(urls []string, timeout time.Duration, logerr bool, tr *http.Transport) (*cluster, error) {
 	cc := &http.Client{
 	cc := &http.Client{
 		Transport: tr,
 		Transport: tr,
-		Timeout:   time.Second,
+		Timeout:   timeout,
 	}
 	}
 	for _, u := range urls {
 	for _, u := range urls {
 		resp, err := cc.Get(u + "/members")
 		resp, err := cc.Get(u + "/members")