fix: classify upstream failures and prevent empty billed responses

This commit is contained in:
wlx committed 2026-09-28 04:55:00 +08:00
1 parent b455d8d79f
commit 9acc69d4c7
28 files changed
+1449 -131

No files matched your search

@@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.security.SecretCipher;
import java.net.URI;
import java.net.SocketTimeoutException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
@@ -67,8 +69,13 @@ public class OpenAiCompatibleProvider {
try(var response=open(providerId,upstreamModel,input,false)) {
byte[] bytes=response.body.readNBytes(4*1024*1024+1);
if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
Map<String,Object> body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
if(body.containsKey("error")) throw classifyUpstreamError(200,writeJson(body));
return body;
} catch(ApiException e) { throw e; }
catch(HttpTimeoutException | SocketTimeoutException e) {
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游模型供应商响应超时",504);
}
catch(Exception e) { throw classifyUpstreamError(502, ""); }
}
@@ -109,6 +116,8 @@ public class OpenAiCompatibleProvider {
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
} catch (ApiException exception) {
throw exception;
} catch (HttpTimeoutException | SocketTimeoutException exception) {
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
@@ -131,23 +140,28 @@ public class OpenAiCompatibleProvider {
StringBuilder event=new StringBuilder(), line=new StringBuilder();
long total=0;
int c;
// Bound both individual frames and the whole response before allocating unbounded strings.
while((c=reader.read())!=-1) {
try {
// Bound both individual frames and the whole response before allocating unbounded strings.
while((c=reader.read())!=-1) {
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
if(c=='\n') {
String text=line.toString(); line.setLength(0);
if(text.endsWith("\r")) text=text.substring(0,text.length()-1);
if(text.isEmpty()) {
if(!event.isEmpty()) {
String data=event.substring(0,event.length()-1); event.setLength(0);
if("[DONE]".equals(data)) return;
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
consumer.accept(chunk);
}
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
} else line.append((char)c);
}
} catch(IOException exception) {
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
if(c=='\n') {
String text=line.toString(); line.setLength(0);
if(text.endsWith("\r")) text=text.substring(0,text.length()-1);
if(text.isEmpty()) {
if(!event.isEmpty()) {
String data=event.substring(0,event.length()-1); event.setLength(0);
if("[DONE]".equals(data)) return;
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
consumer.accept(chunk);
}
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
} else line.append((char)c);
throw exception;
}
throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502);
}
@@ -169,6 +183,9 @@ public class OpenAiCompatibleProvider {
*/
static ApiException classifyUpstreamError(int status, String body) {
String details = body == null ? "" : body.toLowerCase(java.util.Locale.ROOT);
if (status == 408 || status == 504 || details.matches(".*(request|gateway|upstream).{0,12}(timed out|timeout|超时).*")) {
return new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
}
if (status == 401 || status == 403 || details.matches(".*(invalid.{0,8}(api[ _-]?key|token)|unauthori[sz]ed|forbidden|无效.*(key|凭据)|无权限|认证失败).*")) {
return new ApiException("UPSTREAM_AUTH_FAILED", "上游渠道凭据无效或无权限", 502);
}
@@ -178,6 +195,9 @@ public class OpenAiCompatibleProvider {
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
}
if (status == 404 || details.matches(".*(no available channel|model.{0,16}(not found|does not exist|unavailable)|not_found|unsupported model|没有可用渠道|模型不存在|模型不可用).*")) {
return new ApiException("UPSTREAM_MODEL_UNAVAILABLE", "上游渠道没有可用的模型路由", 502);
}
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
}