Skip to content

Commit

Permalink
Reuse http client on storage resolver (#1147)
Browse files Browse the repository at this point in the history
Reuses http client on storage resolves and sets a generous connect timeout
to circuit-break on network issues.
  • Loading branch information
jsoriano authored Apr 5, 2024
1 parent f05075c commit 618c875
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Add support for multi-platform container images. [#1162](https://github.com/elastic/package-registry/pull/1162)
* Use Wolfi as base for container images. [#1169](https://github.com/elastic/package-registry/pull/1169)
* Reuse HTTP client when proxifying resolver requests. [#1147](https://github.com/elastic/package-registry/pull/1147)

### Deprecated

Expand Down
12 changes: 12 additions & 0 deletions storage/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"sync"
Expand Down Expand Up @@ -100,7 +102,17 @@ func (i *Indexer) setupResolver() error {
return err
}

httpClient := http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
// Connect timeout.
Timeout: 20 * time.Second,
}).DialContext,
},
}

i.resolver = storageResolver{
client: &httpClient,
artifactsPackagesURL: *baseURL.ResolveReference(&url.URL{Path: artifactsPackagesStoragePath + "/"}),
artifactsStaticURL: *baseURL.ResolveReference(&url.URL{Path: artifactsStaticStoragePath + "/"}),
}
Expand Down
5 changes: 2 additions & 3 deletions storage/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

type storageResolver struct {
client *http.Client
artifactsPackagesURL url.URL
artifactsStaticURL url.URL
}
Expand All @@ -26,15 +27,13 @@ var acceptedHeaders = map[string]string{
}

func (resolver storageResolver) pipeRequestProxy(w http.ResponseWriter, r *http.Request, remoteURL string) {
client := &http.Client{}

forwardRequest, err := http.NewRequestWithContext(r.Context(), r.Method, remoteURL, nil)
if err != nil {
http.Error(w, "failed to create request for the package-storage", http.StatusInternalServerError)
return
}

resp, err := client.Do(forwardRequest)
resp, err := resolver.client.Do(forwardRequest)
if err != nil {
http.Error(w, "error from package-storage server", http.StatusInternalServerError)
return
Expand Down

0 comments on commit 618c875

Please sign in to comment.