MessageEncoding.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2020, 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. /// Whether compression should be enabled for the message.
  17. public struct Compression: Hashable {
  18. @usableFromInline
  19. internal enum _Wrapped: Hashable {
  20. case enabled
  21. case disabled
  22. case deferToCallDefault
  23. }
  24. @usableFromInline
  25. internal var _wrapped: _Wrapped
  26. private init(_ wrapped: _Wrapped) {
  27. self._wrapped = wrapped
  28. }
  29. /// Enable compression. Note that this will be ignored if compression has not been enabled or is
  30. /// not supported on the call.
  31. public static let enabled = Compression(.enabled)
  32. /// Disable compression.
  33. public static let disabled = Compression(.disabled)
  34. /// Defer to the call (the `CallOptions` for the client, and the context for the server) to
  35. /// determine whether compression should be used for the message.
  36. public static let deferToCallDefault = Compression(.deferToCallDefault)
  37. }
  38. extension Compression {
  39. @inlinable
  40. internal func isEnabled(callDefault: Bool) -> Bool {
  41. switch self._wrapped {
  42. case .enabled:
  43. return callDefault
  44. case .disabled:
  45. return false
  46. case .deferToCallDefault:
  47. return callDefault
  48. }
  49. }
  50. }
  51. /// Whether compression is enabled or disabled for a client.
  52. public enum ClientMessageEncoding {
  53. /// Compression is enabled with the given configuration.
  54. case enabled(Configuration)
  55. /// Compression is disabled.
  56. case disabled
  57. }
  58. extension ClientMessageEncoding {
  59. internal var enabledForRequests: Bool {
  60. switch self {
  61. case let .enabled(configuration):
  62. return configuration.outbound != nil
  63. case .disabled:
  64. return false
  65. }
  66. }
  67. }
  68. extension ClientMessageEncoding {
  69. public struct Configuration {
  70. public init(
  71. forRequests outbound: CompressionAlgorithm?,
  72. acceptableForResponses inbound: [CompressionAlgorithm] = CompressionAlgorithm.all,
  73. decompressionLimit: DecompressionLimit
  74. ) {
  75. self.outbound = outbound
  76. self.inbound = inbound
  77. self.decompressionLimit = decompressionLimit
  78. }
  79. /// The compression algorithm used for outbound messages.
  80. public var outbound: CompressionAlgorithm?
  81. /// The set of compression algorithms advertised to the remote peer that they may use.
  82. public var inbound: [CompressionAlgorithm]
  83. /// The decompression limit acceptable for responses. RPCs which receive a message whose
  84. /// decompressed size exceeds the limit will be cancelled.
  85. public var decompressionLimit: DecompressionLimit
  86. /// Accept all supported compression on responses, do not compress requests.
  87. public static func responsesOnly(
  88. acceptable: [CompressionAlgorithm] = CompressionAlgorithm.all,
  89. decompressionLimit: DecompressionLimit
  90. ) -> Configuration {
  91. return Configuration(
  92. forRequests: .identity,
  93. acceptableForResponses: acceptable,
  94. decompressionLimit: decompressionLimit
  95. )
  96. }
  97. internal var acceptEncodingHeader: String {
  98. return self.inbound.map { $0.name }.joined(separator: ",")
  99. }
  100. }
  101. }
  102. /// Whether compression is enabled or disabled on the server.
  103. public enum ServerMessageEncoding {
  104. /// Compression is supported with this configuration.
  105. case enabled(Configuration)
  106. /// Compression is not enabled. However, 'identity' compression is still supported.
  107. case disabled
  108. @usableFromInline
  109. internal var isEnabled: Bool {
  110. switch self {
  111. case .enabled:
  112. return true
  113. case .disabled:
  114. return false
  115. }
  116. }
  117. }
  118. extension ServerMessageEncoding {
  119. public struct Configuration {
  120. /// The set of compression algorithms advertised that we will accept from clients for requests.
  121. /// Note that clients may send us messages compressed with algorithms not included in this list;
  122. /// if we support it then we still accept the message.
  123. ///
  124. /// All cases of `CompressionAlgorithm` are supported.
  125. public var enabledAlgorithms: [CompressionAlgorithm]
  126. /// The decompression limit acceptable for requests. RPCs which receive a message whose
  127. /// decompressed size exceeds the limit will be cancelled.
  128. public var decompressionLimit: DecompressionLimit
  129. /// Create a configuration for server message encoding.
  130. ///
  131. /// - Parameters:
  132. /// - enabledAlgorithms: The list of algorithms which are enabled.
  133. /// - decompressionLimit: Decompression limit acceptable for requests.
  134. public init(
  135. enabledAlgorithms: [CompressionAlgorithm] = CompressionAlgorithm.all,
  136. decompressionLimit: DecompressionLimit
  137. ) {
  138. self.enabledAlgorithms = enabledAlgorithms
  139. self.decompressionLimit = decompressionLimit
  140. }
  141. }
  142. }