Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse http client on storage resolver #1147

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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