ContextPropagationToken.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading;
  18. using Grpc.Core.Internal;
  19. using Grpc.Core.Utils;
  20. namespace Grpc.Core
  21. {
  22. /// <summary>
  23. /// Token for propagating context of server side handlers to child calls.
  24. /// In situations when a backend is making calls to another backend,
  25. /// it makes sense to propagate properties like deadline and cancellation
  26. /// token of the server call to the child call.
  27. /// The gRPC native layer provides some other contexts (like tracing context) that
  28. /// are not accessible to explicitly C# layer, but this token still allows propagating them.
  29. /// </summary>
  30. public class ContextPropagationToken
  31. {
  32. /// <summary>
  33. /// Default propagation mask used by C core.
  34. /// </summary>
  35. private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff;
  36. /// <summary>
  37. /// Default propagation mask used by C# - we want to propagate deadline
  38. /// and cancellation token by our own means.
  39. /// </summary>
  40. internal const ContextPropagationFlags DefaultMask = DefaultCoreMask
  41. & ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation;
  42. readonly CallSafeHandle parentCall;
  43. readonly DateTime deadline;
  44. readonly CancellationToken cancellationToken;
  45. readonly ContextPropagationOptions options;
  46. internal ContextPropagationToken(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options)
  47. {
  48. this.parentCall = GrpcPreconditions.CheckNotNull(parentCall);
  49. this.deadline = deadline;
  50. this.cancellationToken = cancellationToken;
  51. this.options = options ?? ContextPropagationOptions.Default;
  52. }
  53. /// <summary>
  54. /// Gets the native handle of the parent call.
  55. /// </summary>
  56. internal CallSafeHandle ParentCall
  57. {
  58. get
  59. {
  60. return this.parentCall;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets the parent call's deadline.
  65. /// </summary>
  66. internal DateTime ParentDeadline
  67. {
  68. get
  69. {
  70. return this.deadline;
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the parent call's cancellation token.
  75. /// </summary>
  76. internal CancellationToken ParentCancellationToken
  77. {
  78. get
  79. {
  80. return this.cancellationToken;
  81. }
  82. }
  83. /// <summary>
  84. /// Get the context propagation options.
  85. /// </summary>
  86. internal ContextPropagationOptions Options
  87. {
  88. get
  89. {
  90. return this.options;
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// Options for <see cref="ContextPropagationToken"/>.
  96. /// </summary>
  97. public class ContextPropagationOptions
  98. {
  99. /// <summary>
  100. /// The context propagation options that will be used by default.
  101. /// </summary>
  102. public static readonly ContextPropagationOptions Default = new ContextPropagationOptions();
  103. bool propagateDeadline;
  104. bool propagateCancellation;
  105. /// <summary>
  106. /// Creates new context propagation options.
  107. /// </summary>
  108. /// <param name="propagateDeadline">If set to <c>true</c> parent call's deadline will be propagated to the child call.</param>
  109. /// <param name="propagateCancellation">If set to <c>true</c> parent call's cancellation token will be propagated to the child call.</param>
  110. public ContextPropagationOptions(bool propagateDeadline = true, bool propagateCancellation = true)
  111. {
  112. this.propagateDeadline = propagateDeadline;
  113. this.propagateCancellation = propagateCancellation;
  114. }
  115. /// <summary><c>true</c> if parent call's deadline should be propagated to the child call.</summary>
  116. public bool IsPropagateDeadline
  117. {
  118. get { return this.propagateDeadline; }
  119. }
  120. /// <summary><c>true</c> if parent call's cancellation token should be propagated to the child call.</summary>
  121. public bool IsPropagateCancellation
  122. {
  123. get { return this.propagateCancellation; }
  124. }
  125. }
  126. /// <summary>
  127. /// Context propagation flags from grpc/grpc.h.
  128. /// </summary>
  129. [Flags]
  130. internal enum ContextPropagationFlags
  131. {
  132. Deadline = 1,
  133. CensusStatsContext = 2,
  134. CensusTracingContext = 4,
  135. Cancellation = 8
  136. }
  137. }