CallOptions.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. namespace Grpc.Core
  20. {
  21. /// <summary>
  22. /// Options for calls made by client.
  23. /// </summary>
  24. public struct CallOptions
  25. {
  26. Metadata headers;
  27. DateTime? deadline;
  28. CancellationToken cancellationToken;
  29. WriteOptions writeOptions;
  30. ContextPropagationToken propagationToken;
  31. CallCredentials credentials;
  32. CallFlags flags;
  33. /// <summary>
  34. /// Creates a new instance of <c>CallOptions</c> struct.
  35. /// </summary>
  36. /// <param name="headers">Headers to be sent with the call.</param>
  37. /// <param name="deadline">Deadline for the call to finish. null means no deadline.</param>
  38. /// <param name="cancellationToken">Can be used to request cancellation of the call.</param>
  39. /// <param name="writeOptions">Write options that will be used for this call.</param>
  40. /// <param name="propagationToken">Context propagation token obtained from <see cref="ServerCallContext"/>.</param>
  41. /// <param name="credentials">Credentials to use for this call.</param>
  42. public CallOptions(Metadata headers = null, DateTime? deadline = null, CancellationToken cancellationToken = default(CancellationToken),
  43. WriteOptions writeOptions = null, ContextPropagationToken propagationToken = null, CallCredentials credentials = null)
  44. {
  45. this.headers = headers;
  46. this.deadline = deadline;
  47. this.cancellationToken = cancellationToken;
  48. this.writeOptions = writeOptions;
  49. this.propagationToken = propagationToken;
  50. this.credentials = credentials;
  51. this.flags = default(CallFlags);
  52. }
  53. /// <summary>
  54. /// Headers to send at the beginning of the call.
  55. /// </summary>
  56. public Metadata Headers
  57. {
  58. get { return headers; }
  59. }
  60. /// <summary>
  61. /// Call deadline.
  62. /// </summary>
  63. public DateTime? Deadline
  64. {
  65. get { return deadline; }
  66. }
  67. /// <summary>
  68. /// Token that can be used for cancelling the call on the client side.
  69. /// Cancelling the token will request cancellation
  70. /// of the remote call. Best effort will be made to deliver the cancellation
  71. /// notification to the server and interaction of the call with the server side
  72. /// will be terminated. Unless the call finishes before the cancellation could
  73. /// happen (there is an inherent race),
  74. /// the call will finish with <c>StatusCode.Cancelled</c> status.
  75. /// </summary>
  76. public CancellationToken CancellationToken
  77. {
  78. get { return cancellationToken; }
  79. }
  80. /// <summary>
  81. /// Write options that will be used for this call.
  82. /// </summary>
  83. public WriteOptions WriteOptions
  84. {
  85. get { return this.writeOptions; }
  86. }
  87. /// <summary>
  88. /// Token for propagating parent call context.
  89. /// </summary>
  90. public ContextPropagationToken PropagationToken
  91. {
  92. get { return this.propagationToken; }
  93. }
  94. /// <summary>
  95. /// Credentials to use for this call.
  96. /// </summary>
  97. public CallCredentials Credentials
  98. {
  99. get { return this.credentials; }
  100. }
  101. /// <summary>
  102. /// If <c>true</c> and channel is in <c>ChannelState.TransientFailure</c>, the call will attempt waiting for the channel to recover
  103. /// instead of failing immediately (which is the default "FailFast" semantics).
  104. /// Note: experimental API that can change or be removed without any prior notice.
  105. /// </summary>
  106. public bool IsWaitForReady
  107. {
  108. get { return (this.flags & CallFlags.WaitForReady) == CallFlags.WaitForReady; }
  109. }
  110. /// <summary>
  111. /// Flags to use for this call.
  112. /// </summary>
  113. internal CallFlags Flags
  114. {
  115. get { return this.flags; }
  116. }
  117. /// <summary>
  118. /// Returns new instance of <see cref="CallOptions"/> with
  119. /// <c>Headers</c> set to the value provided. Values of all other fields are preserved.
  120. /// </summary>
  121. /// <param name="headers">The headers.</param>
  122. public CallOptions WithHeaders(Metadata headers)
  123. {
  124. var newOptions = this;
  125. newOptions.headers = headers;
  126. return newOptions;
  127. }
  128. /// <summary>
  129. /// Returns new instance of <see cref="CallOptions"/> with
  130. /// <c>Deadline</c> set to the value provided. Values of all other fields are preserved.
  131. /// </summary>
  132. /// <param name="deadline">The deadline.</param>
  133. public CallOptions WithDeadline(DateTime deadline)
  134. {
  135. var newOptions = this;
  136. newOptions.deadline = deadline;
  137. return newOptions;
  138. }
  139. /// <summary>
  140. /// Returns new instance of <see cref="CallOptions"/> with
  141. /// <c>CancellationToken</c> set to the value provided. Values of all other fields are preserved.
  142. /// </summary>
  143. /// <param name="cancellationToken">The cancellation token.</param>
  144. public CallOptions WithCancellationToken(CancellationToken cancellationToken)
  145. {
  146. var newOptions = this;
  147. newOptions.cancellationToken = cancellationToken;
  148. return newOptions;
  149. }
  150. /// <summary>
  151. /// Returns new instance of <see cref="CallOptions"/> with
  152. /// <c>WriteOptions</c> set to the value provided. Values of all other fields are preserved.
  153. /// </summary>
  154. /// <param name="writeOptions">The write options.</param>
  155. public CallOptions WithWriteOptions(WriteOptions writeOptions)
  156. {
  157. var newOptions = this;
  158. newOptions.writeOptions = writeOptions;
  159. return newOptions;
  160. }
  161. /// <summary>
  162. /// Returns new instance of <see cref="CallOptions"/> with
  163. /// <c>PropagationToken</c> set to the value provided. Values of all other fields are preserved.
  164. /// </summary>
  165. /// <param name="propagationToken">The context propagation token.</param>
  166. public CallOptions WithPropagationToken(ContextPropagationToken propagationToken)
  167. {
  168. var newOptions = this;
  169. newOptions.propagationToken = propagationToken;
  170. return newOptions;
  171. }
  172. /// <summary>
  173. /// Returns new instance of <see cref="CallOptions"/> with
  174. /// <c>Credentials</c> set to the value provided. Values of all other fields are preserved.
  175. /// </summary>
  176. /// <param name="credentials">The call credentials.</param>
  177. public CallOptions WithCredentials(CallCredentials credentials)
  178. {
  179. var newOptions = this;
  180. newOptions.credentials = credentials;
  181. return newOptions;
  182. }
  183. /// <summary>
  184. /// Returns new instance of <see cref="CallOptions"/> with "WaitForReady" semantics enabled/disabled.
  185. /// <see cref="IsWaitForReady"/>.
  186. /// Note: experimental API that can change or be removed without any prior notice.
  187. /// </summary>
  188. public CallOptions WithWaitForReady(bool waitForReady = true)
  189. {
  190. if (waitForReady)
  191. {
  192. return WithFlags(this.flags | CallFlags.WaitForReady);
  193. }
  194. return WithFlags(this.flags & ~CallFlags.WaitForReady);
  195. }
  196. /// <summary>
  197. /// Returns new instance of <see cref="CallOptions"/> with
  198. /// <c>Flags</c> set to the value provided. Values of all other fields are preserved.
  199. /// </summary>
  200. /// <param name="flags">The call flags.</param>
  201. internal CallOptions WithFlags(CallFlags flags)
  202. {
  203. var newOptions = this;
  204. newOptions.flags = flags;
  205. return newOptions;
  206. }
  207. }
  208. }