blob: fe5c8a5c99ace2f77db6bbd321885b738fd3135d [file] [log] [blame]
Jan Tattermusch392fae22015-08-08 22:21:57 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch392fae22015-08-08 22:21:57 -07004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermusch392fae22015-08-08 22:21:57 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch392fae22015-08-08 22:21:57 -070010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermusch392fae22015-08-08 22:21:57 -070016
17#endregion
18
19using System;
20using System.Threading;
21
22using Grpc.Core.Internal;
23using Grpc.Core.Utils;
24
25namespace 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 Tattermusch12855fc2015-08-24 16:43:23 -070032 /// 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 Tattermusch392fae22015-08-08 22:21:57 -070034 /// </summary>
35 public class ContextPropagationToken
36 {
37 /// <summary>
38 /// Default propagation mask used by C core.
39 /// </summary>
Jan Tattermusch9698b5b2015-08-10 16:40:19 -070040 private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff;
Jan Tattermusch392fae22015-08-08 22:21:57 -070041
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 Tattermusch7a3ee6a2016-02-18 10:36:02 -080056 this.parentCall = GrpcPreconditions.CheckNotNull(parentCall);
Jan Tattermusch392fae22015-08-08 22:21:57 -070057 this.deadline = deadline;
58 this.cancellationToken = cancellationToken;
59 this.options = options ?? ContextPropagationOptions.Default;
60 }
61
Jan Tattermusch9698b5b2015-08-10 16:40:19 -070062 /// <summary>
63 /// Gets the native handle of the parent call.
64 /// </summary>
Jan Tattermusch392fae22015-08-08 22:21:57 -070065 internal CallSafeHandle ParentCall
66 {
67 get
68 {
69 return this.parentCall;
70 }
71 }
72
Jan Tattermusch9698b5b2015-08-10 16:40:19 -070073 /// <summary>
74 /// Gets the parent call's deadline.
75 /// </summary>
76 internal DateTime ParentDeadline
Jan Tattermusch392fae22015-08-08 22:21:57 -070077 {
78 get
79 {
80 return this.deadline;
81 }
82 }
83
Jan Tattermusch9698b5b2015-08-10 16:40:19 -070084 /// <summary>
85 /// Gets the parent call's cancellation token.
86 /// </summary>
87 internal CancellationToken ParentCancellationToken
Jan Tattermusch392fae22015-08-08 22:21:57 -070088 {
89 get
90 {
91 return this.cancellationToken;
92 }
93 }
94
Jan Tattermusch9698b5b2015-08-10 16:40:19 -070095 /// <summary>
96 /// Get the context propagation options.
97 /// </summary>
Jan Tattermusch392fae22015-08-08 22:21:57 -070098 internal ContextPropagationOptions Options
99 {
100 get
101 {
102 return this.options;
103 }
104 }
Jan Tattermusch392fae22015-08-08 22:21:57 -0700105 }
106
107 /// <summary>
108 /// Options for <see cref="ContextPropagationToken"/>.
109 /// </summary>
110 public class ContextPropagationOptions
111 {
Jan Tattermusch9698b5b2015-08-10 16:40:19 -0700112 /// <summary>
113 /// The context propagation options that will be used by default.
114 /// </summary>
Jan Tattermusch392fae22015-08-08 22:21:57 -0700115 public static readonly ContextPropagationOptions Default = new ContextPropagationOptions();
Jan Tattermusch9698b5b2015-08-10 16:40:19 -0700116
117 bool propagateDeadline;
118 bool propagateCancellation;
119
Jan Tattermusch9698b5b2015-08-10 16:40:19 -0700120 /// <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 Tattermusch12855fc2015-08-24 16:43:23 -0700131 /// <summary><c>true</c> if parent call's deadline should be propagated to the child call.</summary>
Jan Tattermusch9698b5b2015-08-10 16:40:19 -0700132 public bool IsPropagateDeadline
133 {
134 get { return this.propagateDeadline; }
135 }
136
Jan Tattermusch12855fc2015-08-24 16:43:23 -0700137 /// <summary><c>true</c> if parent call's cancellation token should be propagated to the child call.</summary>
Jan Tattermusch9698b5b2015-08-10 16:40:19 -0700138 public bool IsPropagateCancellation
139 {
140 get { return this.propagateCancellation; }
141 }
Jan Tattermusch392fae22015-08-08 22:21:57 -0700142 }
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}