Skip to content

Commit

Permalink
Reuse http client on storage resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano committed Feb 21, 2024
1 parent ec2ba43 commit 632927b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 12 additions & 0 deletions storage/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package storage
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"sync"
Expand Down Expand Up @@ -99,7 +101,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 632927b

Please sign in to comment.