1 #region Copyright notice and license 2 3 // Copyright 2019 The gRPC Authors 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using Grpc.Core.Utils; 21 22 namespace Grpc.Core.Internal 23 { 24 internal static class CallOptionsExtensions 25 { 26 /// <summary> 27 /// Returns a new instance of <see cref="CallOptions"/> with 28 /// all previously unset values set to their defaults and deadline and cancellation 29 /// token propagated when appropriate. 30 /// </summary> Normalize(this CallOptions options)31 internal static CallOptions Normalize(this CallOptions options) 32 { 33 var newOptions = options; 34 // silently ignore the context propagation token if it wasn't produced by "us" 35 var propagationTokenImpl = options.PropagationToken.AsImplOrNull(); 36 if (propagationTokenImpl != null) 37 { 38 if (propagationTokenImpl.Options.IsPropagateDeadline) 39 { 40 GrpcPreconditions.CheckArgument(!newOptions.Deadline.HasValue, 41 "Cannot propagate deadline from parent call. The deadline has already been set explicitly."); 42 newOptions = newOptions.WithDeadline(propagationTokenImpl.ParentDeadline); 43 } 44 if (propagationTokenImpl.Options.IsPropagateCancellation) 45 { 46 GrpcPreconditions.CheckArgument(!newOptions.CancellationToken.CanBeCanceled, 47 "Cannot propagate cancellation token from parent call. The cancellation token has already been set to a non-default value."); 48 newOptions = newOptions.WithCancellationToken(propagationTokenImpl.ParentCancellationToken); 49 } 50 } 51 52 newOptions = newOptions.WithHeaders(newOptions.Headers ?? Metadata.Empty); 53 newOptions = newOptions.WithDeadline(newOptions.Deadline ?? DateTime.MaxValue); 54 return newOptions; 55 } 56 } 57 } 58