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:
Keepers 2023-11-28 11:16:04 -07:00 committed by GitHub
parent bbf5350f6e
commit 5a6ddde363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 22 deletions

View File

@ -54,7 +54,6 @@ func NewHTTPWrapper(
} }
hc = &http.Client{ hc = &http.Client{
CheckRedirect: redirect, CheckRedirect: redirect,
Timeout: defaultHTTPClientTimeout,
Transport: rt, Transport: rt,
} }
) )

View File

@ -24,15 +24,12 @@ import (
) )
const ( const (
defaultMaxRetries = 3 locationHeader = "Location"
defaultDelay = 3 * time.Second rateLimitHeader = "RateLimit-Limit"
locationHeader = "Location" rateRemainingHeader = "RateLimit-Remaining"
rateLimitHeader = "RateLimit-Limit" rateResetHeader = "RateLimit-Reset"
rateRemainingHeader = "RateLimit-Remaining" retryAfterHeader = "Retry-After"
rateResetHeader = "RateLimit-Reset" retryAttemptHeader = "Retry-Attempt"
retryAfterHeader = "Retry-After"
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
} }
} }