Skip to content

Commit dc63c46

Browse files
author
Siva M
authored
Add default timeout for HTTP client (#192)
1 parent ad6ba06 commit dc63c46

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

pkg/license/license.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"net/http"
87
"time"
98

109
"github.com/pkg/errors"
@@ -63,7 +62,7 @@ func getLicenseFromAPI(url string, licenseID string) (*LicenseData, error) {
6362
instanceData := report.GetInstanceData(store.GetStore())
6463
report.InjectInstanceDataHeaders(req, instanceData)
6564

66-
resp, err := http.DefaultClient.Do(req)
65+
resp, err := util.HttpClient().Do(req)
6766
if err != nil {
6867
return nil, errors.Wrap(err, "failed to execute get request")
6968
}
@@ -125,7 +124,7 @@ func GetLatestLicenseFields(license *kotsv1beta1.License, endpoint string) (type
125124
instanceData := report.GetInstanceData(store.GetStore())
126125
report.InjectInstanceDataHeaders(req, instanceData)
127126

128-
resp, err := http.DefaultClient.Do(req)
127+
resp, err := util.HttpClient().Do(req)
129128
if err != nil {
130129
return nil, errors.Wrap(err, "failed to execute get request")
131130
}
@@ -164,7 +163,7 @@ func GetLatestLicenseField(license *kotsv1beta1.License, endpoint string, fieldN
164163
instanceData := report.GetInstanceData(store.GetStore())
165164
report.InjectInstanceDataHeaders(req, instanceData)
166165

167-
resp, err := http.DefaultClient.Do(req)
166+
resp, err := util.HttpClient().Do(req)
168167
if err != nil {
169168
return nil, errors.Wrap(err, "failed to execute get request")
170169
}

pkg/report/custom_app_metrics.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"net/http"
109
"net/url"
1110
"time"
1211

@@ -91,7 +90,7 @@ func SendOnlineCustomAppMetrics(sdkStore store.Store, data map[string]interface{
9190
instanceData := GetInstanceData(sdkStore)
9291
InjectInstanceDataHeaders(req, instanceData)
9392

94-
resp, err := http.DefaultClient.Do(req)
93+
resp, err := util.HttpClient().Do(req)
9594
if err != nil {
9695
return errors.Wrap(err, "failed to execute get request")
9796
}

pkg/report/instance.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9-
"net/http"
109
"sync"
1110
"time"
1211

@@ -111,7 +110,7 @@ func SendOnlineInstanceData(license *v1beta1.License, instanceData *types.Instan
111110

112111
InjectInstanceDataHeaders(postReq, instanceData)
113112

114-
resp, err := http.DefaultClient.Do(postReq)
113+
resp, err := util.HttpClient().Do(postReq)
115114
if err != nil {
116115
return errors.Wrap(err, "failed to post request")
117116
}

pkg/upstream/replicated.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"net/http"
109
"net/url"
1110

1211
"github.com/pkg/errors"
@@ -69,7 +68,7 @@ func GetUpdates(sdkStore store.Store, license *kotsv1beta1.License, currentCurso
6968

7069
report.InjectInstanceDataHeaders(req, instanceData)
7170

72-
resp, err := http.DefaultClient.Do(req)
71+
resp, err := util.HttpClient().Do(req)
7372
if err != nil {
7473
return nil, errors.Wrap(err, "failed to execute get request")
7574
}

pkg/util/http.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package util
2+
3+
import (
4+
"net/http"
5+
"time"
6+
)
7+
8+
func HttpClient() *http.Client {
9+
return &http.Client{
10+
Timeout: 10 * time.Second,
11+
}
12+
}

0 commit comments

Comments
 (0)