Skip to content

Commit

Permalink
Support overall request timeout in all services
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Oct 3, 2023
1 parent 568b467 commit 4dffe70
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 72 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</div>

| Branch | Status |
|:------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| :----: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| master | [![Build status](https://github.com/jfrog/jfrog-client-go/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/jfrog/jfrog-client-go/actions) [![Static Analysis](https://github.com/jfrog/jfrog-client-go/actions/workflows/analysis.yml/badge.svg?branch=master)](https://github.com/jfrog/jfrog-client-go/actions/workflows/analysis.yml) |
| dev | [![Build status](https://github.com/jfrog/jfrog-client-go/actions/workflows/tests.yml/badge.svg?branch=dev)](https://github.com/jfrog/jfrog-client-go/actions) [![Static Analysis](https://github.com/jfrog/jfrog-client-go/actions/workflows/analysis.yml/badge.svg?branch=dev)](https://github.com/jfrog/jfrog-client-go/actions/workflows/analysis.yml) |

Expand Down Expand Up @@ -251,7 +251,7 @@ content of this repository is deleted.
#### Test Types

| Type | Description | Prerequisites |
|----------------------|--------------------|-------------------------------|
| -------------------- | ------------------ | ----------------------------- |
| `-test.artifactory` | Artifactory tests | Artifactory Pro |
| `-test.distribution` | Distribution tests | Artifactory with Distribution |
| `-test.xray` | Xray tests | Artifactory with Xray |
Expand All @@ -262,7 +262,7 @@ content of this repository is deleted.
#### Connection Details

| Flag | Description |
|---------------------|--------------------------------------------------------------------------------------------------------|
| ------------------- | ------------------------------------------------------------------------------------------------------ |
| `-rt.url` | [Default: http://localhost:8081/artifactory] Artifactory URL. |
| `-ds.url` | [Optional] JFrog Distribution URL. |
| `-xr.url` | [Optional] JFrog Xray URL. |
Expand Down Expand Up @@ -357,8 +357,10 @@ serviceConfig, err := config.NewConfigBuilder().
SetDryRun(false).
// Add [Context](https://golang.org/pkg/context/)
SetContext(ctx).
// Optionally overwrite the default HTTP timeout, which is set to 30 seconds.
SetHttpTimeout(180 * time.Second).
// Optionally overwrite the default dial timeout, which is set to 30 seconds.
SetDialTimeout(180 * time.Second).
// Optionally set the total HTTP request timeout.
SetOverallRequestTimeout(10 * time.Minute).
// Optionally overwrite the default HTTP retries, which is set to 3.
SetHttpRetries(8).
Build()
Expand Down Expand Up @@ -1965,7 +1967,7 @@ vulnerabilitiesReportRequest := services.VulnerabilitiesReportRequestParams{
Severity: []string{
"High",
"Medium"
},
},
CvssScore: services.CvssScore {
MinScore: float64(6.3),
MaxScore: float64(9)
Expand Down Expand Up @@ -2020,6 +2022,7 @@ reportContent, err := xrayManager.ReportContent(reportContentRequest)
// The reportId argument value is returned as part of the xrayManager.GenerateVulnerabilitiesReport API response.
err := xrayManager.DeleteReport(reportId)
```
#### Generate Licences Report
```go
Expand Down Expand Up @@ -2119,7 +2122,7 @@ violationsReportRequest := services.ViolationsReportRequestParams{
Severity: []string{
"High",
"Medium"
},
},
CvssScore: services.CvssScore {
MinScore: float64(6.3),
MaxScore: float64(9)
Expand All @@ -2132,7 +2135,7 @@ violationsReportRequest := services.ViolationsReportRequestParams{
Start: "2020-06-29T12:22:16Z",
End: "2020-06-29T12:22:16Z"
},
SummaryContains: "kernel",
SummaryContains: "kernel",
HasRemediation: &falseValue,
},
LicenseFilters: services.LicensesFilter {
Expand Down
2 changes: 2 additions & 0 deletions access/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func New(config config.Config) (*AccessServicesManager, error) {
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
Build()
Expand Down
3 changes: 2 additions & 1 deletion artifactory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func NewWithProgress(config config.Config, progress ioutils.ProgressMgr) (Artifa
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetContext(config.GetContext()).
SetTimeout(config.GetHttpTimeout()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetClientCertPath(artDetails.GetClientCertPath()).
SetClientCertKeyPath(artDetails.GetClientCertKeyPath()).
AppendPreRequestInterceptor(artDetails.RunPreRequestFunctions).
Expand Down
30 changes: 20 additions & 10 deletions auth/servicedetails.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package auth

import (
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"sync"
"time"

"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"

"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/io/httputils"
Expand Down Expand Up @@ -44,7 +45,8 @@ type ServiceDetails interface {
SetSshPassphrase(sshPassphrase string)
SetSshAuthHeaders(sshAuthHeaders map[string]string)
SetClient(client *jfroghttpclient.JfrogHttpClient)
SetHttpTimeout(httpTimeout time.Duration)
SetDialTimeout(dialTimeout time.Duration)
SetOverallRequestTimeout(overallRequestTimeout time.Duration)

IsSshAuthHeaderSet() bool
IsSshAuthentication() bool
Expand All @@ -71,7 +73,8 @@ type CommonConfigFields struct {
SshAuthHeaders map[string]string `json:"-"`
TokenMutex sync.Mutex
client *jfroghttpclient.JfrogHttpClient
httpTimeout time.Duration
dialTimeout time.Duration
overallRequestTimeout time.Duration
}

func (ccf *CommonConfigFields) GetUrl() string {
Expand Down Expand Up @@ -178,8 +181,12 @@ func (ccf *CommonConfigFields) SetClient(client *jfroghttpclient.JfrogHttpClient
ccf.client = client
}

func (ccf *CommonConfigFields) SetHttpTimeout(httpTimeout time.Duration) {
ccf.httpTimeout = httpTimeout
func (ccf *CommonConfigFields) SetDialTimeout(dialTimeout time.Duration) {
ccf.dialTimeout = dialTimeout
}

func (ccf *CommonConfigFields) SetOverallRequestTimeout(overallRequestTimeout time.Duration) {
ccf.overallRequestTimeout = overallRequestTimeout
}

func (ccf *CommonConfigFields) IsSshAuthHeaderSet() bool {
Expand Down Expand Up @@ -267,9 +274,12 @@ func SshTokenRefreshPreRequestInterceptor(fields *CommonConfigFields, httpClient

func (ccf *CommonConfigFields) CreateHttpClientDetails() httputils.HttpClientDetails {
return httputils.HttpClientDetails{
User: ccf.User,
Password: ccf.Password,
ApiKey: ccf.ApiKey,
AccessToken: ccf.AccessToken,
Headers: utils.CopyMap(ccf.GetSshAuthHeaders())}
User: ccf.User,
Password: ccf.Password,
ApiKey: ccf.ApiKey,
AccessToken: ccf.AccessToken,
Headers: utils.CopyMap(ccf.GetSshAuthHeaders()),
DialTimeout: ccf.dialTimeout,
OverallRequestTimeout: ccf.overallRequestTimeout,
}
}
19 changes: 13 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package config

import (
"context"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
"time"

"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/utils/log"
)

type Config interface {
Expand All @@ -16,7 +17,8 @@ type Config interface {
GetLogger() log.Log
IsInsecureTls() bool
GetContext() context.Context
GetHttpTimeout() time.Duration
GetDialTimeout() time.Duration
GetOverallRequestTimeout() time.Duration
GetHttpRetries() int
GetHttpRetryWaitMilliSecs() int
GetHttpClient() *http.Client
Expand All @@ -30,7 +32,8 @@ type servicesConfig struct {
logger log.Log
insecureTls bool
ctx context.Context
httpTimeout time.Duration
dialTimeout time.Duration
overallRequestTimeout time.Duration
httpRetries int
httpRetryWaitMilliSecs int
httpClient *http.Client
Expand Down Expand Up @@ -64,8 +67,12 @@ func (config *servicesConfig) GetContext() context.Context {
return config.ctx
}

func (config *servicesConfig) GetHttpTimeout() time.Duration {
return config.httpTimeout
func (config *servicesConfig) GetDialTimeout() time.Duration {
return config.dialTimeout
}

func (config *servicesConfig) GetOverallRequestTimeout() time.Duration {
return config.overallRequestTimeout
}

func (config *servicesConfig) GetHttpRetries() int {
Expand Down
29 changes: 22 additions & 7 deletions config/configbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package config

import (
"context"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"net/http"
"time"

"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/log"
)

func NewConfigBuilder() *servicesConfigBuilder {
configBuilder := &servicesConfigBuilder{}
configBuilder.threads = 3
configBuilder.httpTimeout = httpclient.DefaultHttpTimeout
configBuilder.dialTimeout = httpclient.DefaultDialTimeout
configBuilder.httpRetries = 3
configBuilder.httpRetryWaitMilliSecs = 0
return configBuilder
Expand All @@ -24,7 +26,8 @@ type servicesConfigBuilder struct {
isDryRun bool
insecureTls bool
ctx context.Context
httpTimeout time.Duration
dialTimeout time.Duration
overallRequestTimeout time.Duration
httpRetries int
httpRetryWaitMilliSecs int
httpClient *http.Client
Expand Down Expand Up @@ -60,8 +63,19 @@ func (builder *servicesConfigBuilder) SetContext(ctx context.Context) *servicesC
return builder
}

func (builder *servicesConfigBuilder) SetHttpTimeout(httpTimeout time.Duration) *servicesConfigBuilder {
builder.httpTimeout = httpTimeout
// Deprecated
func (builder *servicesConfigBuilder) SetHttpTimeout(dialTimeout time.Duration) *servicesConfigBuilder {
log.Warn("SetHttpTimeout is deprecated. Use SetDialTimeout instead.")
return builder.SetDialTimeout(dialTimeout)
}

func (builder *servicesConfigBuilder) SetDialTimeout(dialTimeout time.Duration) *servicesConfigBuilder {
builder.dialTimeout = dialTimeout
return builder
}

func (builder *servicesConfigBuilder) SetOverallRequestTimeout(overallRequestTimeout time.Duration) *servicesConfigBuilder {
builder.overallRequestTimeout = overallRequestTimeout
return builder
}

Expand All @@ -88,7 +102,8 @@ func (builder *servicesConfigBuilder) Build() (Config, error) {
c.dryRun = builder.isDryRun
c.insecureTls = builder.insecureTls
c.ctx = builder.ctx
c.httpTimeout = builder.httpTimeout
c.dialTimeout = builder.dialTimeout
c.overallRequestTimeout = builder.overallRequestTimeout
c.httpRetries = builder.httpRetries
c.httpRetryWaitMilliSecs = builder.httpRetryWaitMilliSecs
c.httpClient = builder.httpClient
Expand Down
3 changes: 2 additions & 1 deletion distribution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func New(config config.Config) (*DistributionServicesManager, error) {
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetContext(config.GetContext()).
SetTimeout(config.GetHttpTimeout()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetClientCertPath(details.GetClientCertPath()).
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
Expand Down
36 changes: 21 additions & 15 deletions http/httpclient/clientBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import (
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

var DefaultHttpTimeout = 30 * time.Second
var DefaultDialTimeout = 30 * time.Second

func ClientBuilder() *httpClientBuilder {
builder := &httpClientBuilder{}
builder.SetTimeout(DefaultHttpTimeout)
builder.SetDialTimeout(DefaultDialTimeout)
return builder
}

type httpClientBuilder struct {
certificatesDirPath string
clientCertPath string
clientCertKeyPath string
insecureTls bool
ctx context.Context
timeout time.Duration
retries int
retryWaitMilliSecs int
httpClient *http.Client
certificatesDirPath string
clientCertPath string
clientCertKeyPath string
insecureTls bool
ctx context.Context
dialTimeout time.Duration
overallRequestTimeout time.Duration
retries int
retryWaitMilliSecs int
httpClient *http.Client
}

func (builder *httpClientBuilder) SetCertificatesPath(certificatesPath string) *httpClientBuilder {
Expand Down Expand Up @@ -61,8 +62,13 @@ func (builder *httpClientBuilder) SetContext(ctx context.Context) *httpClientBui
return builder
}

func (builder *httpClientBuilder) SetTimeout(timeout time.Duration) *httpClientBuilder {
builder.timeout = timeout
func (builder *httpClientBuilder) SetDialTimeout(dialTimeout time.Duration) *httpClientBuilder {
builder.dialTimeout = dialTimeout
return builder
}

func (builder *httpClientBuilder) SetOverallRequestTimeout(overallRequestTimeout time.Duration) *httpClientBuilder {
builder.overallRequestTimeout = overallRequestTimeout
return builder
}

Expand Down Expand Up @@ -107,14 +113,14 @@ func (builder *httpClientBuilder) Build() (*HttpClient, error) {
}
}
err = builder.AddClientCertToTransport(transport)
return &HttpClient{client: &http.Client{Transport: transport}, ctx: builder.ctx, retries: builder.retries, retryWaitMilliSecs: builder.retryWaitMilliSecs}, err
return &HttpClient{client: &http.Client{Transport: transport, Timeout: builder.overallRequestTimeout}, ctx: builder.ctx, retries: builder.retries, retryWaitMilliSecs: builder.retryWaitMilliSecs}, err
}

func (builder *httpClientBuilder) createDefaultHttpTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: builder.timeout,
Timeout: builder.dialTimeout,
KeepAlive: 20 * time.Second,
DualStack: true,
}).DialContext,
Expand Down
Loading

0 comments on commit 4dffe70

Please sign in to comment.