Skip to content

Commit 4692655

Browse files
chengzeyiclaude
andcommitted
Uploads use the 120s read timeout; no total deadline on the file PUT
Ticket request uses the standard bounded request client. The file PUT uses read/write inactivity timeouts of Config.api.readTimeout and no callTimeout, so a large progressing upload is not aborted. An explicit timeout keeps its previous per-request behavior. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 76bb479 commit 4692655

3 files changed

Lines changed: 91 additions & 5 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Global configuration via `Config.api`:
8181
- `baseUrl` - API base URL (default: https://api.wavespeed.ai)
8282
- `connectionTimeout` - Connection timeout in seconds (default: 10.0)
8383
- `timeout` - Overall wait limit in seconds (default: null = no limit; tasks may run for hours). When set, it also bounds each request
84-
- `readTimeout` - Read/call timeout of one async submit or result request when `timeout` is unset (default: 120.0; sync-mode submit has none)
84+
- `readTimeout` - Read/call timeout of one async submit or result request when `timeout` is unset (default: 120.0; sync-mode submit has none). File upload requests use the same read timeout (the file PUT has no total-transfer deadline)
8585
- `maxRetries` - Task-level retries (default: 0)
8686
- `maxConnectionRetries` - HTTP connection retries (default: 5)
8787
- `retryInterval` - Base retry delay in seconds (default: 1.0)

‎src/main/java/ai/wavespeed/api/Client.java‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,24 @@ OkHttpClient requestClient(Double explicit, boolean boundedByDefault) {
353353
.build();
354354
}
355355

356+
/**
357+
* Client for the upload file PUT. A configured timeout behaves as for other
358+
* requests. By default read and write timeouts are Config.api.readTimeout
359+
* (socket inactivity) and there is no call timeout, since callTimeout is a
360+
* total deadline that would abort a large upload that is still progressing.
361+
*/
362+
OkHttpClient uploadPutClient(Double explicit) {
363+
if (configuredTimeout(explicit) != null) {
364+
return requestClient(explicit, true);
365+
}
366+
long ms = (long) (Config.api.readTimeout * 1000);
367+
return httpClient.newBuilder()
368+
.readTimeout(ms, TimeUnit.MILLISECONDS)
369+
.writeTimeout(ms, TimeUnit.MILLISECONDS)
370+
.callTimeout(0, TimeUnit.MILLISECONDS)
371+
.build();
372+
}
373+
356374
/** Clock for the wait loop's overall cap (replaceable in tests). */
357375
java.util.function.LongSupplier nowMillis = System::currentTimeMillis;
358376

@@ -681,7 +699,7 @@ public Map<String, Object> run(String model, Map<String, Object> input, double t
681699
* Upload a file to WaveSpeed.
682700
*
683701
* @param file File path string to upload
684-
* @param timeout Request timeout in seconds (null = Config.api.timeout; default no call limit)
702+
* @param timeout Request timeout in seconds (null = Config.api.timeout; default: 120s read timeout per request, no total-transfer limit on the file PUT)
685703
* @return URL of the uploaded file
686704
* @throws IllegalArgumentException if API key is not configured
687705
* @throws IllegalArgumentException if file path does not exist
@@ -713,8 +731,11 @@ public String upload(String file, Double timeout) {
713731
payload.put("content_type", contentType);
714732
}
715733

716-
// Uploads can legitimately take long: only a configured timeout bounds the call.
717-
OkHttpClient callClient = requestClient(timeout, false);
734+
// Ticket request: same bounded single-request timeouts as other requests.
735+
// The file PUT uses uploadPutClient(): an inactivity (read/write) timeout
736+
// without a total-transfer deadline, so a large progressing upload is not aborted.
737+
OkHttpClient callClient = requestClient(timeout, true);
738+
OkHttpClient putClient = uploadPutClient(timeout);
718739

719740
Request request = addClientHeaders(new Request.Builder()
720741
.url(this.baseUrl + "/api/v3/media/uploads")
@@ -766,7 +787,7 @@ public String upload(String file, Double timeout) {
766787
uploadHeaders.forEach((key, value) -> uploadRequest.addHeader(key, String.valueOf(value)));
767788
}
768789

769-
try (Response uploadResponse = callClient.newCall(uploadRequest.build()).execute()) {
790+
try (Response uploadResponse = putClient.newCall(uploadRequest.build()).execute()) {
770791
if (!uploadResponse.isSuccessful()) {
771792
String errorBody = uploadResponse.body() != null ? uploadResponse.body().string() : "";
772793
throw new RuntimeException(

‎src/test/java/ai/wavespeed/api/ClientTimeoutTest.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,69 @@ void hungResultGetFailsAloneAndIsRetried() throws Exception {
138138
assertEquals(3, server.getRequestCount());
139139
}
140140
}
141+
142+
@Test
143+
void uploadTicketAndPutDefaultTimeouts() {
144+
Client c = new Client("k");
145+
OkHttpClient ticket = c.requestClient(null, true);
146+
assertEquals(120_000, ticket.readTimeoutMillis());
147+
assertEquals(120_000, ticket.callTimeoutMillis());
148+
OkHttpClient put = c.uploadPutClient(null);
149+
assertEquals(120_000, put.readTimeoutMillis());
150+
assertEquals(120_000, put.writeTimeoutMillis());
151+
assertEquals(0, put.callTimeoutMillis()); // no total-transfer deadline
152+
}
153+
154+
@Test
155+
void uploadExplicitTimeoutKeepsTodaysBehavior() {
156+
OkHttpClient put = new Client("k").uploadPutClient(7.0);
157+
assertEquals(7_000, put.readTimeoutMillis());
158+
assertEquals(7_000, put.callTimeoutMillis());
159+
}
160+
161+
/**
162+
* Raw PUT endpoint whose response head trickles in over ~1.6s, with a
163+
* piece every 400ms (each gap is inside a 0.6s read timeout). Total time exceeds the read timeout; inactivity never does.
164+
*/
165+
private String uploadAgainstSlowPut(Double explicit, java.nio.file.Path dir) throws Exception {
166+
java.nio.file.Path f = dir.resolve("a.bin");
167+
java.nio.file.Files.write(f, new byte[]{1, 2, 3});
168+
try (java.net.ServerSocket raw = new java.net.ServerSocket(0);
169+
MockWebServer server = new MockWebServer()) {
170+
Thread t = new Thread(() -> {
171+
try (java.net.Socket sock = raw.accept()) {
172+
java.io.InputStream in = sock.getInputStream();
173+
byte[] buf = new byte[8192];
174+
in.read(buf); // request head + tiny body in one segment
175+
java.io.OutputStream out = sock.getOutputStream();
176+
sock.setTcpNoDelay(true);
177+
for (String part : new String[]{"HTTP/1.1 200 OK\r\n", "Content-Length: 0\r\n",
178+
"Connection: close\r\n", "\r\n"}) {
179+
Thread.sleep(400);
180+
out.write(part.getBytes());
181+
out.flush();
182+
}
183+
} catch (Exception ignored) {
184+
}
185+
});
186+
t.setDaemon(true);
187+
t.start();
188+
String putUrl = "http://127.0.0.1:" + raw.getLocalPort() + "/put";
189+
server.enqueue(new MockResponse().setBody("{\"code\":200,\"data\":{\"download_url\":\"https://e/x\","
190+
+ "\"upload\":{\"method\":\"PUT\",\"url\":\"" + putUrl + "\"}}}"));
191+
return new Client("k", server.url("/").toString(), null, 0, 0, 0.0).upload(f.toString(), explicit);
192+
}
193+
}
194+
195+
@Test
196+
void throttledPutLongerThanReadTimeoutIsNotAborted(@org.junit.jupiter.api.io.TempDir java.nio.file.Path dir) throws Exception {
197+
Config.api.readTimeout = 0.6;
198+
assertEquals("https://e/x", uploadAgainstSlowPut(null, dir));
199+
}
200+
201+
@Test
202+
void explicitTimeoutStillIsATotalDeadlineForUpload(@org.junit.jupiter.api.io.TempDir java.nio.file.Path dir) {
203+
Config.api.readTimeout = 0.6;
204+
assertThrows(RuntimeException.class, () -> uploadAgainstSlowPut(0.6, dir));
205+
}
141206
}

0 commit comments

Comments
 (0)