Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 51f06c5

Browse files
committed
retries flag
1 parent b834204 commit 51f06c5

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

main.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func boostrapper() pstore.PeerInfo {
8080
}
8181
}
8282

83-
func makeAndStartNode(ds ds.Batching, addr string, relay bool) (host.Host, *dht.IpfsDHT, error) {
83+
func makeAndStartNode(ds ds.Batching, addr string, relay bool, retries int) (host.Host, *dht.IpfsDHT, error) {
8484
opts := []libp2p.Option{libp2p.ListenAddrStrings(addr)}
8585
if relay {
8686
opts = append(opts, libp2p.EnableRelay(circuit.OptHop))
@@ -102,7 +102,15 @@ func makeAndStartNode(ds ds.Batching, addr string, relay bool) (host.Host, *dht.
102102
}
103103

104104
go func() {
105-
err = h.Connect(context.Background(), boostrapper())
105+
var err error
106+
for r := 0; r < retries; r++ {
107+
err = h.Connect(context.Background(), boostrapper())
108+
if err == nil {
109+
break
110+
}
111+
time.Sleep(6000 * time.Millisecond)
112+
fmt.Fprintf(os.Stderr, "Error starting node: %s\n", err.Error())
113+
}
106114
if err != nil {
107115
panic(err)
108116
}
@@ -115,6 +123,7 @@ func makeAndStartNode(ds ds.Batching, addr string, relay bool) (host.Host, *dht.
115123

116124
func main() {
117125
many := flag.Int("many", -1, "Instead of running one dht, run many!")
126+
retries := flag.Int("retries", 1, "Number of times to retry starting nodes")
118127
dbpath := flag.String("db", "dht-data", "Database folder")
119128
inmem := flag.Bool("mem", false, "Use an in-memory database. This overrides the -db option")
120129
pprofport := flag.Int("pprof-port", -1, "Specify a port to run pprof http server on")
@@ -137,7 +146,7 @@ func main() {
137146
*dbpath = ""
138147
}
139148
if *many == -1 {
140-
runSingleDHTWithUI(*dbpath, *relay)
149+
runSingleDHTWithUI(*dbpath, *relay, *retries)
141150
}
142151

143152
ds, err := levelds.NewDatastore(*dbpath, nil)
@@ -149,9 +158,9 @@ func main() {
149158
var hosts []host.Host
150159
var dhts []*dht.IpfsDHT
151160
uniqpeers := make(map[peer.ID]struct{})
152-
fmt.Fprintf(os.Stderr, "Running %d DHT Instances...", *many)
161+
fmt.Fprintf(os.Stderr, "Running %d DHT Instances...\n", *many)
153162
for i := 0; i < *many; i++ {
154-
h, d, err := makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/0", *relay)
163+
h, d, err := makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/0", *relay, *retries)
155164
if err != nil {
156165
panic(err)
157166
}
@@ -180,12 +189,12 @@ func printStatusLine(ndht int, start time.Time, hosts []host.Host, dhts []*dht.I
180189
fmt.Fprintf(os.Stderr, "[NumDhts: %d, Uptime: %s, Memory Usage: %s, TotalPeers: %d/%d]\n", ndht, uptime, human.Bytes(mstat.Alloc), totalpeers, len(uniqprs))
181190
}
182191

183-
func runSingleDHTWithUI(path string, relay bool) {
192+
func runSingleDHTWithUI(path string, relay bool, retries int) {
184193
ds, err := levelds.NewDatastore(path, nil)
185194
if err != nil {
186195
panic(err)
187196
}
188-
h, _, err := makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/19264", relay)
197+
h, _, err := makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/19264", relay, retries)
189198
if err != nil {
190199
panic(err)
191200
}

0 commit comments

Comments
 (0)