Skip to content

Commit

Permalink
Make CheckRedirects aync/await
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Jan 12, 2024
1 parent 6d368ba commit 5b00aa3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
44 changes: 23 additions & 21 deletions Sources/ValidatorCore/Commands/CheckRedirects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,34 @@ extension Validator {
}

var normalized = Set(inputURLs.map { $0.normalized() })
let updated = try inputURLs[offset...]
var updated = [PackageURL]()

for (index, packageURL) in inputURLs[offset...]
.prefix(prefix)
.chunk(index: chunk, of: numberOfChunks)
.enumerated()
.compactMap { (index, packageURL) -> PackageURL? in
let index = index + offset
let redirect = try resolvePackageRedirects(client: httpClient, for: packageURL)
.wait()

if index % 100 == 0, let token = Current.githubToken() {
let rateLimit = try Github.getRateLimit(client: httpClient,
token: token).wait()
if rateLimit.remaining < 200 {
print("Rate limit remaining: \(rateLimit.remaining)")
print("Sleeping until reset at \(rateLimit.resetDate) ...")
sleep(UInt32(rateLimit.secondsUntilReset + 0.5))
}
.enumerated() {
let index = index + offset
let redirect = try await resolvePackageRedirects(client: httpClient, for: packageURL)

if index % 100 == 0, let token = Current.githubToken() {
let rateLimit = try await Github.getRateLimit(client: httpClient, token: token).get()
if rateLimit.remaining < 200 {
print("Rate limit remaining: \(rateLimit.remaining)")
print("Sleeping until reset at \(rateLimit.resetDate) ...")
sleep(UInt32(rateLimit.secondsUntilReset + 0.5))
}
}

return try Self.process(redirect: redirect,
verbose: verbose,
index: index,
packageURL: packageURL,
normalized: &normalized)
if let res = try Self.process(redirect: redirect,
verbose: verbose,
index: index,
packageURL: packageURL,
normalized: &normalized) {
updated.append(res)
}
.sorted(by: { $0.lowercased() < $1.lowercased() })
}

updated.sort(by: { $0.lowercased() < $1.lowercased() })

if let path = output {
try Current.fileManager.saveList(updated, path: path)
Expand Down
18 changes: 8 additions & 10 deletions Sources/ValidatorCore/RedirectFollower.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum Redirect: Equatable {
}



@available(*, deprecated)
func resolveRedirects(for url: PackageURL) async throws -> Redirect {
let client = HTTPClient(eventLoopGroupProvider: .singleton,
configuration: .init(redirectConfiguration: .disallow))
Expand Down Expand Up @@ -118,14 +118,12 @@ private func resolveRedirects(client: HTTPClient, for url: PackageURL) async thr
}


/// Resolve redirects for package urls. In particular, this strips the `.git` extension from the test url, because it would always lead to a redirect. It also normalizes the output to always have a `.git` extension.
/// - Returns: `Redirect`
@available(*, deprecated)
func resolvePackageRedirects(client: HTTPClient, for url: PackageURL) -> EventLoopFuture<Redirect> {
let promise = client.eventLoopGroup.next().makePromise(of: Redirect.self)
promise.completeWithTask {
try await resolveRedirects(client: client, for: url.deletingGitExtension())
func resolvePackageRedirects(client: HTTPClient, for url: PackageURL) async throws -> Redirect {
let res = try await resolveRedirects(client: client, for: url.deletingGitExtension())
switch res {
case .initial, .notFound, .error, .unauthorized, .rateLimited:
return res
case .redirected(to: let newURL):
return .redirected(to: newURL.appendingGitExtension())
}
return promise.futureResult
}

0 comments on commit 5b00aa3

Please sign in to comment.