Parse string time (eg 30s) instead of assuming seconds for —-dht-timeout
License: MIT Signed-off-by: Dirk McCormick <dirkmdev@gmail.com>
Dirk McCormick committed
Mar 22, 2018 at 12:52 UTC
494f242b76825b59cb4761400c9bc9e52c06b3bd
2 files changed
+25
-6
core/commands/ipns.go
+12
-3
@@ -60,7 +60,7 @@ Resolve the value of a dnslink:
60
cmdkit.BoolOption("recursive", "r", "Resolve until the result is not an IPNS name."),
61
cmdkit.BoolOption("nocache", "n", "Do not use cached entries."),
62
cmdkit.UintOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
63
- cmdkit.UintOption("dht-timeout", "dhtt", "Timeout in seconds for DHT resolution. Pass 0 for no timeout."),
63
+ cmdkit.StringOption("dht-timeout", "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
64
},
65
Run: func(req cmds.Request, res cmds.Response) {
66
n, err := req.InvocContext().GetNode()
@@ -111,7 +111,7 @@ Resolve the value of a dnslink:
111
112
recursive, _, _ := req.Option("recursive").Bool()
113
rc, rcok, _ := req.Option("dht-record-count").Int()
114
- dhtt, dhttok, _ := req.Option("dht-timeout").Int()
114
+ dhtt, dhttok, _ := req.Option("dht-timeout").String()
115
var ropts []nsopts.ResolveOpt
116
if !recursive {
117
ropts = append(ropts, nsopts.Depth(1))
@@ -120,7 +120,16 @@ Resolve the value of a dnslink:
120
ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
121
}
122
if dhttok {
123
- ropts = append(ropts, nsopts.DhtTimeout(time.Duration(dhtt)*time.Second))
123
+ d, err := time.ParseDuration(dhtt)
124
+ if err != nil {
125
+ res.SetError(err, cmdkit.ErrNormal)
126
+ return
127
+ }
128
+ if d < 0 {
129
+ res.SetError(errors.New("DHT timeout value must be >= 0"), cmdkit.ErrNormal)
130
+ return
131
+ }
132
+ ropts = append(ropts, nsopts.DhtTimeout(d))
133
}
134
135
if !strings.HasPrefix(name, "/ipns/") {
core/commands/resolve.go
+13
-3
@@ -1,6 +1,7 @@
1
package commands
2
3
import (
4
+ "errors"
5
"io"
6
"strings"
7
"time"
@@ -65,7 +66,7 @@ Resolve the value of an IPFS DAG path:
66
Options: []cmdkit.Option{
67
cmdkit.BoolOption("recursive", "r", "Resolve until the result is an IPFS name."),
68
cmdkit.UintOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
68
- cmdkit.UintOption("dht-timeout", "dhtt", "Timeout in seconds for DHT resolution. Pass 0 for no timeout."),
69
+ cmdkit.StringOption("dht-timeout", "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
70
},
71
Run: func(req cmds.Request, res cmds.Response) {
72
@@ -89,13 +90,22 @@ Resolve the value of an IPFS DAG path:
90
// the case when ipns is resolved step by step
91
if strings.HasPrefix(name, "/ipns/") && !recursive {
92
rc, rcok, _ := req.Option("dht-record-count").Int()
92
- dhtt, dhttok, _ := req.Option("dht-timeout").Int()
93
+ dhtt, dhttok, _ := req.Option("dht-timeout").String()
94
ropts := []nsopts.ResolveOpt{nsopts.Depth(1)}
95
if rcok {
96
ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
97
}
98
if dhttok {
98
- ropts = append(ropts, nsopts.DhtTimeout(time.Duration(dhtt)*time.Second))
99
+ d, err := time.ParseDuration(dhtt)
100
+ if err != nil {
101
+ res.SetError(err, cmdkit.ErrNormal)
102
+ return
103
+ }
104
+ if d < 0 {
105
+ res.SetError(errors.New("DHT timeout value must be >= 0"), cmdkit.ErrNormal)
106
+ return
107
+ }
108
+ ropts = append(ropts, nsopts.DhtTimeout(d))
109
}
110
p, err := n.Namesys.Resolve(req.Context(), name, ropts...)
111
// ErrResolveRecursion is fine