Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Simplify setting the HttpClient timeout #2746

Merged
merged 1 commit into from
May 22, 2024
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
45 changes: 45 additions & 0 deletions Src/Support/Google.Apis.Core/Util/HttpTimeoutInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2024 Google Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using Google.Apis.Http;
using System;

namespace Google.Apis.Core;

/// <summary>
/// Used for specifying a custom timeout for a <see cref="ConfigurableHttpClient"/>.
/// </summary>
public sealed class HttpTimeoutInitializer : IConfigurableHttpClientInitializer
{
/// <summary>
/// The timeout to set on <see cref="ConfigurableHttpClient"/> instances on which this initializer is applied.
/// </summary>
public TimeSpan HttpClientTimeout { get; }

/// <summary>
/// Creates a new instance with the given timeout.
/// </summary>
/// <remarks>
/// This initializer relies on <see cref="ConfigurableHttpClient"/> for validation.
/// That means that <paramref name="httpClientTimeout"/> is not validated in this constructor.
/// It's when this initializer is applied to a <see cref="ConfigurableHttpClient"/>
/// that the value will be validated and an exception possibly thrown.
/// </remarks>
public HttpTimeoutInitializer(TimeSpan httpClientTimeout) => HttpClientTimeout = httpClientTimeout;

/// <inheritdoc/>
public void Initialize(ConfigurableHttpClient httpClient) => httpClient.Timeout = HttpClientTimeout;
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,19 @@ public void InvalidApplicationName()
}));
}

[Fact]
public void HttpClientTimeout()
{
var timeout = TimeSpan.FromHours(10);
var service = new MockClientService(new BaseClientService.Initializer
{
HttpClientTimeout = timeout
});

Assert.Equal(timeout, service.HttpClientTimeout);
Assert.Equal(timeout, service.HttpClient.Timeout);
}

[Fact]
public void VersionHeader()
{
Expand Down
22 changes: 22 additions & 0 deletions Src/Support/Google.Apis/Services/BaseClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ You may obtain a copy of the License at
limitations under the License.
*/

using Google.Apis.Core;
using Google.Apis.Discovery;
using Google.Apis.Http;
using Google.Apis.Json;
Expand Down Expand Up @@ -123,6 +124,13 @@ public class Initializer
/// </remarks>
public string UniverseDomain { get; set; }

/// <summary>
/// The timeout to set on <see cref="HttpClient"/> instances used by the service.
/// May be null, in which case the default timeout values on <see cref="HttpClient"/> instances
/// used by this service will be left unchanged.
/// </summary>
public TimeSpan? HttpClientTimeout { get; set; }

/// <summary>
/// Builder for the x-goog-api-client header, collecting version information.
/// Services automatically add the API library version to this.
Expand Down Expand Up @@ -180,6 +188,7 @@ protected BaseClientService(Initializer initializer)
ApplicationName = initializer.ApplicationName;
BaseUriOverride = initializer.BaseUri;
UniverseDomain = initializer.UniverseDomain;
HttpClientTimeout = initializer.HttpClientTimeout;
ValidateParameters = initializer.ValidateParameters;
if (ApplicationName == null)
{
Expand Down Expand Up @@ -213,6 +222,13 @@ protected BaseClientService(Initializer initializer)
/// </remarks>
public string UniverseDomain { get; set; }

/// <summary>
/// The timeout to set on <see cref="HttpClient"/> instances used by the service.
/// May be null, in which case the default timeout values on <see cref="HttpClient"/> instances
/// used by this service will be left unchanged.
/// </summary>
public TimeSpan? HttpClientTimeout { get; }

/// <summary>Returns <c>true</c> if this service contains the specified feature.</summary>
private bool HasFeature(Features feature)
{
Expand Down Expand Up @@ -244,6 +260,12 @@ private ConfigurableHttpClient CreateHttpClient(Initializer initializer, string
CreateBackOffHandler));
}

// Add HTTP client timeout initializer if necessary.
if (HttpClientTimeout is not null)
{
args.Initializers.Add(new HttpTimeoutInitializer(HttpClientTimeout.Value));
}

var httpClient = factory.CreateHttpClient(args);
if (initializer.MaxUrlLength > 0)
{
Expand Down