-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpinger.go
More file actions
65 lines (55 loc) · 1.2 KB
/
pinger.go
File metadata and controls
65 lines (55 loc) · 1.2 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
61
62
63
64
65
package main
import (
"database/sql"
"log"
"time"
"github.com/hashicorp/vault/api"
)
type pinger interface {
ping() error
}
type dbPinger struct {
db *sql.DB
}
func (d dbPinger) ping() error {
return d.db.Ping()
}
// pingExternalService pings an external service with linearly increasing backoff time.
func pingExternalService(addr string, pinger pinger) error {
numBackOffIterations := 15
for i := 1; i <= numBackOffIterations; i++ {
log.Printf("Pinging %s.\n", addr)
err := pinger.ping()
if err != nil {
log.Println(err)
}
if err == nil {
log.Printf("Connected to %s.", addr)
break
}
waitDuration := time.Duration(i) * time.Second
log.Printf("Backing off for %v.\n", waitDuration)
time.Sleep(waitDuration)
if i == numBackOffIterations {
return err
}
}
return nil
}
type vaultPinger struct {
vaultClient *api.Client
path string
}
func (v *vaultPinger) ping() error {
_, err := v.vaultClient.Logical().Read(v.path)
return err
}
type vaultAppRolePinger struct {
vaultClient *api.Client
path string
options map[string]interface{}
}
func (v *vaultAppRolePinger) ping() error {
_, err := v.vaultClient.Logical().Write(v.path, v.options)
return err
}