You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add 2-second wall-clock timeout to Cloud Rewrite (#16)
Use withThrowingTaskGroup to race the API call against a 2s deadline.
If the Cerebras API doesn't respond within 2 seconds, the original
transcription text is used immediately — no more 12-second waits.
Implementation:
- TaskGroup race: API call vs Task.sleep(2s), first result wins
- Loser is cancelled via group.cancelAll() (frees URLSession resources)
- URLRequest.timeoutInterval lowered from 15s to 3s as safety net
- Timeout events logged for monitoring
- rewriteTimeout defined as named Duration constant
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
/// Models to try in priority order. First available model wins.
12
16
privatestaticletpreferredModels=[
13
17
"qwen-3-235b-a22b-instruct-2507",
@@ -114,7 +118,31 @@ final class CloudRewriteService {
114
118
}
115
119
116
120
do{
117
-
returntryawaitrewrite(text: text, apiKey: apiKey)
121
+
returntryawaitwithThrowingTaskGroup(of:String.self){ group in
122
+
// Race 1: the actual API call
123
+
group.addTask{[self]in
124
+
tryawaitself.rewrite(text: text, apiKey: apiKey)
125
+
}
126
+
127
+
// Race 2: timeout deadline
128
+
group.addTask{
129
+
tryawaitTask.sleep(for:Self.rewriteTimeout)
130
+
throwRewriteTimeoutError()
131
+
}
132
+
133
+
// First completed result wins; cancel the loser
134
+
guardlet result =tryawait group.next()else{
135
+
return text
136
+
}
137
+
group.cancelAll()
138
+
return result
139
+
}
140
+
}catch is RewriteTimeoutError{
141
+
cloudRewriteLogger.warning("Cloud rewrite timed out (\(Self.rewriteTimeout, privacy:.public)), using original text (\(text.count, privacy:.public) chars)")
0 commit comments