Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Threading; |
| 21 | |
| 22 | using Grpc.Core.Internal; |
| 23 | using Grpc.Core.Utils; |
| 24 | |
| 25 | namespace Grpc.Core |
| 26 | { |
| 27 | /// <summary> |
| 28 | /// Token for propagating context of server side handlers to child calls. |
| 29 | /// In situations when a backend is making calls to another backend, |
| 30 | /// it makes sense to propagate properties like deadline and cancellation |
| 31 | /// token of the server call to the child call. |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 32 | /// The gRPC native layer provides some other contexts (like tracing context) that |
| 33 | /// are not accessible to explicitly C# layer, but this token still allows propagating them. |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 34 | /// </summary> |
| 35 | public class ContextPropagationToken |
| 36 | { |
| 37 | /// <summary> |
| 38 | /// Default propagation mask used by C core. |
| 39 | /// </summary> |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 40 | private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff; |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 41 | |
| 42 | /// <summary> |
| 43 | /// Default propagation mask used by C# - we want to propagate deadline |
| 44 | /// and cancellation token by our own means. |
| 45 | /// </summary> |
| 46 | internal const ContextPropagationFlags DefaultMask = DefaultCoreMask |
| 47 | & ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation; |
| 48 | |
| 49 | readonly CallSafeHandle parentCall; |
| 50 | readonly DateTime deadline; |
| 51 | readonly CancellationToken cancellationToken; |
| 52 | readonly ContextPropagationOptions options; |
| 53 | |
| 54 | internal ContextPropagationToken(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options) |
| 55 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 56 | this.parentCall = GrpcPreconditions.CheckNotNull(parentCall); |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 57 | this.deadline = deadline; |
| 58 | this.cancellationToken = cancellationToken; |
| 59 | this.options = options ?? ContextPropagationOptions.Default; |
| 60 | } |
| 61 | |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 62 | /// <summary> |
| 63 | /// Gets the native handle of the parent call. |
| 64 | /// </summary> |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 65 | internal CallSafeHandle ParentCall |
| 66 | { |
| 67 | get |
| 68 | { |
| 69 | return this.parentCall; |
| 70 | } |
| 71 | } |
| 72 | |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 73 | /// <summary> |
| 74 | /// Gets the parent call's deadline. |
| 75 | /// </summary> |
| 76 | internal DateTime ParentDeadline |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 77 | { |
| 78 | get |
| 79 | { |
| 80 | return this.deadline; |
| 81 | } |
| 82 | } |
| 83 | |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 84 | /// <summary> |
| 85 | /// Gets the parent call's cancellation token. |
| 86 | /// </summary> |
| 87 | internal CancellationToken ParentCancellationToken |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 88 | { |
| 89 | get |
| 90 | { |
| 91 | return this.cancellationToken; |
| 92 | } |
| 93 | } |
| 94 | |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 95 | /// <summary> |
| 96 | /// Get the context propagation options. |
| 97 | /// </summary> |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 98 | internal ContextPropagationOptions Options |
| 99 | { |
| 100 | get |
| 101 | { |
| 102 | return this.options; |
| 103 | } |
| 104 | } |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Options for <see cref="ContextPropagationToken"/>. |
| 109 | /// </summary> |
| 110 | public class ContextPropagationOptions |
| 111 | { |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 112 | /// <summary> |
| 113 | /// The context propagation options that will be used by default. |
| 114 | /// </summary> |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 115 | public static readonly ContextPropagationOptions Default = new ContextPropagationOptions(); |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 116 | |
| 117 | bool propagateDeadline; |
| 118 | bool propagateCancellation; |
| 119 | |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 120 | /// <summary> |
| 121 | /// Creates new context propagation options. |
| 122 | /// </summary> |
| 123 | /// <param name="propagateDeadline">If set to <c>true</c> parent call's deadline will be propagated to the child call.</param> |
| 124 | /// <param name="propagateCancellation">If set to <c>true</c> parent call's cancellation token will be propagated to the child call.</param> |
| 125 | public ContextPropagationOptions(bool propagateDeadline = true, bool propagateCancellation = true) |
| 126 | { |
| 127 | this.propagateDeadline = propagateDeadline; |
| 128 | this.propagateCancellation = propagateCancellation; |
| 129 | } |
| 130 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 131 | /// <summary><c>true</c> if parent call's deadline should be propagated to the child call.</summary> |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 132 | public bool IsPropagateDeadline |
| 133 | { |
| 134 | get { return this.propagateDeadline; } |
| 135 | } |
| 136 | |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 137 | /// <summary><c>true</c> if parent call's cancellation token should be propagated to the child call.</summary> |
Jan Tattermusch | 9698b5b | 2015-08-10 16:40:19 -0700 | [diff] [blame] | 138 | public bool IsPropagateCancellation |
| 139 | { |
| 140 | get { return this.propagateCancellation; } |
| 141 | } |
Jan Tattermusch | 392fae2 | 2015-08-08 22:21:57 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Context propagation flags from grpc/grpc.h. |
| 146 | /// </summary> |
| 147 | [Flags] |
| 148 | internal enum ContextPropagationFlags |
| 149 | { |
| 150 | Deadline = 1, |
| 151 | CensusStatsContext = 2, |
| 152 | CensusTracingContext = 4, |
| 153 | Cancellation = 8 |
| 154 | } |
| 155 | } |