func newEtcdClient() (client *etcd.Client) { maxConnectRetries := 12 for maxConnectRetries > 0 { if _, _, err := tools.GetEtcdVersion(*etcd_server); err != nil { log.Fatalf("Failed to connect to etcd server: %v, error: %v", *etcd_server, err) if maxConnectRetries > 0 { log.Println("Retrying request after 5 second sleep.") time.Sleep(5 * time.Second) maxConnectRetries-- } else { return nil } } else { log.Printf("Etcd server found: %v", *etcd_server) break } } client = etcd.NewClient([]string{*etcd_server}) if client == nil { return nil } client.SyncCluster() return client }
func newEtcdClient(etcdServer string) (*etcd.Client, error) { var ( client *etcd.Client err error ) for attempt := 1; attempt <= maxConnectAttempts; attempt++ { if _, err = tools.GetEtcdVersion(etcdServer); err == nil { break } if attempt == maxConnectAttempts { break } glog.Infof("[Attempt: %d] Attempting access to etcd after 5 second sleep", attempt) time.Sleep(5 * time.Second) } if err != nil { return nil, fmt.Errorf("failed to connect to etcd server: %v, error: %v", etcdServer, err) } glog.Infof("Etcd server found: %v", etcdServer) // loop until we have > 0 machines && machines[0] != "" poll, timeout := 1*time.Second, 10*time.Second if err := wait.Poll(poll, timeout, func() (bool, error) { if client = etcd.NewClient([]string{etcdServer}); client == nil { return false, fmt.Errorf("etcd.NewClient returned nil") } client.SyncCluster() machines := client.GetCluster() if len(machines) == 0 || len(machines[0]) == 0 { return false, nil } return true, nil }); err != nil { return nil, fmt.Errorf("Timed out after %s waiting for at least 1 synchronized etcd server in the cluster. Error: %v", timeout, err) } return client, nil }