• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 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 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.
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.
34     /// </summary>
35     public class ContextPropagationToken
36     {
37         /// <summary>
38         /// Default propagation mask used by C core.
39         /// </summary>
40         private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff;
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 
ContextPropagationToken(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options)54         internal ContextPropagationToken(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options)
55         {
56             this.parentCall = GrpcPreconditions.CheckNotNull(parentCall);
57             this.deadline = deadline;
58             this.cancellationToken = cancellationToken;
59             this.options = options ?? ContextPropagationOptions.Default;
60         }
61 
62         /// <summary>
63         /// Gets the native handle of the parent call.
64         /// </summary>
65         internal CallSafeHandle ParentCall
66         {
67             get
68             {
69                 return this.parentCall;
70             }
71         }
72 
73         /// <summary>
74         /// Gets the parent call's deadline.
75         /// </summary>
76         internal DateTime ParentDeadline
77         {
78             get
79             {
80                 return this.deadline;
81             }
82         }
83 
84         /// <summary>
85         /// Gets the parent call's cancellation token.
86         /// </summary>
87         internal CancellationToken ParentCancellationToken
88         {
89             get
90             {
91                 return this.cancellationToken;
92             }
93         }
94 
95         /// <summary>
96         /// Get the context propagation options.
97         /// </summary>
98         internal ContextPropagationOptions Options
99         {
100             get
101             {
102                 return this.options;
103             }
104         }
105     }
106 
107     /// <summary>
108     /// Options for <see cref="ContextPropagationToken"/>.
109     /// </summary>
110     public class ContextPropagationOptions
111     {
112         /// <summary>
113         /// The context propagation options that will be used by default.
114         /// </summary>
115         public static readonly ContextPropagationOptions Default = new ContextPropagationOptions();
116 
117         bool propagateDeadline;
118         bool propagateCancellation;
119 
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>
ContextPropagationOptions(bool propagateDeadline = true, bool propagateCancellation = true)125         public ContextPropagationOptions(bool propagateDeadline = true, bool propagateCancellation = true)
126         {
127             this.propagateDeadline = propagateDeadline;
128             this.propagateCancellation = propagateCancellation;
129         }
130 
131         /// <summary><c>true</c> if parent call's deadline should be propagated to the child call.</summary>
132         public bool IsPropagateDeadline
133         {
134             get { return this.propagateDeadline; }
135         }
136 
137         /// <summary><c>true</c> if parent call's cancellation token should be propagated to the child call.</summary>
138         public bool IsPropagateCancellation
139         {
140             get { return this.propagateCancellation; }
141         }
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 }
156