-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathetcd.go
More file actions
60 lines (52 loc) · 1.29 KB
/
etcd.go
File metadata and controls
60 lines (52 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package etcd
import (
"fmt"
"net/http"
"os"
"time"
"github.com/coreos/go-etcd/etcd"
)
const (
hostEnvVar = "DEIS_ETCD_1_SERVICE_HOST"
portEnvVar = "DEIS_ETCD_1_SERVICE_PORT_CLIENT"
defaultHost = "localhost"
defaultPort = "4001"
defaultRetryCycles = 2
)
var (
defaultRetrySleep = 200 * time.Millisecond
)
func CreateClientFromEnv() (*etcd.Client, error) {
host := os.Getenv(hostEnvVar)
port := os.Getenv(portEnvVar)
if host == "" {
host = defaultHost
}
if port == "" {
port = defaultPort
}
hosts := []string{
fmt.Sprintf("http://%s:%s", host, port),
}
client := etcd.NewClient(hosts)
client.CheckRetry = getCheckRetryFunc(defaultRetryCycles, defaultRetrySleep)
return client, nil
}
func getCheckRetryFunc(retryCycles int, retrySleep time.Duration) func(*etcd.Cluster, int, http.Response, error) error {
return func(c *etcd.Cluster, numReqs int, last http.Response, err error) error {
if numReqs > retryCycles*len(c.Machines) {
return fmt.Errorf("Tried and failed %d cluster connections: %s", retryCycles, err)
}
switch last.StatusCode {
case 0:
return nil
case 500:
time.Sleep(retrySleep)
return nil
case 200:
return nil
default:
return fmt.Errorf("Unhandled HTTP Error: %s %d", last.Status, last.StatusCode)
}
}
}