File tree 3 files changed +33
-0
lines changed
3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -4,16 +4,22 @@ package crawler
4
4
import (
5
5
"context"
6
6
"encoding/json"
7
+ "errors"
7
8
"fmt"
8
9
"io/ioutil"
10
+ "math"
11
+ "math/rand"
9
12
"net/http"
10
13
"net/url"
11
14
"path/filepath"
12
15
"runtime"
13
16
"strings"
14
17
"sync"
18
+ "syscall"
19
+ "time"
15
20
16
21
"github.com/hashicorp/go-retryablehttp"
22
+ "github.com/tschaub/retry"
17
23
"github.com/tschaub/workgroup"
18
24
)
19
25
@@ -71,6 +77,29 @@ func loadFile(resourcePath string, value any) error {
71
77
}
72
78
73
79
func loadUrl (resourceUrl string , value any ) error {
80
+ ctx , cancel := context .WithTimeout (context .Background (), time .Minute * 30 )
81
+ defer cancel ()
82
+
83
+ retries := 5
84
+
85
+ return retry .Limit (ctx , retries , func (ctx context.Context , attempt int ) error {
86
+ err := tryLoadUrl (resourceUrl , value )
87
+ if err == nil {
88
+ return nil
89
+ }
90
+
91
+ // these come when parsing the response body
92
+ if ! errors .Is (err , syscall .ECONNRESET ) {
93
+ return retry .Stop (err )
94
+ }
95
+
96
+ jitter := time .Duration (rand .Float64 ()) * time .Second
97
+ time .Sleep (time .Second * time .Duration (math .Pow (2 , float64 (attempt ))) + jitter )
98
+ return err
99
+ })
100
+ }
101
+
102
+ func tryLoadUrl (resourceUrl string , value any ) error {
74
103
resp , err := httpClient .Get (resourceUrl )
75
104
if err != nil {
76
105
return err
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ require (
10
10
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
11
11
github.com/schollz/progressbar/v3 v3.8.6
12
12
github.com/stretchr/testify v1.7.1
13
+ github.com/tschaub/retry v1.0.0
13
14
github.com/tschaub/workgroup v0.2.0
14
15
github.com/urfave/cli/v2 v2.4.0
15
16
go.uber.org/zap v1.21.0
Original file line number Diff line number Diff line change @@ -45,13 +45,16 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
45
45
github.com/stretchr/testify v1.2.2 /go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs =
46
46
github.com/stretchr/testify v1.3.0 /go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI =
47
47
github.com/stretchr/testify v1.4.0 /go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4 =
48
+ github.com/stretchr/testify v1.5.1 /go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA =
48
49
github.com/stretchr/testify v1.7.0 /go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg =
49
50
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY =
50
51
github.com/stretchr/testify v1.7.1 /go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg =
51
52
github.com/tschaub/jsonschema/v5 v5.1.0-beta.1 h1:yuHeTkhFAApxLRdWXFEeArU0iMIBJm27f1/9t41Gf5A =
52
53
github.com/tschaub/jsonschema/v5 v5.1.0-beta.1 /go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0 =
53
54
github.com/tschaub/limited v0.2.0 h1:XBjhp4JNhRruzRlU4/RCLGxxSmRPO+rJelpB5EVZYJU =
54
55
github.com/tschaub/limited v0.2.0 /go.mod h1:MpOH3bHLpfI+6MO7P1DulWY4aXXqKdkUn8v4eMm4xOI =
56
+ github.com/tschaub/retry v1.0.0 h1:zY9jeVNPxv2UovkFikgYOpvT5q52/3KQuJrY70MK+ws =
57
+ github.com/tschaub/retry v1.0.0 /go.mod h1:yBQOt5++ZYLg8LqzLICIFMmiG7WM45CYCnemriP95T8 =
55
58
github.com/tschaub/workgroup v0.2.0 h1:llc29/O3boFokyJkPWL3SFifcnQeD0kIcPtrmtsM0Nw =
56
59
github.com/tschaub/workgroup v0.2.0 /go.mod h1:UEtjKJmK1+hsGa6zC1sSHC6iZh8GmzesqybVruhdGX8 =
57
60
github.com/urfave/cli/v2 v2.4.0 h1:m2pxjjDFgDxSPtO8WSdbndj17Wu2y8vOT86wE/tjr+I =
You can’t perform that action at this time.
0 commit comments