Skip to content

Commit ae4131f

Browse files
authored
feat: Add option to enable compression of event payloads (#399)
1 parent 8f3f8d8 commit ae4131f

File tree

7 files changed

+121
-10
lines changed

7 files changed

+121
-10
lines changed

ContractTests/Source/Controllers/SdkController.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ final class SdkController: RouteCollection {
2929
"etag-caching",
3030
"inline-context",
3131
"anonymous-redaction",
32-
"evaluation-hooks"
32+
"evaluation-hooks",
33+
"event-gzip",
34+
"optional-event-gzip"
3335
]
3436

3537
return StatusResponse(
@@ -85,6 +87,10 @@ final class SdkController: RouteCollection {
8587
if let flushIntervalMs = events.flushIntervalMs {
8688
config.eventFlushInterval = flushIntervalMs
8789
}
90+
91+
if let enableCompression = events.enableGzip {
92+
config.enableCompression = enableCompression
93+
}
8894
}
8995

9096
if let tags = createInstance.configuration.tags {

ContractTests/Source/Models/client.swift

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct EventParameters: Content {
3636
var allAttributesPrivate: Bool?
3737
var globalPrivateAttributes: [String]?
3838
var flushIntervalMs: Double?
39+
var enableGzip: Bool?
3940
}
4041

4142
struct TagParameters: Content {

LaunchDarkly.podspec

+1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ Pod::Spec.new do |ld|
3939

4040
ld.subspec 'Core' do |es|
4141
es.dependency 'LDSwiftEventSource', '3.3.0'
42+
es.dependency 'DataCompression', '3.8.0'
4243
end
4344
end

LaunchDarkly.xcodeproj/project.pbxproj

+88-6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@
244244
A35AD4612A619E45005A8DCB /* SystemCapabilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35AD45F2A619E45005A8DCB /* SystemCapabilities.swift */; };
245245
A35AD4622A619E45005A8DCB /* SystemCapabilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35AD45F2A619E45005A8DCB /* SystemCapabilities.swift */; };
246246
A35AD4632A619E45005A8DCB /* SystemCapabilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35AD45F2A619E45005A8DCB /* SystemCapabilities.swift */; };
247+
A3647AA52C5BF6EF00902D0C /* DataCompression in Frameworks */ = {isa = PBXBuildFile; productRef = A3647AA42C5BF6EF00902D0C /* DataCompression */; };
248+
A3647AA92C5BF70300902D0C /* DataCompression in Frameworks */ = {isa = PBXBuildFile; productRef = A3647AA82C5BF70300902D0C /* DataCompression */; };
249+
A3647AAD2C5BF71100902D0C /* DataCompression in Frameworks */ = {isa = PBXBuildFile; productRef = A3647AAC2C5BF71100902D0C /* DataCompression */; };
250+
A3647AB12C5BF72000902D0C /* DataCompression in Frameworks */ = {isa = PBXBuildFile; productRef = A3647AB02C5BF72000902D0C /* DataCompression */; };
247251
A36EDFC82853883400D91B05 /* ObjcLDReference.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36EDFC72853883400D91B05 /* ObjcLDReference.swift */; };
248252
A36EDFC92853883400D91B05 /* ObjcLDReference.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36EDFC72853883400D91B05 /* ObjcLDReference.swift */; };
249253
A36EDFCA2853883400D91B05 /* ObjcLDReference.swift in Sources */ = {isa = PBXBuildFile; fileRef = A36EDFC72853883400D91B05 /* ObjcLDReference.swift */; };
@@ -530,6 +534,7 @@
530534
buildActionMask = 2147483647;
531535
files = (
532536
A30EF4F328C24AB600CD220E /* LDSwiftEventSource in Frameworks */,
537+
A3647AB12C5BF72000902D0C /* DataCompression in Frameworks */,
533538
);
534539
runOnlyForDeploymentPostprocessing = 0;
535540
};
@@ -538,6 +543,7 @@
538543
buildActionMask = 2147483647;
539544
files = (
540545
A30EF4F128C24AAD00CD220E /* LDSwiftEventSource in Frameworks */,
546+
A3647AAD2C5BF71100902D0C /* DataCompression in Frameworks */,
541547
);
542548
runOnlyForDeploymentPostprocessing = 0;
543549
};
@@ -546,6 +552,7 @@
546552
buildActionMask = 2147483647;
547553
files = (
548554
A30EF4ED28C24A9A00CD220E /* LDSwiftEventSource in Frameworks */,
555+
A3647AA52C5BF6EF00902D0C /* DataCompression in Frameworks */,
549556
);
550557
runOnlyForDeploymentPostprocessing = 0;
551558
};
@@ -565,6 +572,7 @@
565572
buildActionMask = 2147483647;
566573
files = (
567574
A30EF4EF28C24AA400CD220E /* LDSwiftEventSource in Frameworks */,
575+
A3647AA92C5BF70300902D0C /* DataCompression in Frameworks */,
568576
);
569577
runOnlyForDeploymentPostprocessing = 0;
570578
};
@@ -981,11 +989,13 @@
981989
buildRules = (
982990
);
983991
dependencies = (
992+
A3647AAF2C5BF71800902D0C /* PBXTargetDependency */,
984993
B445A6FB24C0D237000BAD6D /* PBXTargetDependency */,
985994
);
986995
name = LaunchDarkly_tvOS;
987996
packageProductDependencies = (
988997
A30EF4F228C24AB600CD220E /* LDSwiftEventSource */,
998+
A3647AB02C5BF72000902D0C /* DataCompression */,
989999
);
9901000
productName = Darkly_tvOS;
9911001
productReference = 831188382113A16900D77CB5 /* LaunchDarkly_tvOS.framework */;
@@ -995,7 +1005,7 @@
9951005
isa = PBXNativeTarget;
9961006
buildConfigurationList = 831EF34220655D700001C643 /* Build configuration list for PBXNativeTarget "LaunchDarkly_macOS" */;
9971007
buildPhases = (
998-
830C2AC2207416A5001D645D /* ShellScript */,
1008+
830C2AC2207416A5001D645D /* Run Script */,
9991009
831EF33620655D700001C643 /* Sources */,
10001010
831EF33720655D700001C643 /* Frameworks */,
10011011
831EF33820655D700001C643 /* Headers */,
@@ -1005,11 +1015,13 @@
10051015
buildRules = (
10061016
);
10071017
dependencies = (
1018+
A3647AAB2C5BF70C00902D0C /* PBXTargetDependency */,
10081019
B445A6F924C0D232000BAD6D /* PBXTargetDependency */,
10091020
);
10101021
name = LaunchDarkly_macOS;
10111022
packageProductDependencies = (
10121023
A30EF4F028C24AAD00CD220E /* LDSwiftEventSource */,
1024+
A3647AAC2C5BF71100902D0C /* DataCompression */,
10131025
);
10141026
productName = Darkly_macOS;
10151027
productReference = 831EF33B20655D700001C643 /* LaunchDarkly_macOS.framework */;
@@ -1029,11 +1041,13 @@
10291041
buildRules = (
10301042
);
10311043
dependencies = (
1044+
A3647AA32C5BF6E700902D0C /* PBXTargetDependency */,
10321045
B445A6E424C0D1E3000BAD6D /* PBXTargetDependency */,
10331046
);
10341047
name = LaunchDarkly_iOS;
10351048
packageProductDependencies = (
10361049
A30EF4EC28C24A9A00CD220E /* LDSwiftEventSource */,
1050+
A3647AA42C5BF6EF00902D0C /* DataCompression */,
10371051
);
10381052
productName = Darkly;
10391053
productReference = 8354EFC21F22491C00C05156 /* LaunchDarkly.framework */;
@@ -1078,11 +1092,13 @@
10781092
buildRules = (
10791093
);
10801094
dependencies = (
1095+
A3647AA72C5BF6FF00902D0C /* PBXTargetDependency */,
10811096
B445A6F324C0D21E000BAD6D /* PBXTargetDependency */,
10821097
);
10831098
name = LaunchDarkly_watchOS;
10841099
packageProductDependencies = (
10851100
A30EF4EE28C24AA400CD220E /* LDSwiftEventSource */,
1101+
A3647AA82C5BF70300902D0C /* DataCompression */,
10861102
);
10871103
productName = "Darkly-watchOS";
10881104
productReference = 83D9EC6B2062DBB7004D7FA6 /* LaunchDarkly_watchOS.framework */;
@@ -1100,17 +1116,17 @@
11001116
TargetAttributes = {
11011117
831188372113A16900D77CB5 = {
11021118
CreatedOnToolsVersion = 9.2;
1103-
LastSwiftMigration = 1020;
1119+
LastSwiftMigration = 1540;
11041120
ProvisioningStyle = Automatic;
11051121
};
11061122
831EF33A20655D700001C643 = {
11071123
CreatedOnToolsVersion = 9.2;
1108-
LastSwiftMigration = 1020;
1124+
LastSwiftMigration = 1540;
11091125
ProvisioningStyle = Automatic;
11101126
};
11111127
8354EFC11F22491C00C05156 = {
11121128
CreatedOnToolsVersion = 8.3.3;
1113-
LastSwiftMigration = 1020;
1129+
LastSwiftMigration = 1540;
11141130
ProvisioningStyle = Automatic;
11151131
};
11161132
8354EFCA1F22491C00C05156 = {
@@ -1120,7 +1136,7 @@
11201136
};
11211137
83D9EC6A2062DBB7004D7FA6 = {
11221138
CreatedOnToolsVersion = 9.2;
1123-
LastSwiftMigration = 1020;
1139+
LastSwiftMigration = 1540;
11241140
ProvisioningStyle = Automatic;
11251141
};
11261142
};
@@ -1139,6 +1155,7 @@
11391155
B4903D9924BD61D000F087C4 /* XCRemoteSwiftPackageReference "Nimble" */,
11401156
B4903D9C24BD61EF00F087C4 /* XCRemoteSwiftPackageReference "Quick" */,
11411157
B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */,
1158+
A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */,
11421159
);
11431160
productRefGroup = 8354EFC31F22491C00C05156 /* Products */;
11441161
projectDirPath = "";
@@ -1210,14 +1227,15 @@
12101227
shellPath = /bin/sh;
12111228
shellScript = "# Adds support for Apple Silicon brew directory\nexport PATH=\"$PATH:/opt/homebrew/bin\"\n\nif which mint >/dev/null; then\n /usr/bin/xcrun --sdk macosx mint run realm/SwiftLint\nelse\n echo \"warning: mint not installed, available from https://github.com/yonaskolb/Mint\"\nfi\n";
12121229
};
1213-
830C2AC2207416A5001D645D /* ShellScript */ = {
1230+
830C2AC2207416A5001D645D /* Run Script */ = {
12141231
isa = PBXShellScriptBuildPhase;
12151232
alwaysOutOfDate = 1;
12161233
buildActionMask = 2147483647;
12171234
files = (
12181235
);
12191236
inputPaths = (
12201237
);
1238+
name = "Run Script";
12211239
outputPaths = (
12221240
);
12231241
runOnlyForDeploymentPostprocessing = 0;
@@ -1625,6 +1643,22 @@
16251643
target = 8354EFC11F22491C00C05156 /* LaunchDarkly_iOS */;
16261644
targetProxy = 8354EFCD1F22491C00C05156 /* PBXContainerItemProxy */;
16271645
};
1646+
A3647AA32C5BF6E700902D0C /* PBXTargetDependency */ = {
1647+
isa = PBXTargetDependency;
1648+
productRef = A3647AA22C5BF6E700902D0C /* DataCompression */;
1649+
};
1650+
A3647AA72C5BF6FF00902D0C /* PBXTargetDependency */ = {
1651+
isa = PBXTargetDependency;
1652+
productRef = A3647AA62C5BF6FF00902D0C /* DataCompression */;
1653+
};
1654+
A3647AAB2C5BF70C00902D0C /* PBXTargetDependency */ = {
1655+
isa = PBXTargetDependency;
1656+
productRef = A3647AAA2C5BF70C00902D0C /* DataCompression */;
1657+
};
1658+
A3647AAF2C5BF71800902D0C /* PBXTargetDependency */ = {
1659+
isa = PBXTargetDependency;
1660+
productRef = A3647AAE2C5BF71800902D0C /* DataCompression */;
1661+
};
16281662
B445A6E424C0D1E3000BAD6D /* PBXTargetDependency */ = {
16291663
isa = PBXTargetDependency;
16301664
productRef = B445A6E324C0D1E3000BAD6D /* LDSwiftEventSource */;
@@ -2051,6 +2085,14 @@
20512085
/* End XCConfigurationList section */
20522086

20532087
/* Begin XCRemoteSwiftPackageReference section */
2088+
A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */ = {
2089+
isa = XCRemoteSwiftPackageReference;
2090+
repositoryURL = "https://github.com/mw99/DataCompression";
2091+
requirement = {
2092+
kind = upToNextMajorVersion;
2093+
minimumVersion = 3.8.0;
2094+
};
2095+
};
20542096
B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */ = {
20552097
isa = XCRemoteSwiftPackageReference;
20562098
repositoryURL = "https://github.com/LaunchDarkly/swift-eventsource.git";
@@ -2106,6 +2148,46 @@
21062148
package = B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */;
21072149
productName = LDSwiftEventSource;
21082150
};
2151+
A3647AA22C5BF6E700902D0C /* DataCompression */ = {
2152+
isa = XCSwiftPackageProductDependency;
2153+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2154+
productName = DataCompression;
2155+
};
2156+
A3647AA42C5BF6EF00902D0C /* DataCompression */ = {
2157+
isa = XCSwiftPackageProductDependency;
2158+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2159+
productName = DataCompression;
2160+
};
2161+
A3647AA62C5BF6FF00902D0C /* DataCompression */ = {
2162+
isa = XCSwiftPackageProductDependency;
2163+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2164+
productName = DataCompression;
2165+
};
2166+
A3647AA82C5BF70300902D0C /* DataCompression */ = {
2167+
isa = XCSwiftPackageProductDependency;
2168+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2169+
productName = DataCompression;
2170+
};
2171+
A3647AAA2C5BF70C00902D0C /* DataCompression */ = {
2172+
isa = XCSwiftPackageProductDependency;
2173+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2174+
productName = DataCompression;
2175+
};
2176+
A3647AAC2C5BF71100902D0C /* DataCompression */ = {
2177+
isa = XCSwiftPackageProductDependency;
2178+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2179+
productName = DataCompression;
2180+
};
2181+
A3647AAE2C5BF71800902D0C /* DataCompression */ = {
2182+
isa = XCSwiftPackageProductDependency;
2183+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2184+
productName = DataCompression;
2185+
};
2186+
A3647AB02C5BF72000902D0C /* DataCompression */ = {
2187+
isa = XCSwiftPackageProductDependency;
2188+
package = A3647AA12C5BF6DD00902D0C /* XCRemoteSwiftPackageReference "DataCompression" */;
2189+
productName = DataCompression;
2190+
};
21092191
B445A6E324C0D1E3000BAD6D /* LDSwiftEventSource */ = {
21102192
isa = XCSwiftPackageProductDependency;
21112193
package = B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */;

LaunchDarkly/LaunchDarkly/Models/LDConfig.swift

+8
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ public struct LDConfig {
252252

253253
/// The default logger for the SDK. Can be overridden to provide customization.
254254
static let logger: OSLog = OSLog(subsystem: "com.launchdarkly", category: "ios-client-sdk")
255+
256+
/// The default behavior for event payload compression.
257+
static let enableCompression: Bool = false
255258
}
256259

257260
/// Constants relevant to setting up an `LDConfig`
@@ -408,6 +411,11 @@ public struct LDConfig {
408411
/// Additional headers that should be added to all HTTP requests from SDK components to LaunchDarkly services
409412
public var additionalHeaders: [String: String] = [:]
410413

414+
/// Should the event payload sent to LaunchDarkly use gzip compression. By default this is false to prevent backward breaking compatibility issues with older versions of the relay proxy.
415+
///
416+
/// Customers not using the relay proxy are strongly encouraged to enable this feature to reduce egress bandwidth cost.
417+
public var enableCompression: Bool = Defaults.enableCompression
418+
411419
/* TODO: find a way to make delegates equatable */
412420
/// a closure to allow dynamic changes of headers on connect & reconnect
413421
public var headerDelegate: RequestHeaderTransform?

LaunchDarkly/LaunchDarkly/Networking/DarklyService.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import LDSwiftEventSource
33
import OSLog
4+
import DataCompression
45

56
typealias ServiceResponse = (data: Data?, urlResponse: URLResponse?, error: Error?, etag: String?)
67
typealias ServiceCompletionHandler = (ServiceResponse) -> Void
@@ -204,9 +205,19 @@ final class DarklyService: DarklyServiceProvider {
204205
}
205206

206207
private func doPublish(url: URL, headers: [String: String], body: Data, completion: ServiceCompletionHandler?) {
208+
var headers = headers
209+
210+
var httpBody = body
211+
if config.enableCompression {
212+
if let compressed = body.gzip() {
213+
httpBody = compressed
214+
headers.updateValue("gzip", forKey: "Content-Encoding")
215+
}
216+
}
217+
207218
var request = URLRequest(url: url, ldHeaders: headers, ldConfig: config)
208219
request.httpMethod = URLRequest.HTTPMethods.post
209-
request.httpBody = body
220+
request.httpBody = httpBody
210221

211222
session.dataTask(with: request) { data, response, error in
212223
completion?((data: data, urlResponse: response, error: error, etag: nil))

Package.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ let package = Package(
1919
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", .exact("9.1.0")),
2020
.package(url: "https://github.com/Quick/Quick.git", .exact("4.0.0")),
2121
.package(url: "https://github.com/Quick/Nimble.git", .exact("9.2.1")),
22-
.package(name: "LDSwiftEventSource", url: "https://github.com/LaunchDarkly/swift-eventsource.git", .exact("3.3.0"))
22+
.package(name: "LDSwiftEventSource", url: "https://github.com/LaunchDarkly/swift-eventsource.git", .exact("3.3.0")),
23+
.package(name: "DataCompression", url: "https://github.com/mw99/DataCompression", .exact("3.8.0"))
2324
],
2425
targets: [
2526
.target(
2627
name: "LaunchDarkly",
2728
dependencies: [
28-
.product(name: "LDSwiftEventSource", package: "LDSwiftEventSource")
29+
.product(name: "LDSwiftEventSource", package: "LDSwiftEventSource"),
30+
.product(name: "DataCompression", package: "DataCompression")
2931
],
3032
path: "LaunchDarkly/LaunchDarkly",
3133
exclude: ["Support"],

0 commit comments

Comments
 (0)