Skip to content

Fix: Multipart request now uses httptimeout, added funcs to expose that to the SDK #286

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ClientConfig struct {
EnableDynamicRateLimiting bool `json:"enable_dynamic_rate_limiting"`

// CustomTimeout // TODO also because I don't know.
CustomTimeout time.Duration
Timeout time.Duration

// TokenRefreshBufferPeriod is the duration of time before the token expires in which it's deemed
// more sensible to replace the token rather then carry on using it.
Expand Down Expand Up @@ -107,12 +107,12 @@ func (c *ClientConfig) Build() (*Client, error) {

httpClient := c.HTTP

if c.CustomTimeout == 0 {
c.CustomTimeout = DefaultTimeout
if c.Timeout == 0 {
httpClient.Timeout = DefaultTimeout
} else {
httpClient.Timeout = c.Timeout
}

httpClient.Timeout = c.CustomTimeout

cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions httpclient/config_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func LoadConfigFromEnv() (*ClientConfig, error) {
MaxRetryAttempts: getEnvAsInt("MAX_RETRY_ATTEMPTS", DefaultMaxRetryAttempts),
MaxConcurrentRequests: getEnvAsInt("MAX_CONCURRENT_REQUESTS", DefaultMaxConcurrentRequests),
EnableDynamicRateLimiting: getEnvAsBool("ENABLE_DYNAMIC_RATE_LIMITING", DefaultEnableDynamicRateLimiting),
CustomTimeout: getEnvAsDuration("CUSTOM_TIMEOUT", DefaultCustomTimeout),
Timeout: getEnvAsDuration("CUSTOM_TIMEOUT", DefaultCustomTimeout),
TokenRefreshBufferPeriod: getEnvAsDuration("TOKEN_REFRESH_BUFFER_PERIOD", DefaultTokenRefreshBufferPeriod),
TotalRetryDuration: getEnvAsDuration("TOTAL_RETRY_DURATION", DefaultTotalRetryDuration),
EnableConcurrencyManagement: getEnvAsBool("ENABLE_CONCURRENCY_MANAGEMENT", DefaultEnableConcurrencyManagement),
Expand Down Expand Up @@ -111,7 +111,7 @@ func (c ClientConfig) validateClientConfig() error {
}
}

if c.CustomTimeout.Seconds() < 0 {
if c.Timeout.Seconds() < 0 {
return errors.New("timeout cannot be less than 0 seconds")
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func (c *ClientConfig) SetDefaultValuesClientConfig() {
setDefaultInt(&c.MaxRetryAttempts, DefaultMaxRetryAttempts, 1)
setDefaultInt(&c.MaxConcurrentRequests, DefaultMaxConcurrentRequests, 1)
setDefaultBool(&c.EnableDynamicRateLimiting, DefaultEnableDynamicRateLimiting)
setDefaultDuration(&c.CustomTimeout, DefaultCustomTimeout)
setDefaultDuration(&c.Timeout, DefaultCustomTimeout)
setDefaultDuration(&c.TokenRefreshBufferPeriod, DefaultTokenRefreshBufferPeriod)
setDefaultDuration(&c.TotalRetryDuration, DefaultTotalRetryDuration)
setDefaultBool(&c.EnableConcurrencyManagement, DefaultEnableConcurrencyManagement)
Expand Down
6 changes: 3 additions & 3 deletions httpclient/multipartrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
var ctx context.Context
var cancel context.CancelFunc

if c.config.CustomTimeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), c.config.CustomTimeout)
c.Sugar.Infow("Using timeout context for multipart request", zap.Duration("custom_timeout_seconds", c.config.CustomTimeout))
if c.http.Timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), c.http.Timeout)
c.Sugar.Infow("Using timeout context for multipart request", zap.Duration("custom_timeout_seconds", c.http.Timeout))
} else {
ctx = context.Background()
cancel = func() {}
Expand Down
11 changes: 4 additions & 7 deletions httpclient/timeouts.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package httpclient

import (
"sync"
"time"
)

var mu sync.Mutex

// Amends the HTTP timeout time
func (c *Client) ModifyHttpTimeout(newTimeout time.Duration) {
mu.Lock()
defer mu.Unlock()
c.http.Timeout = newTimeout
}

// Resets HTTP timeout time back to 10 seconds
func (c *Client) ResetTimeout() {
mu.Lock()
defer mu.Unlock()
c.http.Timeout = DefaultTimeout
}

func (c *Client) HttpTimeout() time.Duration {
return c.http.Timeout
}
Loading