Skip to content

Commit

Permalink
timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuai1415 authored and huiguangjun committed May 29, 2023
1 parent 33c65f4 commit b683873
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion OSSSwiftDemo/OSSSwiftDemo/Classes/OSSRootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,35 @@ class OSSRootViewController: UIViewController, URLSessionDelegate, URLSessionDat
print("bytesSent:\(bytesSent),totalBytesSent:\(totalBytesSent),totalBytesExpectedToSend:\(totalBytesExpectedToSend)");
};

let provider = OSSAuthCredentialProvider(authServerUrl: OSS_STSTOKEN_URL)
let provider = OSSFederationTokenCredentialProvider {
let tcs = TaskCompletionSource()
DispatchQueue(label: "test").async {
Thread.sleep(forTimeInterval: 10)
let token = OSSFederationToken()
token.tAccessKey = "STS.tAccessKey"
token.tSecretKey = "tSecretKey"
token.tToken = "tToken"
token.expirationTimeInGMTFormat = "2023-05-23T07:34:27Z"
// or tcs.trySetError(<#T##error: Error##Error#>)
tcs.trySetResult(token)
}
tcs.wait(timeout: 5)
if let error = tcs.task.error {
let nsError = error as NSError
if nsError.code == OSSClientErrorCODE.codeNotKnown.rawValue,
let errorMessage = nsError.userInfo[OSSErrorMessageTOKEN] as? String,
errorMessage == "TaskCompletionSource wait timeout." {
// 超时错误
}
throw error
} else if let result = tcs.task.result as? OSSFederationToken {
return result
}
throw NSError(domain: OSSClientErrorDomain,
code: OSSClientErrorCODE.codeSignFailed.rawValue,
userInfo: [OSSErrorMessageTOKEN : "Can not get FederationToken."])
}

let client = OSSClient(endpoint: OSS_ENDPOINT, credentialProvider: provider)
let task = client.putObject(request)
task.continue({ (t) -> Any? in
Expand Down Expand Up @@ -485,3 +513,62 @@ class OSSRootViewController: UIViewController, URLSessionDelegate, URLSessionDat
}
}

public class OSSFederationTokenCredentialProvider: OSSFederationCredentialProvider {
var token: OSSFederationToken?
private var tokenGetter: () throws -> OSSFederationToken

public init(tokenGetter: @escaping () throws -> OSSFederationToken) {
self.tokenGetter = tokenGetter
super.init()
}

public override func getToken() throws -> OSSFederationToken {
do {
if var token = token {
if let expirationTimeInGMTFormat = token.expirationTimeInGMTFormat {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "GMT")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let data = dateFormatter.date(from: expirationTimeInGMTFormat) {
token.expirationTimeInMilliSecond = Int64(data.timeIntervalSince1970 * 1000)
}
}
let expirationDate = Date(timeIntervalSince1970: TimeInterval(token.expirationTimeInMilliSecond / 1000))
let interval = expirationDate.timeIntervalSince(NSDate.oss_clockSkewFixed())
if interval < 5 * 60 {
token = try self.tokenGetter()
self.token = token
}
return token
} else {
let token = try self.tokenGetter()
self.token = token
return token
}
} catch {
throw NSError(domain: OSSClientErrorDomain,
code: OSSClientErrorCODE.codeSignFailed.rawValue,
userInfo: [OSSErrorMessageTOKEN : error])
}
}
}

public class TaskCompletionSource: OSSTaskCompletionSource<AnyObject> {

public func wait(timeout: TimeInterval) {
let timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now() + timeout)
timer.setEventHandler {
if !self.task.isCompleted {
let error = NSError(domain: OSSClientErrorDomain,
code: OSSClientErrorCODE.codeNotKnown.rawValue,
userInfo: [OSSErrorMessageTOKEN : "TaskCompletionSource wait timeout."])
self.trySetError(error)
}
}
timer.resume()
task.waitUntilFinished()
timer.cancel()
}
}

0 comments on commit b683873

Please sign in to comment.