Skip to content

Commit d41c85b

Browse files
authored
Limit concurrent HTTP requests when downloading binary dependencies (#4017)
* Limit concurrent download of binary dependencies * Add retryStrategy for downloading binary dependencies
1 parent 63d19f3 commit d41c85b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Sources/Workspace/Workspace.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,23 +2270,25 @@ extension Workspace {
22702270
}
22712271
}
22722272

2273+
// download max n files concurrently
2274+
let semaphore = DispatchSemaphore(value: Concurrency.maxOperations)
2275+
22732276
// finally download zip files, if any
22742277
for artifact in (zipArtifacts.map{ $0 }) {
2275-
group.enter()
2276-
defer { group.leave() }
2277-
22782278
let parentDirectory = self.location.artifactsDirectory.appending(component: artifact.packageRef.identity.description)
22792279
guard observabilityScope.trap ({ try fileSystem.createDirectory(parentDirectory, recursive: true) }) else {
22802280
continue
22812281
}
22822282

22832283
let archivePath = parentDirectory.appending(component: artifact.url.lastPathComponent)
22842284

2285+
semaphore.wait()
22852286
group.enter()
22862287
var headers = HTTPClientHeaders()
22872288
headers.add(name: "Accept", value: "application/octet-stream")
22882289
var request = HTTPClient.Request.download(url: artifact.url, headers: headers, fileSystem: self.fileSystem, destination: archivePath)
22892290
request.options.authorizationProvider = self.authorizationProvider?.httpAuthorizationHeader(for:)
2291+
request.options.retryStrategy = .exponentialBackoff(maxAttempts: 3, baseDelay: .milliseconds(50))
22902292
request.options.validResponseCodes = [200]
22912293
self.httpClient.execute(
22922294
request,
@@ -2297,7 +2299,10 @@ extension Workspace {
22972299
totalBytesToDownload: totalBytesToDownload)
22982300
},
22992301
completion: { downloadResult in
2300-
defer { group.leave() }
2302+
defer {
2303+
group.leave()
2304+
semaphore.signal()
2305+
}
23012306

23022307
switch downloadResult {
23032308
case .success:

0 commit comments

Comments
 (0)