MCPcopy
hub / github.com/adonovan/gopl.io / WaitForServer

Function WaitForServer

ch5/wait/wait.go:21–33  ·  view source on GitHub ↗

!+ WaitForServer attempts to contact the server of a URL. It tries for one minute using exponential back-off. It reports an error if all attempts fail.

(url string)

Source from the content-addressed store, hash-verified

19// It tries for one minute using exponential back-off.
20// It reports an error if all attempts fail.
21func WaitForServer(url string) error {
22 const timeout = 1 * time.Minute
23 deadline := time.Now().Add(timeout)
24 for tries := 0; time.Now().Before(deadline); tries++ {
25 _, err := http.Head(url)
26 if err == nil {
27 return nil // success
28 }
29 log.Printf("server not responding (%s); retrying...", err)
30 time.Sleep(time.Second << uint(tries)) // exponential back-off
31 }
32 return fmt.Errorf("server %s failed to respond after %s", url, timeout)
33}
34
35//!-
36

Callers 1

mainFunction · 0.85

Calls 1

AddMethod · 0.80

Tested by

no test coverage detected