MessageEncoding.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. extension CallOptions {
  40. public struct MessageEncoding {
  41. public init(
  42. forRequests outbound: CompressionAlgorithm?,
  43. acceptableForResponses inbound: [CompressionAlgorithm] = CompressionAlgorithm.all
  44. ) {
  45. self.outbound = outbound
  46. self.inbound = inbound
  47. }
  48. /// The compression algorithm used for outbound messages.
  49. public var outbound: CompressionAlgorithm?
  50. /// The set of compression algorithms advertised to the remote peer that they may use.
  51. public var inbound: [CompressionAlgorithm]
  52. /// No compression.
  53. public static let none = MessageEncoding(
  54. forRequests: nil,
  55. acceptableForResponses: []
  56. )
  57. /// Accept all supported compression on responses, do not compress requests.
  58. public static let responsesOnly = MessageEncoding(
  59. forRequests: .identity,
  60. acceptableForResponses: CompressionAlgorithm.all
  61. )
  62. /// Whether compression is enabled for requests.
  63. internal var enabledForRequests: Bool {
  64. return self.outbound != nil
  65. }
  66. }
  67. }
  68. extension CallOptions.MessageEncoding {
  69. var acceptEncodingHeader: String {
  70. return self.inbound.map { $0.name }.joined(separator: ",")
  71. }
  72. }
  73. extension Server.Configuration {
  74. public struct MessageEncoding {
  75. /// The set of compression algorithms advertised that we will accept from clients. Note that
  76. /// clients may send us messages compressed with algorithms not included in this list; if we
  77. /// support it then we still accept the message.
  78. public var enabled: [CompressionAlgorithm]
  79. public init(enabled: [CompressionAlgorithm]) {
  80. self.enabled = enabled
  81. }
  82. // All supported algorithms are enabled.
  83. public static let enabled = MessageEncoding(enabled: CompressionAlgorithm.all)
  84. /// No compression.
  85. public static let none = MessageEncoding(enabled: [.identity])
  86. }
  87. }