-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkin.go
56 lines (46 loc) · 1.2 KB
/
checkin.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
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
)
const maxRetries = 4
func httpCheckin(checkinURL string) (ok bool, err error) {
parameterizedCheckinURL := checkinURL + "?instance=" + strconv.Itoa(int(InstanceID))
log.Println("Going to make request to URL: ", parameterizedCheckinURL)
req, err := http.Get(parameterizedCheckinURL)
if err != nil {
log.Println("Error building Request to checkinURL: ", checkinURL)
return false, fmt.Errorf("CheckinError: Could not reach the checkin URL")
}
// defer request close
defer req.Body.Close()
if req.StatusCode == http.StatusOK {
log.Println("Successfully Checked In!")
return true, nil
}
return false, fmt.Errorf("Expected 200 response code from checkin, got: " + req.Status)
}
func checkinToHashRing(checkinURL string) bool {
backoffTime := 2
timesTried := 0
for {
if timesTried > maxRetries {
break
}
checkinOK, err := httpCheckin(checkinURL)
if err != nil {
log.Println("ERROR: httpCheckin returned: ", checkinOK)
time.Sleep(time.Duration(backoffTime) * time.Second)
timesTried++
backoffTime *= 2
}
// If we successfully checked in, return true
if checkinOK {
return true
}
}
return false
}