Skip to content

Commit 4e4eaa3

Browse files
authored
Merge pull request #3092 from cozy/network
Improve http client options
2 parents 8b22226 + 1e8b293 commit 4e4eaa3

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

model/app/fetcher_http.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/hex"
99
"hash"
1010
"io"
11+
"io/ioutil"
1112
"net/http"
1213
"net/url"
1314
"os"
@@ -22,7 +23,7 @@ import (
2223
)
2324

2425
var httpClient = http.Client{
25-
Timeout: 2 * 60 * time.Second,
26+
Timeout: 60 * time.Second,
2627
}
2728

2829
type httpFetcher struct {
@@ -49,6 +50,8 @@ func (f *httpFetcher) FetchManifest(src *url.URL) (r io.ReadCloser, err error) {
4950
}
5051
defer func() {
5152
if err != nil {
53+
// Flush the body, so that the connecion can be reused by keep-alive
54+
_, _ = io.Copy(ioutil.Discard, resp.Body)
5255
resp.Body.Close()
5356
}
5457
}()
@@ -125,6 +128,8 @@ func fetchHTTP(src *url.URL, shasum []byte, fs appfs.Copier, man Manifest, prefi
125128
resp, err := httpClient.Do(req)
126129
elapsed := time.Since(start)
127130
if err != nil {
131+
log := logger.WithNamespace("fetcher")
132+
log.Printf("cannot fetch %s: %s", src.String(), err)
128133
return err
129134
}
130135
defer resp.Body.Close()

model/bitwarden/icon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const (
2323

2424
var iconClient = &http.Client{
2525
Timeout: 10 * time.Second,
26+
// As we are connecting to a new host each time, it is better to disable
27+
// keep-alive
28+
Transport: &http.Transport{
29+
DisableKeepAlives: true,
30+
},
2631
}
2732

2833
// Icon is a simple struct with a content-type and the content of an icon.

pkg/config/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,9 @@ func UseViper(v *viper.Viper) error {
490490
couchURL.Path = "/"
491491
}
492492
couchClient, _, err := tlsclient.NewHTTPClient(tlsclient.HTTPEndpoint{
493-
Timeout: 10 * time.Second,
494-
RootCAFile: v.GetString("couchdb.root_ca"),
493+
Timeout: 10 * time.Second,
494+
MaxIdleConnsPerHost: 20,
495+
RootCAFile: v.GetString("couchdb.root_ca"),
495496
ClientCertificateFiles: tlsclient.ClientCertificateFilePair{
496497
CertificateFile: v.GetString("couchdb.client_cert"),
497498
KeyFile: v.GetString("couchdb.client_key"),

pkg/couchdb/couchdb.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"net/http"
910
"net/url"
@@ -344,6 +345,8 @@ func makeRequest(db Database, doctype, method, path string, reqbody interface{},
344345
return err
345346
}
346347
if resbody == nil {
348+
// Flush the body, so that the connecion can be reused by keep-alive
349+
_, _ = io.Copy(ioutil.Discard, resp.Body)
347350
return nil
348351
}
349352

pkg/manager/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"io"
99
"net/http"
10+
"time"
1011

1112
"golang.org/x/oauth2"
1213
)
@@ -33,9 +34,11 @@ type APIClient struct {
3334
// NewAPIClient builds a new client for the manager API
3435
func NewAPIClient(baseURL, token string) *APIClient {
3536
tokenSource := &tokenSource{token: token}
37+
client := oauth2.NewClient(context.Background(), tokenSource)
38+
client.Timeout = 15 * time.Second
3639
return &APIClient{
3740
baseURL: baseURL,
38-
client: oauth2.NewClient(context.Background(), tokenSource),
41+
client: client,
3942
}
4043
}
4144

pkg/registry/registry.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io"
8+
"io/ioutil"
79
"net"
810
"net/http"
911
"net/url"
@@ -502,6 +504,8 @@ func fetch(client *http.Client, registry, ref *url.URL, cache CacheControl) (res
502504
elapsed := time.Since(start)
503505
defer func() {
504506
if !ok {
507+
// Flush the body, so that the connecion can be reused by keep-alive
508+
_, _ = io.Copy(ioutil.Discard, resp.Body)
505509
resp.Body.Close()
506510
}
507511
}()

web/oidc/oidc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"io"
910
"io/ioutil"
1011
"net/http"
1112
"net/url"
@@ -357,6 +358,8 @@ func getToken(conf *Config, code string) (string, error) {
357358
}
358359
defer res.Body.Close()
359360
if res.StatusCode != 200 {
361+
// Flush the body, so that the connecion can be reused by keep-alive
362+
_, _ = io.Copy(ioutil.Discard, res.Body)
360363
logger.WithNamespace("oidc").
361364
Infof("Invalid status code %d for %s", res.StatusCode, conf.TokenURL)
362365
return "", fmt.Errorf("OIDC service responded with %d", res.StatusCode)
@@ -426,6 +429,8 @@ func getUserInfo(conf *Config, token string) (map[string]interface{}, error) {
426429
}
427430
defer res.Body.Close()
428431
if res.StatusCode != 200 {
432+
// Flush the body, so that the connecion can be reused by keep-alive
433+
_, _ = io.Copy(ioutil.Discard, res.Body)
429434
logger.WithNamespace("oidc").
430435
Infof("Invalid status code %d for %s", res.StatusCode, conf.UserInfoURL)
431436
return nil, fmt.Errorf("OIDC service responded with %d", res.StatusCode)

0 commit comments

Comments
 (0)