Skip to content

Commit 86ebabe

Browse files
committed
feat(model/transcription): add timeout in sync wait api
1 parent 5d3f1e5 commit 86ebabe

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

src/main/java/com/alibaba/dashscope/api/AsynchronousApi.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.alibaba.dashscope.base.HalfDuplexParamBase;
88
import com.alibaba.dashscope.common.DashScopeResult;
9+
import com.alibaba.dashscope.common.Status;
910
import com.alibaba.dashscope.common.TaskStatus;
1011
import com.alibaba.dashscope.exception.ApiException;
1112
import com.alibaba.dashscope.exception.NoApiKeyException;
@@ -76,12 +77,17 @@ public DashScopeResult asyncCall(ParamT param, ServiceOption serviceOption)
7677
* @param apiKey The api-key.
7778
* @param baseUrl The base http url.
7879
* @param customHeaders The custom headers.
80+
* @param timeoutSeconds The maximum time to wait in seconds.
7981
* @return The task result.
8082
* @throws NoApiKeyException Can not find api key
81-
* @throws ApiException The request failed, possibly due to a network or data error.
83+
* @throws ApiException The request failed, possibly due to a network or data error, or timeout.
8284
*/
8385
public DashScopeResult wait(
84-
String taskId, String apiKey, String baseUrl, Map<String, String> customHeaders)
86+
String taskId,
87+
String apiKey,
88+
String baseUrl,
89+
Map<String, String> customHeaders,
90+
long timeoutSeconds)
8591
throws ApiException, NoApiKeyException {
8692
AsyncTaskOption serviceOption =
8793
AsyncTaskOption.builder()
@@ -98,7 +104,23 @@ public DashScopeResult wait(
98104
int maxWaitMilliseconds = 5 * 1000;
99105
int incrementSteps = 3;
100106
int step = 0;
107+
long startTime = System.currentTimeMillis();
108+
long timeoutMillis = timeoutSeconds > 0 ? timeoutSeconds * 1000L : -1L;
101109
while (true) {
110+
if (timeoutMillis > 0) {
111+
long elapsed = System.currentTimeMillis() - startTime;
112+
if (elapsed >= timeoutMillis) {
113+
throw new ApiException(
114+
Status.builder()
115+
.statusCode(HttpURLConnection.HTTP_CLIENT_TIMEOUT)
116+
.code("TaskWaitTimeout")
117+
.message(
118+
StringUtils.format(
119+
"Waiting for task [%s] timed out after %d ms (timeoutSeconds=%d).",
120+
taskId, elapsed, timeoutSeconds))
121+
.build());
122+
}
123+
}
102124
try {
103125
DashScopeResult taskResult = client.send(req);
104126
JsonObject output = (JsonObject) taskResult.getOutput();
@@ -121,9 +143,20 @@ public DashScopeResult wait(
121143
? maxWaitMilliseconds
122144
: waitMilliseconds * 2;
123145
}
146+
long sleepMs = waitMilliseconds;
147+
if (timeoutMillis > 0) {
148+
long remaining = timeoutMillis - (System.currentTimeMillis() - startTime);
149+
if (remaining <= 0) {
150+
continue;
151+
}
152+
if (remaining < sleepMs) {
153+
sleepMs = remaining;
154+
}
155+
}
124156
try {
125-
Thread.sleep(waitMilliseconds);
126-
} catch (InterruptedException ignored) {
157+
Thread.sleep(sleepMs);
158+
} catch (InterruptedException e) {
159+
Thread.currentThread().interrupt();
127160
}
128161
}
129162
} catch (ApiException e) {
@@ -135,6 +168,23 @@ public DashScopeResult wait(
135168
}
136169
}
137170

171+
/**
172+
* Wait for async task completed and return task result.
173+
*
174+
* @param taskId The async task id.
175+
* @param apiKey The api-key.
176+
* @param baseUrl The base http url.
177+
* @param customHeaders The custom headers.
178+
* @return The task result.
179+
* @throws NoApiKeyException Can not find api key
180+
* @throws ApiException The request failed, possibly due to a network or data error.
181+
*/
182+
public DashScopeResult wait(
183+
String taskId, String apiKey, String baseUrl, Map<String, String> customHeaders)
184+
throws ApiException, NoApiKeyException {
185+
return wait(taskId, apiKey, baseUrl, customHeaders, -1);
186+
}
187+
138188
/**
139189
* Wait for async task completed and return task result.
140190
*

src/main/java/com/alibaba/dashscope/audio/asr/recognition/Recognition.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public Recognition() {
106106
duplexApi = new SynchronizeFullDuplexApi<>(serviceOption);
107107
}
108108

109+
public void setWebsocketUrl(String websocketUrl) {
110+
serviceOption.setBaseWebSocketUrl(websocketUrl);
111+
}
112+
109113
public Flowable<RecognitionResult> streamCall(
110114
RecognitionParam param, Flowable<ByteBuffer> audioFrame)
111115
throws ApiException, NoApiKeyException {

src/main/java/com/alibaba/dashscope/audio/asr/transcription/Transcription.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ public TranscriptionResult asyncCall(TranscriptionParam param) {
4545
}
4646

4747
public TranscriptionResult wait(TranscriptionQueryParam queryParam) {
48+
return wait(queryParam, -1);
49+
}
50+
51+
public TranscriptionResult wait(TranscriptionQueryParam queryParam, long timeoutSeconds) {
4852
try {
4953
return TranscriptionResult.fromDashScopeResult(
5054
asyncApi.wait(
5155
queryParam.getTaskId(),
5256
queryParam.getApiKey(),
5357
baseUrl,
54-
queryParam.getCustomHeaders()));
58+
queryParam.getCustomHeaders(),
59+
timeoutSeconds));
5560
} catch (NoApiKeyException e) {
5661
throw new ApiException(e);
5762
}

src/main/java/com/alibaba/dashscope/audio/qwen_asr/QwenTranscription.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ public QwenTranscriptionResult asyncCall(QwenTranscriptionParam param) {
4545
}
4646

4747
public QwenTranscriptionResult wait(QwenTranscriptionQueryParam queryParam) {
48+
return wait(queryParam, -1);
49+
}
50+
51+
public QwenTranscriptionResult wait(QwenTranscriptionQueryParam queryParam, long timeoutSeconds) {
4852
try {
4953
return QwenTranscriptionResult.fromDashScopeResult(
5054
asyncApi.wait(
5155
queryParam.getTaskId(),
5256
queryParam.getApiKey(),
5357
baseUrl,
54-
queryParam.getCustomHeaders()));
58+
queryParam.getCustomHeaders(),
59+
timeoutSeconds));
5560
} catch (NoApiKeyException e) {
5661
throw new ApiException(e);
5762
}

0 commit comments

Comments
 (0)