MessageEncoding.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. public enum ClientMessageEncoding {
  52. case enabled(Configuration)
  53. case disabled
  54. }
  55. extension ClientMessageEncoding {
  56. var enabledForRequests: Bool {
  57. switch self {
  58. case let .enabled(configuration):
  59. return configuration.outbound != nil
  60. case .disabled:
  61. return false
  62. }
  63. }
  64. }
  65. extension ClientMessageEncoding {
  66. public struct Configuration {
  67. public init(
  68. forRequests outbound: CompressionAlgorithm?,
  69. acceptableForResponses inbound: [CompressionAlgorithm] = CompressionAlgorithm.all,
  70. decompressionLimit: DecompressionLimit
  71. ) {
  72. self.outbound = outbound
  73. self.inbound = inbound
  74. self.decompressionLimit = decompressionLimit
  75. }
  76. /// The compression algorithm used for outbound messages.
  77. public var outbound: CompressionAlgorithm?
  78. /// The set of compression algorithms advertised to the remote peer that they may use.
  79. public var inbound: [CompressionAlgorithm]
  80. /// The decompression limit acceptable for responses. RPCs which receive a message whose
  81. /// decompressed size exceeds the limit will be cancelled.
  82. public var decompressionLimit: DecompressionLimit
  83. /// Accept all supported compression on responses, do not compress requests.
  84. public static func responsesOnly(
  85. acceptable: [CompressionAlgorithm] = CompressionAlgorithm.all,
  86. decompressionLimit: DecompressionLimit
  87. ) -> Configuration {
  88. return Configuration(
  89. forRequests: .identity,
  90. acceptableForResponses: acceptable,
  91. decompressionLimit: decompressionLimit
  92. )
  93. }
  94. internal var acceptEncodingHeader: String {
  95. return self.inbound.map { $0.name }.joined(separator: ",")
  96. }
  97. }
  98. }
  99. public enum ServerMessageEncoding {
  100. case enabled(Configuration)
  101. case disabled
  102. @usableFromInline
  103. internal var isEnabled: Bool {
  104. switch self {
  105. case .enabled:
  106. return true
  107. case .disabled:
  108. return false
  109. }
  110. }
  111. }
  112. extension ServerMessageEncoding {
  113. public struct Configuration {
  114. /// The set of compression algorithms advertised that we will accept from clients for requests.
  115. /// Note that clients may send us messages compressed with algorithms not included in this list;
  116. /// if we support it then we still accept the message.
  117. ///
  118. /// All cases of `CompressionAlgorithm` are supported.
  119. public var enabledAlgorithms: [CompressionAlgorithm]
  120. /// The decompression limit acceptable for requests. RPCs which receive a message whose
  121. /// decompressed size exceeds the limit will be cancelled.
  122. public var decompressionLimit: DecompressionLimit
  123. /// Create a configuration for server message encoding.
  124. ///
  125. /// - Parameters:
  126. /// - enabledAlgorithms: The list of algorithms which are enabled.
  127. /// - decompressionLimit: Decompression limit acceptable for requests.
  128. public init(
  129. enabledAlgorithms: [CompressionAlgorithm] = CompressionAlgorithm.all,
  130. decompressionLimit: DecompressionLimit
  131. ) {
  132. self.enabledAlgorithms = enabledAlgorithms
  133. self.decompressionLimit = decompressionLimit
  134. }
  135. }
  136. }