MessageEncoding.swift 4.2 KB

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