OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Threading; |
| 36 |
| 37 using Grpc.Core.Internal; |
| 38 using Grpc.Core.Utils; |
| 39 |
| 40 namespace Grpc.Core |
| 41 { |
| 42 /// <summary> |
| 43 /// Token for propagating context of server side handlers to child calls. |
| 44 /// In situations when a backend is making calls to another backend, |
| 45 /// it makes sense to propagate properties like deadline and cancellation |
| 46 /// token of the server call to the child call. |
| 47 /// The gRPC native layer provides some other contexts (like tracing context
) that |
| 48 /// are not accessible to explicitly C# layer, but this token still allows p
ropagating them. |
| 49 /// </summary> |
| 50 public class ContextPropagationToken |
| 51 { |
| 52 /// <summary> |
| 53 /// Default propagation mask used by C core. |
| 54 /// </summary> |
| 55 private const ContextPropagationFlags DefaultCoreMask = (ContextPropagat
ionFlags)0xffff; |
| 56 |
| 57 /// <summary> |
| 58 /// Default propagation mask used by C# - we want to propagate deadline |
| 59 /// and cancellation token by our own means. |
| 60 /// </summary> |
| 61 internal const ContextPropagationFlags DefaultMask = DefaultCoreMask |
| 62 & ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cance
llation; |
| 63 |
| 64 readonly CallSafeHandle parentCall; |
| 65 readonly DateTime deadline; |
| 66 readonly CancellationToken cancellationToken; |
| 67 readonly ContextPropagationOptions options; |
| 68 |
| 69 internal ContextPropagationToken(CallSafeHandle parentCall, DateTime dea
dline, CancellationToken cancellationToken, ContextPropagationOptions options) |
| 70 { |
| 71 this.parentCall = GrpcPreconditions.CheckNotNull(parentCall); |
| 72 this.deadline = deadline; |
| 73 this.cancellationToken = cancellationToken; |
| 74 this.options = options ?? ContextPropagationOptions.Default; |
| 75 } |
| 76 |
| 77 /// <summary> |
| 78 /// Gets the native handle of the parent call. |
| 79 /// </summary> |
| 80 internal CallSafeHandle ParentCall |
| 81 { |
| 82 get |
| 83 { |
| 84 return this.parentCall; |
| 85 } |
| 86 } |
| 87 |
| 88 /// <summary> |
| 89 /// Gets the parent call's deadline. |
| 90 /// </summary> |
| 91 internal DateTime ParentDeadline |
| 92 { |
| 93 get |
| 94 { |
| 95 return this.deadline; |
| 96 } |
| 97 } |
| 98 |
| 99 /// <summary> |
| 100 /// Gets the parent call's cancellation token. |
| 101 /// </summary> |
| 102 internal CancellationToken ParentCancellationToken |
| 103 { |
| 104 get |
| 105 { |
| 106 return this.cancellationToken; |
| 107 } |
| 108 } |
| 109 |
| 110 /// <summary> |
| 111 /// Get the context propagation options. |
| 112 /// </summary> |
| 113 internal ContextPropagationOptions Options |
| 114 { |
| 115 get |
| 116 { |
| 117 return this.options; |
| 118 } |
| 119 } |
| 120 } |
| 121 |
| 122 /// <summary> |
| 123 /// Options for <see cref="ContextPropagationToken"/>. |
| 124 /// </summary> |
| 125 public class ContextPropagationOptions |
| 126 { |
| 127 /// <summary> |
| 128 /// The context propagation options that will be used by default. |
| 129 /// </summary> |
| 130 public static readonly ContextPropagationOptions Default = new ContextPr
opagationOptions(); |
| 131 |
| 132 bool propagateDeadline; |
| 133 bool propagateCancellation; |
| 134 |
| 135 /// <summary> |
| 136 /// Creates new context propagation options. |
| 137 /// </summary> |
| 138 /// <param name="propagateDeadline">If set to <c>true</c> parent call's
deadline will be propagated to the child call.</param> |
| 139 /// <param name="propagateCancellation">If set to <c>true</c> parent cal
l's cancellation token will be propagated to the child call.</param> |
| 140 public ContextPropagationOptions(bool propagateDeadline = true, bool pro
pagateCancellation = true) |
| 141 { |
| 142 this.propagateDeadline = propagateDeadline; |
| 143 this.propagateCancellation = propagateCancellation; |
| 144 } |
| 145 |
| 146 /// <summary><c>true</c> if parent call's deadline should be propagated
to the child call.</summary> |
| 147 public bool IsPropagateDeadline |
| 148 { |
| 149 get { return this.propagateDeadline; } |
| 150 } |
| 151 |
| 152 /// <summary><c>true</c> if parent call's cancellation token should be p
ropagated to the child call.</summary> |
| 153 public bool IsPropagateCancellation |
| 154 { |
| 155 get { return this.propagateCancellation; } |
| 156 } |
| 157 } |
| 158 |
| 159 /// <summary> |
| 160 /// Context propagation flags from grpc/grpc.h. |
| 161 /// </summary> |
| 162 [Flags] |
| 163 internal enum ContextPropagationFlags |
| 164 { |
| 165 Deadline = 1, |
| 166 CensusStatsContext = 2, |
| 167 CensusTracingContext = 4, |
| 168 Cancellation = 8 |
| 169 } |
| 170 } |
OLD | NEW |