Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit concurrent HTTP requests when downloading binary dependencies #4017

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2270,23 +2270,25 @@ extension Workspace {
}
}

// download max n files concurrently
let semaphore = DispatchSemaphore(value: Concurrency.maxOperations)

// finally download zip files, if any
for artifact in (zipArtifacts.map{ $0 }) {
group.enter()
defer { group.leave() }

let parentDirectory = self.location.artifactsDirectory.appending(component: artifact.packageRef.identity.description)
guard observabilityScope.trap ({ try fileSystem.createDirectory(parentDirectory, recursive: true) }) else {
continue
}

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

semaphore.wait()
group.enter()
var headers = HTTPClientHeaders()
headers.add(name: "Accept", value: "application/octet-stream")
var request = HTTPClient.Request.download(url: artifact.url, headers: headers, fileSystem: self.fileSystem, destination: archivePath)
request.options.authorizationProvider = self.authorizationProvider?.httpAuthorizationHeader(for:)
request.options.retryStrategy = .exponentialBackoff(maxAttempts: 3, baseDelay: .milliseconds(50))
request.options.validResponseCodes = [200]
self.httpClient.execute(
request,
Expand All @@ -2297,7 +2299,10 @@ extension Workspace {
totalBytesToDownload: totalBytesToDownload)
},
completion: { downloadResult in
defer { group.leave() }
defer {
group.leave()
semaphore.signal()
}

switch downloadResult {
case .success:
Expand Down