CallOptions.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  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. */
  16. /// Options applied to a call.
  17. ///
  18. /// If set, these options are used in preference to any options configured on
  19. /// the client or its transport.
  20. ///
  21. /// You can create the default set of options, which defers all possible
  22. /// configuration to the transport, by using ``CallOptions/defaults``.
  23. public struct CallOptions: Sendable {
  24. /// The default timeout for the RPC.
  25. ///
  26. /// If no reply is received in the specified amount of time the request is aborted
  27. /// with an ``RPCError`` with code ``RPCError/Code/deadlineExceeded``.
  28. ///
  29. /// The actual deadline used will be the minimum of the value specified here
  30. /// and the value set by the application by the client API. If either one isn't set
  31. /// then the other value is used. If neither is set then the request has no deadline.
  32. ///
  33. /// The timeout applies to the overall execution of an RPC. If, for example, a retry
  34. /// policy is set then the timeout begins when the first attempt is started and _isn't_ reset
  35. /// when subsequent attempts start.
  36. public var timeout: Duration?
  37. /// Whether RPCs for this method should wait until the connection is ready.
  38. ///
  39. /// If `false` the RPC will abort immediately if there is a transient failure connecting to
  40. /// the server. Otherwise gRPC will attempt to connect until the deadline is exceeded.
  41. public var waitForReady: Bool?
  42. /// The maximum allowed payload size in bytes for an individual request message.
  43. ///
  44. /// If a client attempts to send an object larger than this value, it will not be sent and the
  45. /// client will see an error. Note that 0 is a valid value, meaning that the request message
  46. /// must be empty.
  47. ///
  48. /// Note that if compression is used the uncompressed message size is validated.
  49. public var maxRequestMessageBytes: Int?
  50. /// The maximum allowed payload size in bytes for an individual response message.
  51. ///
  52. /// If a server attempts to send an object larger than this value, it will not
  53. /// be sent, and an error will be sent to the client instead. Note that 0 is a valid value,
  54. /// meaning that the response message must be empty.
  55. ///
  56. /// Note that if compression is used the uncompressed message size is validated.
  57. public var maxResponseMessageBytes: Int?
  58. /// The policy determining how many times, and when, the RPC is executed.
  59. ///
  60. /// There are two policy types:
  61. /// 1. Retry
  62. /// 2. Hedging
  63. ///
  64. /// The retry policy allows an RPC to be retried a limited number of times if the RPC
  65. /// fails with one of the configured set of status codes. RPCs are only retried if they
  66. /// fail immediately, that is, the first response part received from the server is a
  67. /// status code.
  68. ///
  69. /// The hedging policy allows an RPC to be executed multiple times concurrently. Typically
  70. /// each execution will be staggered by some delay. The first successful response will be
  71. /// reported to the client. Hedging is only suitable for idempotent RPCs.
  72. public var executionPolicy: RPCExecutionPolicy?
  73. /// The compression used for the call.
  74. ///
  75. /// Compression in gRPC is asymmetrical: the server may compress response messages using a
  76. /// different algorithm than the client used to compress request messages. This configuration
  77. /// controls the compression used by the client for request messages.
  78. ///
  79. /// Note that this configuration is advisory: not all transports support compression and may
  80. /// ignore this configuration. Transports which support compression will use this configuration
  81. /// in preference to the algorithm configured at a transport level. If the transport hasn't
  82. /// enabled the use of the algorithm then compression won't be used for the call.
  83. ///
  84. /// If `nil` the value configured on the transport will be used instead.
  85. public var compression: CompressionAlgorithm?
  86. internal init(
  87. timeout: Duration?,
  88. waitForReady: Bool?,
  89. maxRequestMessageBytes: Int?,
  90. maxResponseMessageBytes: Int?,
  91. executionPolicy: RPCExecutionPolicy?,
  92. compression: CompressionAlgorithm?
  93. ) {
  94. self.timeout = timeout
  95. self.waitForReady = waitForReady
  96. self.maxRequestMessageBytes = maxRequestMessageBytes
  97. self.maxResponseMessageBytes = maxResponseMessageBytes
  98. self.executionPolicy = executionPolicy
  99. self.compression = compression
  100. }
  101. }
  102. extension CallOptions {
  103. /// Default call options.
  104. ///
  105. /// The default values (`nil`) defer values to the underlying transport.
  106. public static var defaults: Self {
  107. Self(
  108. timeout: nil,
  109. waitForReady: nil,
  110. maxRequestMessageBytes: nil,
  111. maxResponseMessageBytes: nil,
  112. executionPolicy: nil,
  113. compression: nil
  114. )
  115. }
  116. }
  117. extension CallOptions {
  118. package mutating func formUnion(with methodConfig: MethodConfig?) {
  119. guard let methodConfig = methodConfig else { return }
  120. self.timeout.setIfNone(to: methodConfig.timeout)
  121. self.waitForReady.setIfNone(to: methodConfig.waitForReady)
  122. self.maxRequestMessageBytes.setIfNone(to: methodConfig.maxRequestMessageBytes)
  123. self.maxResponseMessageBytes.setIfNone(to: methodConfig.maxResponseMessageBytes)
  124. self.executionPolicy.setIfNone(to: methodConfig.executionPolicy)
  125. }
  126. }
  127. extension Optional {
  128. fileprivate mutating func setIfNone(to value: Self) {
  129. switch self {
  130. case .some:
  131. ()
  132. case .none:
  133. self = value
  134. }
  135. }
  136. }