This repository has been archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmembership.go
95 lines (86 loc) · 2.15 KB
/
membership.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package rafthttp
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/CanonicalLtd/raft-membership"
"github.com/hashicorp/raft"
"github.com/pkg/errors"
)
// ChangeMembership can be used to join or leave a cluster over HTTP.
func ChangeMembership(
kind raftmembership.ChangeRequestKind,
path string,
dial Dial,
id raft.ServerID,
address, target string,
timeout time.Duration) error {
url := makeURL(path)
url.RawQuery = fmt.Sprintf("id=%s", id)
if kind == raftmembership.JoinRequest {
url.RawQuery += fmt.Sprintf("&address=%s", address)
}
url.Host = target
url.Scheme = "http"
method := membershipChangeRequestKindToMethod[kind]
request := &http.Request{
Method: method,
URL: url,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
}
remaining := timeout
var response *http.Response
var err error
for remaining > 0 {
start := time.Now()
netDial := func(network, addr string) (net.Conn, error) {
return dial(addr, remaining)
}
client := &http.Client{
Timeout: remaining,
Transport: &http.Transport{Dial: netDial},
}
response, err = client.Do(request)
// If we got a system or network error, just return it.
if err != nil {
break
}
// If we got an HTTP error, let's capture its details,
// and possibly return it if it's not retriable or we
// have hit our timeout.
if response.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(response.Body)
err = fmt.Errorf(
"http code %d '%s'", response.StatusCode,
strings.TrimSpace(string(body)))
}
// If there's a temporary failure, let's retry.
if response.StatusCode == http.StatusServiceUnavailable {
// XXX TODO: use an exponential backoff
// relative to the timeout?
time.Sleep(100 * time.Millisecond)
remaining -= time.Since(start)
continue
}
break
}
if err != nil {
return errors.Wrap(err, fmt.Sprintf("server %s failed", kind))
}
return nil
}
// Build a full url.URL object out of our path.
func makeURL(path string) *url.URL {
url, err := url.Parse(path)
if err != nil {
panic(fmt.Sprintf("invalid URL path %s", path))
}
return url
}