Skip to content

Commit 9a5c13c

Browse files
committed
Retry on connection reset errors
1 parent 1f5e891 commit 9a5c13c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

crawler/crawler.go

+29
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ package crawler
44
import (
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io/ioutil"
10+
"math"
11+
"math/rand"
912
"net/http"
1013
"net/url"
1114
"path/filepath"
1215
"runtime"
1316
"strings"
1417
"sync"
18+
"syscall"
19+
"time"
1520

1621
"github.com/hashicorp/go-retryablehttp"
22+
"github.com/tschaub/retry"
1723
"github.com/tschaub/workgroup"
1824
)
1925

@@ -71,6 +77,29 @@ func loadFile(resourcePath string, value any) error {
7177
}
7278

7379
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 {
74103
resp, err := httpClient.Get(resourceUrl)
75104
if err != nil {
76105
return err

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
1111
github.com/schollz/progressbar/v3 v3.8.6
1212
github.com/stretchr/testify v1.7.1
13+
github.com/tschaub/retry v1.0.0
1314
github.com/tschaub/workgroup v0.2.0
1415
github.com/urfave/cli/v2 v2.4.0
1516
go.uber.org/zap v1.21.0

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
4545
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4646
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
4747
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=
4849
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4950
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
5051
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5152
github.com/tschaub/jsonschema/v5 v5.1.0-beta.1 h1:yuHeTkhFAApxLRdWXFEeArU0iMIBJm27f1/9t41Gf5A=
5253
github.com/tschaub/jsonschema/v5 v5.1.0-beta.1/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
5354
github.com/tschaub/limited v0.2.0 h1:XBjhp4JNhRruzRlU4/RCLGxxSmRPO+rJelpB5EVZYJU=
5455
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=
5558
github.com/tschaub/workgroup v0.2.0 h1:llc29/O3boFokyJkPWL3SFifcnQeD0kIcPtrmtsM0Nw=
5659
github.com/tschaub/workgroup v0.2.0/go.mod h1:UEtjKJmK1+hsGa6zC1sSHC6iZh8GmzesqybVruhdGX8=
5760
github.com/urfave/cli/v2 v2.4.0 h1:m2pxjjDFgDxSPtO8WSdbndj17Wu2y8vOT86wE/tjr+I=

0 commit comments

Comments
 (0)