Skip to content

Commit 72492a6

Browse files
committed
add in pollEndpoint dep
1 parent f15036d commit 72492a6

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

sharness/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ BINS += bin/ipfs-3-to-4
2020
BINS += bin/ipfs-update
2121
BINS += bin/random-files
2222
BINS += bin/go-sleep
23+
BINS += bin/pollEndpoint
2324

2425
# Source files location
2526
FS_REPO_SRC = ../
@@ -28,6 +29,7 @@ IPFS_1_TO_2_SRC = ../ipfs-1-to-2
2829
IPFS_2_TO_3_SRC = ../ipfs-2-to-3
2930
IPFS_3_TO_4_SRC = ../ipfs-3-to-4
3031
IPFS_UPDATE_SRC = ../Godeps/_workspace/src/github.com/ipfs/ipfs-update
32+
POLL_ENDPOINT_SRC = ./dependencies/pollEndpoint
3133

3234
# User might want to override those on the command line
3335
GOFLAGS =
@@ -93,6 +95,10 @@ bin/ipfs-3-to-4: $(call find_go_files, $(IPFS_3_TO_4_SRC)) BUILD-OPTIONS
9395
@echo "*** installing $@ ***"
9496
go build $(GOFLAGS) -o $@ $(IPFS_3_TO_4_SRC)
9597

98+
bin/pollEndpoint: $(call find_go_files, $(POLL_ENDPOINT_SRC)) BUILD-OPTIONS
99+
@echo "*** installing $@ ***"
100+
go build $(GOFLAGS) -o $@ $(POLL_ENDPOINT_SRC)
101+
96102
BUILD-OPTIONS: FORCE
97103
@bin/checkflags '$@' '$(GOFLAGS)' '*** new Go flags ***'
98104

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// pollEndpoint is a helper utility that waits for a http endpoint to be reachable and return with http.StatusOK
2+
package main
3+
4+
import (
5+
"flag"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"net/url"
10+
"os"
11+
"time"
12+
13+
logging "gx/ipfs/QmNQynaz7qfriSUJkiEZUrm2Wen1u3Kj9goZzWtrPyu7XR/go-log"
14+
manet "gx/ipfs/QmPpRcbNUXauP3zWZ1NJMLWpe4QnmEHrd2ba2D3yqWznw7/go-multiaddr-net"
15+
ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
16+
)
17+
18+
var (
19+
host = flag.String("host", "/ip4/127.0.0.1/tcp/5001", "the multiaddr host to dial on")
20+
endpoint = flag.String("ep", "/version", "which http endpoint path to hit")
21+
tries = flag.Int("tries", 10, "how many tries to make before failing")
22+
timeout = flag.Duration("tout", time.Second, "how long to wait between attempts")
23+
verbose = flag.Bool("v", false, "verbose logging")
24+
)
25+
26+
var log = logging.Logger("pollEndpoint")
27+
28+
func main() {
29+
flag.Parse()
30+
31+
// extract address from host flag
32+
addr, err := ma.NewMultiaddr(*host)
33+
if err != nil {
34+
log.Fatal("NewMultiaddr() failed: ", err)
35+
}
36+
p := addr.Protocols()
37+
if len(p) < 2 {
38+
log.Fatal("need two protocols in host flag (/ip/tcp): ", addr)
39+
}
40+
_, host, err := manet.DialArgs(addr)
41+
if err != nil {
42+
log.Fatal("manet.DialArgs() failed: ", err)
43+
}
44+
45+
if *verbose { // lower log level
46+
logging.SetDebugLogging()
47+
}
48+
49+
// construct url to dial
50+
var u url.URL
51+
u.Scheme = "http"
52+
u.Host = host
53+
u.Path = *endpoint
54+
55+
// show what we got
56+
start := time.Now()
57+
log.Debug("starting at %s, tries: %d, timeout: %s, url: %s", start, *tries, *timeout, u)
58+
59+
for *tries > 0 {
60+
61+
err := checkOK(http.Get(u.String()))
62+
if err == nil {
63+
log.Debugf("ok - endpoint reachable with %d tries remaining, took %s", *tries, time.Since(start))
64+
os.Exit(0)
65+
}
66+
log.Debug("get failed: ", err)
67+
time.Sleep(*timeout)
68+
*tries--
69+
}
70+
71+
log.Error("failed.")
72+
os.Exit(1)
73+
}
74+
75+
func checkOK(resp *http.Response, err error) error {
76+
if err == nil { // request worked
77+
defer resp.Body.Close()
78+
if resp.StatusCode == http.StatusOK {
79+
return nil
80+
}
81+
body, err := ioutil.ReadAll(resp.Body)
82+
if err != nil {
83+
fmt.Fprintf(os.Stderr, "pollEndpoint: ioutil.ReadAll() Error: %s", err)
84+
}
85+
return fmt.Errorf("Response not OK. %d %s %q", resp.StatusCode, resp.Status, string(body))
86+
}
87+
return err
88+
}

0 commit comments

Comments
 (0)