CallOptions.swift 6.0 KB

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