-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
35 lines (32 loc) · 799 Bytes
/
main.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
// waitserver waits for the server reachability specified by the command line.
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
log.SetPrefix("waitserver ")
log.SetFlags(0)
for _, url := range os.Args[1:] {
if err := WaitForServer(url); err != nil {
log.Fatalf("Unreachable: %v\n", err)
}
}
}
// WaitForServer wait for the server pointed to by url.
func WaitForServer(url string) error {
const timeout = 30 * time.Second
deadline := time.Now().Add(timeout)
for retries := 0; time.Now().Before(deadline); retries++ {
_, err := http.Head(url)
if err == nil {
return nil
}
log.Printf("server not responding (%v), retrying...", err)
time.Sleep(time.Second << uint(retries))
}
return fmt.Errorf("server failed to respond after %s", timeout)
}