14
14
* limitations under the License.
15
15
*/
16
16
17
-
18
17
/// Whether compression should be enabled for the message.
19
18
public enum Compression {
20
19
/// Enable compression. Note that this will be ignored if compression has not been enabled or is
@@ -42,14 +41,32 @@ extension Compression {
42
41
}
43
42
}
44
43
45
- extension CallOptions {
46
- public struct MessageEncoding {
44
+ public enum ClientMessageEncoding {
45
+ case enabled( Configuration )
46
+ case disabled
47
+ }
48
+
49
+ extension ClientMessageEncoding {
50
+ var enabledForRequests : Bool {
51
+ switch self {
52
+ case . enabled( let configuration) :
53
+ return configuration. outbound != nil
54
+ case . disabled:
55
+ return false
56
+ }
57
+ }
58
+ }
59
+
60
+ extension ClientMessageEncoding {
61
+ public struct Configuration {
47
62
public init (
48
63
forRequests outbound: CompressionAlgorithm ? ,
49
- acceptableForResponses inbound: [ CompressionAlgorithm ] = CompressionAlgorithm . all
64
+ acceptableForResponses inbound: [ CompressionAlgorithm ] = CompressionAlgorithm . all,
65
+ decompressionLimit: DecompressionLimit
50
66
) {
51
67
self . outbound = outbound
52
68
self . inbound = inbound
69
+ self . decompressionLimit = decompressionLimit
53
70
}
54
71
55
72
/// The compression algorithm used for outbound messages.
@@ -58,47 +75,57 @@ extension CallOptions {
58
75
/// The set of compression algorithms advertised to the remote peer that they may use.
59
76
public var inbound : [ CompressionAlgorithm ]
60
77
61
- /// No compression.
62
- public static let none = MessageEncoding (
63
- forRequests: nil ,
64
- acceptableForResponses: [ ]
65
- )
78
+ /// The decompression limit acceptable for responses. RPCs which receive a message whose
79
+ /// decompressed size exceeds the limit will be cancelled.
80
+ public var decompressionLimit : DecompressionLimit
66
81
67
82
/// Accept all supported compression on responses, do not compress requests.
68
- public static let responsesOnly = MessageEncoding (
69
- forRequests: . identity,
70
- acceptableForResponses: CompressionAlgorithm . all
71
- )
72
-
73
- /// Whether compression is enabled for requests.
74
- internal var enabledForRequests : Bool {
75
- return self . outbound != nil
83
+ public static func responsesOnly(
84
+ acceptable: [ CompressionAlgorithm ] = CompressionAlgorithm . all,
85
+ decompressionLimit: DecompressionLimit
86
+ ) -> Configuration {
87
+ return Configuration (
88
+ forRequests: . identity,
89
+ acceptableForResponses: acceptable,
90
+ decompressionLimit: decompressionLimit
91
+ )
76
92
}
77
- }
78
- }
79
93
80
- extension CallOptions . MessageEncoding {
81
- var acceptEncodingHeader : String {
82
- return self . inbound . map { $0 . name } . joined ( separator : " , " )
94
+ internal var acceptEncodingHeader : String {
95
+ return self . inbound . map { $0 . name } . joined ( separator : " , " )
96
+ }
83
97
}
84
98
}
85
99
86
- extension Server . Configuration {
87
- public struct MessageEncoding {
88
- /// The set of compression algorithms advertised that we will accept from clients. Note that
89
- /// clients may send us messages compressed with algorithms not included in this list; if we
90
- /// support it then we still accept the message.
91
- public var enabled : [ CompressionAlgorithm ]
100
+ public enum ServerMessageEncoding {
101
+ case enabled( Configuration )
102
+ case disabled
103
+ }
92
104
93
- public init ( enabled: [ CompressionAlgorithm ] ) {
94
- self . enabled = enabled
105
+ extension ServerMessageEncoding {
106
+ public struct Configuration {
107
+ /// The set of compression algorithms advertised that we will accept from clients for requests.
108
+ /// Note that clients may send us messages compressed with algorithms not included in this list;
109
+ /// if we support it then we still accept the message.
110
+ ///
111
+ /// All cases of `CompressionAlgorithm` are supported.
112
+ public var enabledAlgorithms : [ CompressionAlgorithm ]
113
+
114
+ /// The decompression limit acceptable for requests. RPCs which receive a message whose
115
+ /// decompressed size exceeds the limit will be cancelled.
116
+ public var decompressionLimit : DecompressionLimit
117
+
118
+ /// Create a configuration for server message encoding.
119
+ ///
120
+ /// - Parameters:
121
+ /// - enabledAlgorithms: The list of algorithms which are enabled.
122
+ /// - decompressionLimit: Decompression limit acceptable for requests.
123
+ public init (
124
+ enabledAlgorithms: [ CompressionAlgorithm ] = CompressionAlgorithm . all,
125
+ decompressionLimit: DecompressionLimit
126
+ ) {
127
+ self . enabledAlgorithms = enabledAlgorithms
128
+ self . decompressionLimit = decompressionLimit
95
129
}
96
-
97
- // All supported algorithms are enabled.
98
- public static let enabled = MessageEncoding ( enabled: CompressionAlgorithm . all)
99
-
100
- /// No compression.
101
- public static let none = MessageEncoding ( enabled: [ . identity] )
102
130
}
103
-
104
131
}
0 commit comments