allow sdk users to set graph client timeouts (#4744)
#### Does this PR need a docs update or release note? - [x] ⛔ No #### Type of change - [x] 🌻 Feature #### Test Plan - [x] ⚡ Unit test - [x] 💚 E2E
This commit is contained in:
parent
bbf5350f6e
commit
5a6ddde363
@ -54,7 +54,6 @@ func NewHTTPWrapper(
|
|||||||
}
|
}
|
||||||
hc = &http.Client{
|
hc = &http.Client{
|
||||||
CheckRedirect: redirect,
|
CheckRedirect: redirect,
|
||||||
Timeout: defaultHTTPClientTimeout,
|
|
||||||
Transport: rt,
|
Transport: rt,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@ -24,15 +24,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultMaxRetries = 3
|
|
||||||
defaultDelay = 3 * time.Second
|
|
||||||
locationHeader = "Location"
|
locationHeader = "Location"
|
||||||
rateLimitHeader = "RateLimit-Limit"
|
rateLimitHeader = "RateLimit-Limit"
|
||||||
rateRemainingHeader = "RateLimit-Remaining"
|
rateRemainingHeader = "RateLimit-Remaining"
|
||||||
rateResetHeader = "RateLimit-Reset"
|
rateResetHeader = "RateLimit-Reset"
|
||||||
retryAfterHeader = "Retry-After"
|
retryAfterHeader = "Retry-After"
|
||||||
retryAttemptHeader = "Retry-Attempt"
|
retryAttemptHeader = "Retry-Attempt"
|
||||||
defaultHTTPClientTimeout = 1 * time.Hour
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type QueryParams struct {
|
type QueryParams struct {
|
||||||
@ -161,8 +158,6 @@ func KiotaHTTPClient(
|
|||||||
httpClient = msgraphgocore.GetDefaultClient(&clientOptions, middlewares...)
|
httpClient = msgraphgocore.GetDefaultClient(&clientOptions, middlewares...)
|
||||||
)
|
)
|
||||||
|
|
||||||
httpClient.Timeout = defaultHTTPClientTimeout
|
|
||||||
|
|
||||||
cc.apply(httpClient)
|
cc.apply(httpClient)
|
||||||
|
|
||||||
return httpClient, cc
|
return httpClient, cc
|
||||||
@ -172,8 +167,19 @@ func KiotaHTTPClient(
|
|||||||
// HTTP Client Config
|
// HTTP Client Config
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultDelay = 3 * time.Second
|
||||||
|
defaultHTTPClientTimeout = 1 * time.Hour
|
||||||
|
defaultMaxRetries = 3
|
||||||
|
// FIXME: This should ideally be 0, but if we set to 0, graph
|
||||||
|
// client with automatically set the context timeout to 0 as
|
||||||
|
// well which will make the client unusable.
|
||||||
|
// https://github.com/microsoft/kiota-http-go/pull/71
|
||||||
|
defaultNoTimeout = 48 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
type clientConfig struct {
|
type clientConfig struct {
|
||||||
noTimeout bool
|
timeout time.Duration
|
||||||
// MaxConnectionRetries is the number of connection-level retries that
|
// MaxConnectionRetries is the number of connection-level retries that
|
||||||
// attempt to re-run the request due to a broken or closed connection.
|
// attempt to re-run the request due to a broken or closed connection.
|
||||||
maxConnectionRetries int
|
maxConnectionRetries int
|
||||||
@ -194,6 +200,7 @@ func populateConfig(opts ...Option) *clientConfig {
|
|||||||
maxConnectionRetries: defaultMaxRetries,
|
maxConnectionRetries: defaultMaxRetries,
|
||||||
maxRetries: defaultMaxRetries,
|
maxRetries: defaultMaxRetries,
|
||||||
minDelay: defaultDelay,
|
minDelay: defaultDelay,
|
||||||
|
timeout: defaultHTTPClientTimeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@ -205,22 +212,22 @@ func populateConfig(opts ...Option) *clientConfig {
|
|||||||
|
|
||||||
// apply updates the http.Client with the expected options.
|
// apply updates the http.Client with the expected options.
|
||||||
func (c *clientConfig) apply(hc *http.Client) {
|
func (c *clientConfig) apply(hc *http.Client) {
|
||||||
if c.noTimeout {
|
hc.Timeout = c.timeout
|
||||||
// FIXME: This should ideally be 0, but if we set to 0, graph
|
|
||||||
// client with automatically set the context timeout to 0 as
|
|
||||||
// well which will make the client unusable.
|
|
||||||
// https://github.com/microsoft/kiota-http-go/pull/71
|
|
||||||
hc.Timeout = 48 * time.Hour
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoTimeout sets the httpClient.Timeout to 0 (unlimited).
|
// NoTimeout sets the httpClient.Timeout to 48 hours (eg: unlimited).
|
||||||
// The resulting client isn't suitable for most queries, due to the
|
// The resulting client isn't suitable for most queries, due to the
|
||||||
// capacity for a call to persist forever. This configuration should
|
// capacity for a call to persist forever. This configuration should
|
||||||
// only be used when downloading very large files.
|
// only be used when downloading very large files.
|
||||||
func NoTimeout() Option {
|
func NoTimeout() Option {
|
||||||
return func(c *clientConfig) {
|
return func(c *clientConfig) {
|
||||||
c.noTimeout = true
|
c.timeout = defaultNoTimeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Timeout(timeout time.Duration) Option {
|
||||||
|
return func(c *clientConfig) {
|
||||||
|
c.timeout = timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user