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

Commit ca19d4b

Browse files
committed
retries flag
1 parent b834204 commit ca19d4b

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

main.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,20 @@ 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))
8787
}
8888

8989
h, err := libp2p.New(context.Background(), opts...)
9090
if err != nil {
91-
panic(err)
91+
return nil, nil, err
9292
}
9393

9494
d := dht.NewDHT(context.Background(), h, ds)
9595
if err != nil {
96-
panic(err)
96+
return nil, nil, err
9797
}
9898

9999
d.Validator = record.NamespacedValidator{
@@ -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,14 +158,23 @@ 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+
var err error
164+
for r := 0; r < *retries; r++ {
165+
var h host.Host
166+
var d *dht.IpfsDHT
167+
h, d, err = makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/0", *relay, *retries)
168+
if err == nil {
169+
hosts = append(hosts, h)
170+
dhts = append(dhts, d)
171+
break
172+
}
173+
fmt.Fprintf(os.Stderr, "Error starting node %d: %s\n", i, err.Error())
174+
}
155175
if err != nil {
156176
panic(err)
157177
}
158-
hosts = append(hosts, h)
159-
dhts = append(dhts, d)
160178
}
161179

162180
for range time.Tick(time.Second * 5) {
@@ -180,12 +198,12 @@ func printStatusLine(ndht int, start time.Time, hosts []host.Host, dhts []*dht.I
180198
fmt.Fprintf(os.Stderr, "[NumDhts: %d, Uptime: %s, Memory Usage: %s, TotalPeers: %d/%d]\n", ndht, uptime, human.Bytes(mstat.Alloc), totalpeers, len(uniqprs))
181199
}
182200

183-
func runSingleDHTWithUI(path string, relay bool) {
201+
func runSingleDHTWithUI(path string, relay bool, retries int) {
184202
ds, err := levelds.NewDatastore(path, nil)
185203
if err != nil {
186204
panic(err)
187205
}
188-
h, _, err := makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/19264", relay)
206+
h, _, err := makeAndStartNode(ds, "/ip4/0.0.0.0/tcp/19264", relay, retries)
189207
if err != nil {
190208
panic(err)
191209
}

0 commit comments

Comments
 (0)