Skip to content

Commit

Permalink
Mostly-internal fix-ups to CallSettings
Browse files Browse the repository at this point in the history
CallSettings.Merge is now internal again, as callers can
always use MergedWith.

Other than that, it's just using the new methods to simplify
internal code.
  • Loading branch information
jskeet committed Oct 25, 2016
1 parent 32c84ff commit 6a9a88d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 32 deletions.
20 changes: 7 additions & 13 deletions src/Google.Api.Gax.Grpc/ApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ internal ApiCall(
private readonly CallSettings _baseCallSettings;

private T Call<T>(TRequest request, CallSettings perCallCallSettings, Func<CallSettings, T> fn)
{
CallSettings callSettings = CallSettings.Merge(_baseCallSettings, perCallCallSettings);
return fn(callSettings);
}
=> fn(_baseCallSettings.MergedWith(perCallCallSettings));

/// <summary>
/// Performs an RPC call asynchronously.
Expand All @@ -80,23 +77,20 @@ public Task<TResponse> Async(TRequest request, CallSettings perCallCallSettings)
public TResponse Sync(TRequest request, CallSettings perCallCallSettings) =>
Call(request, perCallCallSettings, callSettings => _syncCall(request, callSettings));

internal ApiCall<TRequest, TResponse> WithUserAgent(string userAgent)
{
internal ApiCall<TRequest, TResponse> WithUserAgent(string userAgent) =>
// TODO: Check that this is what we want. It allows call settings to remove our
// user agent header. The caller can always do this manually anyway, of course, so
// I'm tempted to leave it...
return new ApiCall<TRequest, TResponse>(
new ApiCall<TRequest, TResponse>(
_asyncCall,
_syncCall,
CallSettings.Merge(CallSettings.ForUserAgent(userAgent), _baseCallSettings));
}
CallSettings.FromHeader(UserAgentBuilder.HeaderName, userAgent)
.MergedWith(_baseCallSettings));

internal ApiCall<TRequest, TResponse> WithRetry(IClock clock, IScheduler scheduler)
{
return new ApiCall<TRequest, TResponse>(
internal ApiCall<TRequest, TResponse> WithRetry(IClock clock, IScheduler scheduler) =>
new ApiCall<TRequest, TResponse>(
_asyncCall.WithRetry(clock, scheduler),
_syncCall.WithRetry(clock, scheduler),
_baseCallSettings);
}
}
}
7 changes: 2 additions & 5 deletions src/Google.Api.Gax.Grpc/ApiCallRetryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ internal static Func<TRequest, CallSettings, Task<TResponse>> WithRetry<TRequest
DateTime attemptDeadline = clock.GetCurrentDateTimeUtc() + callTimeout;
// Note: this handles a null total deadline due to "<" returning false if overallDeadline is null.
DateTime combinedDeadline = overallDeadline < attemptDeadline ? overallDeadline.Value : attemptDeadline;
CallSettings attemptCallSettings = CallSettings.Merge
(callSettings, CallSettings.FromCallTiming(CallTiming.FromDeadline(combinedDeadline)));
CallSettings attemptCallSettings = callSettings.WithCallTiming(CallTiming.FromDeadline(combinedDeadline));
try
{
return await fn(request, attemptCallSettings);
Expand Down Expand Up @@ -74,8 +73,7 @@ internal static Func<TRequest, CallSettings, TResponse> WithRetry<TRequest, TRes
DateTime attemptDeadline = clock.GetCurrentDateTimeUtc() + callTimeout;
// Note: this handles a null total deadline due to "<" returning false if overallDeadline is null.
DateTime combinedDeadline = overallDeadline < attemptDeadline ? overallDeadline.Value : attemptDeadline;
CallSettings attemptCallSettings = CallSettings.Merge
(callSettings, CallSettings.FromCallTiming(CallTiming.FromDeadline(combinedDeadline)));
CallSettings attemptCallSettings = callSettings.WithCallTiming(CallTiming.FromDeadline(combinedDeadline));
try
{
return fn(request, attemptCallSettings);
Expand All @@ -97,4 +95,3 @@ internal static Func<TRequest, CallSettings, TResponse> WithRetry<TRequest, TRes

}
}

8 changes: 1 addition & 7 deletions src/Google.Api.Gax.Grpc/CallSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public CallSettings(
/// <param name="original">Original settings. May be null.</param>
/// <param name="overlaid">Settings to overlay. May be null.</param>
/// <returns>A merged set of call settings.</returns>
public static CallSettings Merge(CallSettings original, CallSettings overlaid)
internal static CallSettings Merge(CallSettings original, CallSettings overlaid)
{
if (original == null)
{
Expand All @@ -107,12 +107,6 @@ public static CallSettings Merge(CallSettings original, CallSettings overlaid)
overlaid.PropagationToken ?? original.PropagationToken);
}

/// <summary>
/// Creates a <see cref="CallSettings"/> for the specified user agent.
/// </summary>
internal static CallSettings ForUserAgent(string userAgent) =>
FromHeaderMutation(metadata => metadata.Add(UserAgentBuilder.HeaderName, userAgent));

/// <summary>
/// Creates a <see cref="CallSettings"/> for the specified cancellation token.
/// </summary>
Expand Down
8 changes: 3 additions & 5 deletions src/Google.Api.Gax.Grpc/CallSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ namespace Google.Api.Gax.Grpc
public static class CallSettingsExtensions
{
/// <summary>
/// Shorthand for calling <see cref="CallSettings.Merge(CallSettings, CallSettings)"/>.
/// </summary>
/// <remarks>
/// This method merges the settings in <paramref name="overlaid"/> with those in
/// <paramref name="original"/>, with <paramref name="overlaid"/> taking priority.
/// If both arguments are null, the result is null. If one argument is null,
/// the other argument is returned. Otherwise, a new object is created with a property-wise
/// overlay. Any header mutations are combined, however: the mutation from the original is
/// overlay, where null values do not override non-null values.
/// Any header mutations are combined, however: the mutation from the original is
/// performed, then the mutation in the overlay.
/// </remarks>
/// </summary>
/// <param name="original">Original settings. May be null.</param>
/// <param name="overlaid">Settings to overlay. May be null.</param>
/// <returns>A merged set of call settings, or null if both parameters are null.</returns>
Expand Down
2 changes: 1 addition & 1 deletion src/Google.Api.Gax.Grpc/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ApiCall<TRequest, TResponse> BuildApiCall<TRequest, TResponse>(
where TRequest : class, IMessage<TRequest>
where TResponse : class, IMessage<TResponse>
{
CallSettings baseCallSettings = CallSettings.Merge(_clientCallSettings, perMethodCallSettings);
CallSettings baseCallSettings = _clientCallSettings.MergedWith(perMethodCallSettings);
// These operations are applied in reverse order.
// I.e. User-agent is added first, then retry is performed.
return ApiCall.Create(asyncGrpcCall, syncGrpcCall, baseCallSettings, Clock)
Expand Down
2 changes: 1 addition & 1 deletion src/Google.Api.Gax.Grpc/PageStreamingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task<bool> MoveNext(CancellationToken cancellationToken)
CallSettings effectiveCallSettings = _callSettings;
if (cancellationToken != default(CancellationToken))
{
effectiveCallSettings = CallSettings.Merge(_callSettings, CallSettings.FromCancellationToken(cancellationToken));
effectiveCallSettings = _callSettings.WithCancellationToken(cancellationToken);
}
Current = await _apiCall.Async(_request, effectiveCallSettings);
var nextPageToken = Current.NextPageToken;
Expand Down

0 comments on commit 6a9a88d

Please sign in to comment.