Skip to content

Commit

Permalink
[Feature] Request Token Actor 생성 #627
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Oct 17, 2024
1 parent 9d01c76 commit eb35ea7
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion Projects/Core/SSInterceptor/Sources/SSTokenInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public struct SSTokenInterceptor: RequestInterceptor, Sendable {
public static let shared: SSTokenInterceptor = .init(helper: TokenInterceptHelper())

var helper: TokenInterceptHelpable
var statusActor: TokenRequestActor = .init(requestProgressStatus: .notProgress)

public init(helper: TokenInterceptHelpable) {
private init(helper: TokenInterceptHelpable) {
self.helper = helper
}

Expand Down Expand Up @@ -92,6 +93,13 @@ public struct SSTokenInterceptor: RequestInterceptor, Sendable {
}

public func refreshTokenResponse(result: Result<Response, MoyaError>, completion: @escaping (RetryResult) -> Void) {
// 현재 토큰 재발급중인지 확인
if statusActor.getProgressStatus() == .progress {
completion(.doNotRetry)
return
}
// progressStatus 변경
statusActor.setProgressStatus(.progress)
do {
switch result {
case let .success(response):
Expand All @@ -103,6 +111,8 @@ public struct SSTokenInterceptor: RequestInterceptor, Sendable {
refreshTokenExp: responseDTO.refreshTokenExp
))
os_log("토큰 재발급에 성공했습니다. 이천 요청을 수행합니다.")
// progressStatus 변경
statusActor.setProgressStatus(.notProgress)
completion(.retry)
case let .failure(error):
completion(.doNotRetryWithError(error))
Expand Down Expand Up @@ -158,3 +168,32 @@ public extension SSTokenInterceptor {
private struct UserInfoResponse: Decodable {
let id: Int64
}

final class TokenRequestActor: @unchecked Sendable {

private var requestProgressStatus: TokenRequestActorStatus

func getProgressStatus() -> TokenRequestActorStatus {
requestProgressStatus
}

init(requestProgressStatus: TokenRequestActorStatus) {
self.requestProgressStatus = requestProgressStatus
}

func setProgressStatus(_ status: TokenRequestActorStatus) {
switch status {
case .progress:
self.requestProgressStatus = status
case .notProgress:
DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
self.requestProgressStatus = status
}
}

}
}
enum TokenRequestActorStatus: Equatable, Sendable, CaseIterable {
case progress
case notProgress
}

0 comments on commit eb35ea7

Please sign in to comment.