You've already forked FrameTour-BE
@@ -6,10 +6,6 @@ import com.ycwl.basic.facebody.entity.AddFaceResp;
|
||||
import com.ycwl.basic.facebody.entity.BceFaceBodyConfig;
|
||||
import com.ycwl.basic.facebody.entity.SearchFaceResp;
|
||||
import com.ycwl.basic.facebody.entity.SearchFaceResultItem;
|
||||
import com.ycwl.basic.facebody.exceptions.BceErrorCodeClassifier;
|
||||
import com.ycwl.basic.facebody.exceptions.FaceBodyException;
|
||||
import com.ycwl.basic.facebody.exceptions.NonRetryableFaceBodyException;
|
||||
import com.ycwl.basic.facebody.exceptions.RetryableFaceBodyException;
|
||||
import com.ycwl.basic.utils.ratelimiter.FixedRateLimiter;
|
||||
import com.ycwl.basic.utils.ratelimiter.IRateLimiter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -307,71 +303,18 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
|
||||
@Override
|
||||
public SearchFaceResp searchFace(String dbName, String faceUrl) {
|
||||
int retryCount = 0;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
return doSearchFace(dbName, faceUrl);
|
||||
} catch (RetryableFaceBodyException e) {
|
||||
// 获取建议的最大重试次数
|
||||
Integer maxRetries = BceErrorCodeClassifier.getSuggestedMaxRetries(e.getErrorCode());
|
||||
if (maxRetries == null) {
|
||||
maxRetries = 1; // 默认重试1次
|
||||
}
|
||||
|
||||
if (retryCount >= maxRetries) {
|
||||
log.error("搜索人脸重试{}次后仍失败,错误码: {}, 错误信息: {}",
|
||||
retryCount, e.getErrorCode(), e.getMessage());
|
||||
// 返回空结果而不是抛出异常,保持与原有逻辑一致
|
||||
return null;
|
||||
}
|
||||
|
||||
// 计算延迟时间
|
||||
Long delay = BceErrorCodeClassifier.getSuggestedRetryDelay(e.getErrorCode(), retryCount);
|
||||
if (delay == null) {
|
||||
delay = 500L; // 默认延迟500ms
|
||||
}
|
||||
|
||||
log.warn("搜索人脸失败[错误码: {}],{}ms后进行第{}次重试,错误信息: {}",
|
||||
e.getErrorCode(), delay, retryCount + 1, e.getMessage());
|
||||
|
||||
try {
|
||||
if (delay > 0) {
|
||||
Thread.sleep(delay);
|
||||
}
|
||||
} catch (InterruptedException ignored) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
retryCount++;
|
||||
} catch (NonRetryableFaceBodyException e) {
|
||||
// 不可重试错误,直接返回
|
||||
log.error("搜索人脸失败(不可重试),错误码: {}, 类别: {}, 错误信息: {}",
|
||||
e.getErrorCode(), e.getCategory(), e.getMessage());
|
||||
// 返回空结果而不是抛出异常,保持与原有逻辑一致
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private SearchFaceResp doSearchFace(String dbName, String faceUrl) {
|
||||
IRateLimiter searchFaceLimiter = getLimiter(LOCK_TYPE.SEARCH_FACE);
|
||||
SearchFaceResp resp = new SearchFaceResp();
|
||||
|
||||
try {
|
||||
AipFace client = getClient();
|
||||
HashMap<String, Object> options = new HashMap<>();
|
||||
options.put("max_user_num", "50");
|
||||
|
||||
try {
|
||||
searchFaceLimiter.acquire();
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
|
||||
JSONObject response = client.search(faceUrl, "URL", dbName, options);
|
||||
int errorCode = response.getInt("error_code");
|
||||
|
||||
if (errorCode == 0) {
|
||||
if (response.getInt("error_code") == 0) {
|
||||
resp.setOriginalFaceScore(100f);
|
||||
JSONObject resultObj = response.getJSONObject("result");
|
||||
if (resultObj == null) {
|
||||
@@ -395,18 +338,12 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
|
||||
}
|
||||
return resp;
|
||||
} else {
|
||||
// 使用错误码分类器创建相应的异常
|
||||
String errorMsg = response.optString("error_msg", "未知错误");
|
||||
throw BceErrorCodeClassifier.createException(errorCode,
|
||||
"人脸搜索失败[" + errorCode + "]: " + errorMsg);
|
||||
resp.setOriginalFaceScore(0f);
|
||||
return resp;
|
||||
}
|
||||
} catch (FaceBodyException e) {
|
||||
// 重新抛出 FaceBodyException
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// 其他异常(如网络异常)包装为可重试异常
|
||||
log.error("搜索人脸网络异常", e);
|
||||
throw new RetryableFaceBodyException("搜索人脸网络异常: " + e.getMessage(), e);
|
||||
log.error("搜索人脸失败!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user