mirror of https://github.com/grpc/grpc.git
Merge pull request #17766 from jtattermusch/context_propagation_token_refactor
Refactor ContextPropagationToken to allow moving to Grpc.Core.Api packagepull/17820/head
commit
f0bfcd864c
9 changed files with 254 additions and 137 deletions
@ -0,0 +1,59 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// Copyright 2019 The gRPC Authors |
||||||
|
// |
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
|
||||||
|
namespace Grpc.Core |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Options for <see cref="ContextPropagationToken"/>. |
||||||
|
/// </summary> |
||||||
|
public class ContextPropagationOptions |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// The context propagation options that will be used by default. |
||||||
|
/// </summary> |
||||||
|
public static readonly ContextPropagationOptions Default = new ContextPropagationOptions(); |
||||||
|
|
||||||
|
bool propagateDeadline; |
||||||
|
bool propagateCancellation; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates new context propagation options. |
||||||
|
/// </summary> |
||||||
|
/// <param name="propagateDeadline">If set to <c>true</c> parent call's deadline will be propagated to the child call.</param> |
||||||
|
/// <param name="propagateCancellation">If set to <c>true</c> parent call's cancellation token will be propagated to the child call.</param> |
||||||
|
public ContextPropagationOptions(bool propagateDeadline = true, bool propagateCancellation = true) |
||||||
|
{ |
||||||
|
this.propagateDeadline = propagateDeadline; |
||||||
|
this.propagateCancellation = propagateCancellation; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary><c>true</c> if parent call's deadline should be propagated to the child call.</summary> |
||||||
|
public bool IsPropagateDeadline |
||||||
|
{ |
||||||
|
get { return this.propagateDeadline; } |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary><c>true</c> if parent call's cancellation token should be propagated to the child call.</summary> |
||||||
|
public bool IsPropagateCancellation |
||||||
|
{ |
||||||
|
get { return this.propagateCancellation; } |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// Copyright 2019 The gRPC Authors |
||||||
|
// |
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
|
||||||
|
namespace Grpc.Core.Internal |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Context propagation flags from grpc/grpc.h. |
||||||
|
/// </summary> |
||||||
|
[Flags] |
||||||
|
internal enum ContextPropagationFlags |
||||||
|
{ |
||||||
|
Deadline = 1, |
||||||
|
CensusStatsContext = 2, |
||||||
|
CensusTracingContext = 4, |
||||||
|
Cancellation = 8 |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,119 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// Copyright 2019 The gRPC Authors |
||||||
|
// |
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Threading; |
||||||
|
|
||||||
|
using Grpc.Core.Utils; |
||||||
|
|
||||||
|
namespace Grpc.Core.Internal |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Implementation of <c>ContextPropagationToken</c> that carries |
||||||
|
/// all fields needed for context propagation by C-core based implementation of gRPC. |
||||||
|
/// Instances of <c>ContextPropagationToken</c> that are not of this |
||||||
|
/// type will be recognized as "foreign" and will be silently ignored |
||||||
|
/// (treated as if null). |
||||||
|
/// </summary> |
||||||
|
internal class ContextPropagationTokenImpl : ContextPropagationToken |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Default propagation mask used by C core. |
||||||
|
/// </summary> |
||||||
|
private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Default propagation mask used by C# - we want to propagate deadline |
||||||
|
/// and cancellation token by our own means, everything else will be propagated |
||||||
|
/// by C core automatically (according to <c>DefaultCoreMask</c>). |
||||||
|
/// </summary> |
||||||
|
internal const ContextPropagationFlags DefaultMask = DefaultCoreMask |
||||||
|
& ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation; |
||||||
|
|
||||||
|
readonly CallSafeHandle parentCall; |
||||||
|
readonly DateTime deadline; |
||||||
|
readonly CancellationToken cancellationToken; |
||||||
|
readonly ContextPropagationOptions options; |
||||||
|
|
||||||
|
internal ContextPropagationTokenImpl(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options) |
||||||
|
{ |
||||||
|
this.parentCall = GrpcPreconditions.CheckNotNull(parentCall); |
||||||
|
this.deadline = deadline; |
||||||
|
this.cancellationToken = cancellationToken; |
||||||
|
this.options = options ?? ContextPropagationOptions.Default; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Gets the native handle of the parent call. |
||||||
|
/// </summary> |
||||||
|
internal CallSafeHandle ParentCall |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return this.parentCall; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Gets the parent call's deadline. |
||||||
|
/// </summary> |
||||||
|
internal DateTime ParentDeadline |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return this.deadline; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Gets the parent call's cancellation token. |
||||||
|
/// </summary> |
||||||
|
internal CancellationToken ParentCancellationToken |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return this.cancellationToken; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Get the context propagation options. |
||||||
|
/// </summary> |
||||||
|
internal ContextPropagationOptions Options |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return this.options; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
internal static class ContextPropagationTokenExtensions |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Converts given <c>ContextPropagationToken</c> to <c>ContextPropagationTokenImpl</c> |
||||||
|
/// if possible or returns null. |
||||||
|
/// Being able to convert means that the context propagation token is recognized as |
||||||
|
/// "ours" (was created by this implementation). |
||||||
|
/// </summary> |
||||||
|
public static ContextPropagationTokenImpl AsImplOrNull(this ContextPropagationToken instanceOrNull) |
||||||
|
{ |
||||||
|
return instanceOrNull as ContextPropagationTokenImpl; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue