Skip to content

Commit

Permalink
CI // Add 0.2s time span per each online image download.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed Oct 22, 2024
1 parent bec4684 commit 03dc2ed
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Script/GI_RawAssetsPuller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ let dataDict = try await withThrowingTaskGroup(
) { taskGroup in
urlDict.forEach { fileNameStem, urlString in
taskGroup.addTask {
await URLAsyncTaskStack.waitFor200ms()
let (data, _) = try await URLSession.shared.data(from: URL(string: urlString)!)
return (fileNameStem, data)
}
Expand Down Expand Up @@ -520,3 +521,51 @@ do {
assertionFailure(error.localizedDescription)
exit(1)
}

// MARK: - URLAsyncTaskStack

private actor URLAsyncTaskStack {
// MARK: Public

public static func waitFor200ms() async {
await Self.taskBuffer.addTask {
try await Task.sleep(nanoseconds: 200_000_000) // 300ms sleep
}
}

// MARK: Internal

func addTask(_ task: @escaping () async throws -> Void) async {
// Add the task to the queue and await its execution in sequence
tasks.append(task)

// If this is the only task, start processing the queue
if tasks.count == 1 {
await processNextTask()
}
}

func cancelAllTasks() {
tasks.removeAll()
}

// MARK: Fileprivate

fileprivate static let taskBuffer: URLAsyncTaskStack = .init()

// MARK: Private

private var tasks: [() async throws -> Void] = []

private func processNextTask() async {
while !tasks.isEmpty {
let currentTask = tasks.removeFirst()
do {
// Execute the current task
try await currentTask()
} catch let error as NSError {
print("Task failed with error: \(error)")
}
}
}
}
49 changes: 49 additions & 0 deletions Script/HSR_RawAssetsPuller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ let dataDict = try await withThrowingTaskGroup(
) { taskGroup in
urlDict.forEach { fileNameStem, urlString in
taskGroup.addTask {
await URLAsyncTaskStack.waitFor200ms()
let (data, _) = try await URLSession.shared.data(from: URL(string: urlString)!)
return (fileNameStem, data)
}
Expand Down Expand Up @@ -378,3 +379,51 @@ do {
assertionFailure(error.localizedDescription)
exit(1)
}

// MARK: - URLAsyncTaskStack

private actor URLAsyncTaskStack {
// MARK: Public

public static func waitFor200ms() async {
await Self.taskBuffer.addTask {
try await Task.sleep(nanoseconds: 200_000_000) // 300ms sleep
}
}

// MARK: Internal

func addTask(_ task: @escaping () async throws -> Void) async {
// Add the task to the queue and await its execution in sequence
tasks.append(task)

// If this is the only task, start processing the queue
if tasks.count == 1 {
await processNextTask()
}
}

func cancelAllTasks() {
tasks.removeAll()
}

// MARK: Fileprivate

fileprivate static let taskBuffer: URLAsyncTaskStack = .init()

// MARK: Private

private var tasks: [() async throws -> Void] = []

private func processNextTask() async {
while !tasks.isEmpty {
let currentTask = tasks.removeFirst()
do {
// Execute the current task
try await currentTask()
} catch let error as NSError {
print("Task failed with error: \(error)")
}
}
}
}

0 comments on commit 03dc2ed

Please sign in to comment.