MessageEncoding.swift 4.5 KB

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