CallOptions.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /// Whether compression is enabled or not for request and response messages.
  75. public var compression: Compression
  76. public struct Compression: Sendable {
  77. /// Whether request messages should be compressed.
  78. ///
  79. /// Note that this option is _advisory_: transports are not required to support compression.
  80. public var requests: Bool
  81. /// Whether response messages are permitted to be compressed.
  82. public var responses: Bool
  83. /// Creates a new ``Compression`` configuration.
  84. ///
  85. /// - Parameters:
  86. /// - requests: Whether request messages should be compressed.
  87. /// - responses: Whether response messages may be compressed.
  88. public init(requests: Bool, responses: Bool) {
  89. self.requests = requests
  90. self.responses = responses
  91. }
  92. /// Sets ``requests`` and ``responses`` to `true`.
  93. public static var enabled: Self {
  94. Self(requests: true, responses: true)
  95. }
  96. /// Sets ``requests`` and ``responses`` to `false`.
  97. public static var disabled: Self {
  98. Self(requests: false, responses: false)
  99. }
  100. }
  101. internal init(
  102. timeout: Duration?,
  103. waitForReady: Bool?,
  104. maxRequestMessageBytes: Int?,
  105. maxResponseMessageBytes: Int?,
  106. executionPolicy: RPCExecutionPolicy?,
  107. compression: Compression
  108. ) {
  109. self.timeout = timeout
  110. self.waitForReady = waitForReady
  111. self.maxRequestMessageBytes = maxRequestMessageBytes
  112. self.maxResponseMessageBytes = maxResponseMessageBytes
  113. self.executionPolicy = executionPolicy
  114. self.compression = compression
  115. }
  116. }
  117. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  118. extension CallOptions {
  119. /// Default call options.
  120. ///
  121. /// The default values defer values to the underlying transport. In most cases this means values
  122. /// are `nil`, with the exception of ``compression-swift.property`` which is set
  123. /// to ``Compression-swift.struct/disabled``.
  124. public static var defaults: Self {
  125. Self(
  126. timeout: nil,
  127. waitForReady: nil,
  128. maxRequestMessageBytes: nil,
  129. maxResponseMessageBytes: nil,
  130. executionPolicy: nil,
  131. compression: .disabled
  132. )
  133. }
  134. }
  135. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  136. extension CallOptions {
  137. mutating func formUnion(with methodConfig: MethodConfig?) {
  138. guard let methodConfig = methodConfig else { return }
  139. self.timeout.setIfNone(to: methodConfig.timeout)
  140. self.waitForReady.setIfNone(to: methodConfig.waitForReady)
  141. self.maxRequestMessageBytes.setIfNone(to: methodConfig.maxRequestMessageBytes)
  142. self.maxResponseMessageBytes.setIfNone(to: methodConfig.maxResponseMessageBytes)
  143. self.executionPolicy.setIfNone(to: methodConfig.executionPolicy)
  144. }
  145. }
  146. extension Optional {
  147. fileprivate mutating func setIfNone(to value: Self) {
  148. switch self {
  149. case .some:
  150. ()
  151. case .none:
  152. self = value
  153. }
  154. }
  155. }