Skip to content

Commit d068837

Browse files
authored
Limit concurrent HTTP requests when downloading binary dependencies (#4017) (#4024)
* Limit concurrent download of binary dependencies * Add retryStrategy for downloading binary dependencies
1 parent 5d59d29 commit d068837

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
@@ -2276,23 +2276,25 @@ extension Workspace {
22762276
}
22772277
}
22782278

2279+
// download max n files concurrently
2280+
let semaphore = DispatchSemaphore(value: Concurrency.maxOperations)
2281+
22792282
// finally download zip files, if any
22802283
for artifact in (zipArtifacts.map{ $0 }) {
2281-
group.enter()
2282-
defer { group.leave() }
2283-
22842284
let parentDirectory = self.location.artifactsDirectory.appending(component: artifact.packageRef.identity.description)
22852285
guard observabilityScope.trap ({ try fileSystem.createDirectory(parentDirectory, recursive: true) }) else {
22862286
continue
22872287
}
22882288

22892289
let archivePath = parentDirectory.appending(component: artifact.url.lastPathComponent)
22902290

2291+
semaphore.wait()
22912292
group.enter()
22922293
var headers = HTTPClientHeaders()
22932294
headers.add(name: "Accept", value: "application/octet-stream")
22942295
var request = HTTPClient.Request.download(url: artifact.url, headers: headers, fileSystem: self.fileSystem, destination: archivePath)
22952296
request.options.authorizationProvider = self.authorizationProvider?.httpAuthorizationHeader(for:)
2297+
request.options.retryStrategy = .exponentialBackoff(maxAttempts: 3, baseDelay: .milliseconds(50))
22962298
request.options.validResponseCodes = [200]
22972299
self.httpClient.execute(
22982300
request,
@@ -2303,7 +2305,10 @@ extension Workspace {
23032305
totalBytesToDownload: totalBytesToDownload)
23042306
},
23052307
completion: { downloadResult in
2306-
defer { group.leave() }
2308+
defer {
2309+
group.leave()
2310+
semaphore.signal()
2311+
}
23072312

23082313
switch downloadResult {
23092314
case .success:

0 commit comments

Comments
 (0)