Skip to content

Commit 2bfd780

Browse files
committed
Retry whois on error by default
1 parent 1c2058e commit 2bfd780

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

whois/whois.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,10 @@ type QueryOpts struct {
1616
Port int
1717
Query string
1818
Timeout time.Duration
19+
Attempts int
1920
}
2021

21-
func Query(opts *QueryOpts) ([]byte, error) {
22-
if opts.Port == 0 {
23-
opts.Port = 43
24-
}
25-
26-
if opts.Timeout == 0 {
27-
opts.Timeout = time.Second * 10
28-
}
29-
22+
func queryHandler(opts *QueryOpts) ([]byte, error) {
3023
// Open connection
3124
con, err := net.Dial("tcp", net.JoinHostPort(opts.Hostname, fmt.Sprint(opts.Port)))
3225
if err != nil {
@@ -55,3 +48,33 @@ func Query(opts *QueryOpts) ([]byte, error) {
5548

5649
return resp, nil
5750
}
51+
52+
func queryLoop(opts *QueryOpts, attempt int) ([]byte, error) {
53+
whois, err := queryHandler(opts)
54+
55+
// Retry on failure within attempt threshold
56+
if err != nil {
57+
if attempt < opts.Attempts {
58+
time.Sleep(time.Second * time.Duration(attempt))
59+
return queryLoop(opts, (attempt + 1))
60+
}
61+
}
62+
63+
return whois, err
64+
}
65+
66+
func Query(opts *QueryOpts) ([]byte, error) {
67+
if opts.Port == 0 {
68+
opts.Port = 43
69+
}
70+
71+
if opts.Timeout == 0 {
72+
opts.Timeout = time.Second * 10
73+
}
74+
75+
if opts.Attempts == 0 {
76+
opts.Attempts = 5
77+
}
78+
79+
return queryLoop(opts, 1)
80+
}

0 commit comments

Comments
 (0)