From 02e717651c88c48afeb66ef85f1f868ba9b08dfb Mon Sep 17 00:00:00 2001 From: Ilya Kharabet Date: Tue, 2 Mar 2021 12:26:23 +0300 Subject: [PATCH] Make file download requests chunked (#75) --- .../FigmaExport/Loaders/ImagesLoader.swift | 10 ---------- .../FigmaExport/Output/FileDownloader.swift | 20 ++++++++++++------- .../Extensions/Array+chunked.swift | 9 +++++++++ 3 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 Sources/FigmaExportCore/Extensions/Array+chunked.swift diff --git a/Sources/FigmaExport/Loaders/ImagesLoader.swift b/Sources/FigmaExport/Loaders/ImagesLoader.swift index 3b18a27c..60e07313 100644 --- a/Sources/FigmaExport/Loaders/ImagesLoader.swift +++ b/Sources/FigmaExport/Loaders/ImagesLoader.swift @@ -214,13 +214,3 @@ private extension String { } } - -// MARK: - Array Utils - -private extension Array { - func chunked(into size: Int) -> [[Element]] { - stride(from: 0, to: count, by: size).map { - Array(self[$0 ..< Swift.min($0 + size, count)]) - } - } -} diff --git a/Sources/FigmaExport/Output/FileDownloader.swift b/Sources/FigmaExport/Output/FileDownloader.swift index 60720765..07827042 100644 --- a/Sources/FigmaExport/Output/FileDownloader.swift +++ b/Sources/FigmaExport/Output/FileDownloader.swift @@ -14,22 +14,26 @@ final class FileDownloader { func fetch(files: [FileContents]) throws -> [FileContents] { let group = DispatchGroup() var errors: [Error] = [] - + var newFiles = [FileContents]() let remoteFileCount = files.filter { $0.sourceURL != nil }.count var downloaded = 0 + let semaphore = DispatchSemaphore(value: session.configuration.httpMaximumConnectionsPerHost - 1) + files.forEach { file in guard let remoteURL = file.sourceURL else { newFiles.append(file) return } - group.enter() let task = session.downloadTask(with: remoteURL) { localURL, _, error in - defer { group.leave() } - + defer { + semaphore.signal() + group.leave() + } + guard let fileURL = localURL, error == nil else { errors.append(error!) return @@ -44,15 +48,17 @@ final class FileDownloader { downloaded += 1 self.logger.info("Downloaded \(downloaded)/\(remoteFileCount)") } - + + group.enter() + semaphore.wait() task.resume() } group.wait() - + if !errors.isEmpty { throw ErrorGroup(all: errors) } - + return newFiles } } diff --git a/Sources/FigmaExportCore/Extensions/Array+chunked.swift b/Sources/FigmaExportCore/Extensions/Array+chunked.swift new file mode 100644 index 00000000..017409bf --- /dev/null +++ b/Sources/FigmaExportCore/Extensions/Array+chunked.swift @@ -0,0 +1,9 @@ +import Foundation + +public extension Array { + func chunked(into size: Int) -> [[Element]] { + stride(from: 0, to: count, by: size).map { + Array(self[$0 ..< Swift.min($0 + size, count)]) + } + } +}