-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[trello.com/c/0dgGzsPE]: timeouts for sending messages/attachments
- Loading branch information
1 parent
7e455e3
commit dd90046
Showing
20 changed files
with
225 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Adamant/Modules/Wallets/Adamant/AdmWalletService+Timeouts.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// AdmWalletService+Timeouts.swift | ||
// Adamant | ||
// | ||
// Created by Christian Benua on 06.02.2025. | ||
// Copyright © 2025 Adamant. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension AdmWalletService { | ||
struct MessageTimeouts { | ||
let message: TimeInterval | ||
let attachment: TimeInterval | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// Deadline.swift | ||
// CommonKit | ||
// | ||
// Created by Christian Benua on 06.02.2025. | ||
// | ||
|
||
import Foundation | ||
import QuartzCore | ||
|
||
public func deadline<R>( | ||
until instant: TimeInterval, | ||
isolation: isolated (any Actor)? = #isolation, | ||
operation: @Sendable () async throws -> R | ||
) async throws -> R where R: Sendable { | ||
let result = await withoutActuallyEscaping(operation) { operation in | ||
await withTaskGroup( | ||
of: DeadlineState<R>.self, | ||
returning: Result<R, any Error>.self, | ||
isolation: isolation | ||
) { taskGroup in | ||
|
||
taskGroup.addTask { | ||
do { | ||
let result = try await operation() | ||
return .operationResult(.success(result)) | ||
} catch { | ||
return .operationResult(.failure(error)) | ||
} | ||
} | ||
|
||
taskGroup.addTask { | ||
do { | ||
let interval = instant - CACurrentMediaTime() | ||
guard interval > 0 else { | ||
return .sleepResult(.failure(DeadlineExceededError())) | ||
} | ||
try await Task.sleep(interval: interval) | ||
return .sleepResult(.success(false)) | ||
} catch where Task.isCancelled { | ||
return .sleepResult(.success(true)) | ||
} catch { | ||
return .sleepResult(.failure(error)) | ||
} | ||
} | ||
|
||
defer { | ||
taskGroup.cancelAll() | ||
} | ||
|
||
for await next in taskGroup { | ||
switch next { | ||
case .operationResult(let result): | ||
return result | ||
case .sleepResult(.success(false)): | ||
return .failure(DeadlineExceededError()) | ||
case .sleepResult(.success(true)): | ||
continue | ||
case .sleepResult(.failure(let error)): | ||
return .failure(error) | ||
} | ||
} | ||
|
||
preconditionFailure("Invalid state") | ||
} | ||
} | ||
|
||
return try result.get() | ||
} | ||
|
||
enum DeadlineState<T>: Sendable where T: Sendable { | ||
case operationResult(Result<T, Error>) | ||
case sleepResult(Result<Bool, Error>) | ||
} | ||
|
||
public struct DeadlineExceededError: Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.