feat: support customize registry http client timeout (#17768)

Support override the registry http client timeout from env `REGISTRY_HTTP_CLIENT_TIMEOUT` (in minutes), the timeout is 30 minutes by default.

Signed-off-by: chlins <chenyuzh@vmware.com>
This commit is contained in:
Chlins Zhang 2022-11-09 17:20:47 +08:00 committed by GitHub
parent e2e8f00f35
commit 77df4d92fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
@ -38,6 +39,7 @@ import (
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/config"
"github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/pkg/registry/auth"
"github.com/goharbor/harbor/src/pkg/registry/interceptor"
"github.com/goharbor/harbor/src/pkg/registry/interceptor/readonly"
@ -64,8 +66,28 @@ var (
// const definition
const (
UserAgent = "harbor-registry-client"
// DefaultHTTPClientTimeout is the default timeout for registry http client.
DefaultHTTPClientTimeout = 30 * time.Minute
)
var (
// registryHTTPClientTimeout is the timeout for registry http client.
registryHTTPClientTimeout time.Duration
)
func init() {
registryHTTPClientTimeout = DefaultHTTPClientTimeout
// override it if read from environment variable, in minutes
timeout, err := strconv.ParseInt(os.Getenv("REGISTRY_HTTP_CLIENT_TIMEOUT"), 10, 64)
if err != nil {
log.Errorf("Failed to parse REGISTRY_HTTP_CLIENT_TIMEOUT: %v, use default value: %v", err, DefaultHTTPClientTimeout)
} else {
if timeout > 0 {
registryHTTPClientTimeout = time.Duration(timeout) * time.Minute
}
}
}
// Client defines the methods that a registry client should implements
type Client interface {
// Ping the base API endpoint "/v2/"
@ -120,7 +142,7 @@ func NewClientWithAuthorizer(url string, authorizer lib.Authorizer, insecure boo
interceptors: interceptors,
client: &http.Client{
Transport: commonhttp.GetHTTPTransport(commonhttp.WithInsecure(insecure)),
Timeout: 30 * time.Minute,
Timeout: registryHTTPClientTimeout,
},
}
}