速率限制

This commit is contained in:
Jerry Yan 2025-04-08 01:27:40 +08:00
parent dabdde33a6
commit 248c06a30c

View File

@ -172,6 +172,9 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
AipFace client = getClient(); AipFace client = getClient();
HashMap<String, String> options = new HashMap<>(); HashMap<String, String> options = new HashMap<>();
List<String> tokenList = listUserFace(dbName, entityId); List<String> tokenList = listUserFace(dbName, entityId);
if (tokenList == null) {
return false;
}
AtomicInteger count = new AtomicInteger(0); AtomicInteger count = new AtomicInteger(0);
tokenList.forEach(faceToken -> { tokenList.forEach(faceToken -> {
try { try {
@ -232,9 +235,14 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
} }
public List<String> listUserFace(String dbName, String entityId) { public List<String> listUserFace(String dbName, String entityId) {
IRateLimiter listFaceLimiter = getLimiter(LOCK_TYPE.LIST_FACE);
try { try {
AipFace client = getClient(); AipFace client = getClient();
HashMap<String, String> options = new HashMap<>(); HashMap<String, String> options = new HashMap<>();
try {
listFaceLimiter.acquire();
} catch (InterruptedException ignored) {
}
JSONObject response = client.faceGetlist(entityId, dbName, options); JSONObject response = client.faceGetlist(entityId, dbName, options);
if (response.getInt("error_code") == 0) { if (response.getInt("error_code") == 0) {
JSONObject resultObj = response.getJSONObject("result"); JSONObject resultObj = response.getJSONObject("result");
@ -249,13 +257,16 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
} else { } else {
return Collections.emptyList(); return Collections.emptyList();
} }
} else if (response.getInt("error_code") == 223103) {
// 用户不存在
return Collections.emptyList();
} else { } else {
log.warn("获取人脸列表失败!{}", response); log.warn("获取人脸列表失败!{}", response);
return Collections.emptyList(); return null;
} }
} catch (Exception e) { } catch (Exception e) {
log.error("获取人脸列表失败!", e); log.error("获取人脸列表失败!", e);
return Collections.emptyList(); return null;
} }
} }
@ -325,21 +336,21 @@ public class BceFaceBodyAdapter implements IFaceBodyAdapter {
private IRateLimiter getLimiter(LOCK_TYPE type) { private IRateLimiter getLimiter(LOCK_TYPE type) {
switch (type) { switch (type) {
case ADD_DB: case ADD_DB:
return addDbLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(100, TimeUnit.MILLISECONDS)); return addDbLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(105, TimeUnit.MILLISECONDS));
case ADD_FACE: case ADD_FACE:
return addFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(config.getAddQps())); return addFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(config.getAddQps()));
case LIST_DB: case LIST_DB:
return listDbLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(100, TimeUnit.MILLISECONDS)); return listDbLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(105, TimeUnit.MILLISECONDS));
case LIST_FACE: case LIST_FACE:
return listFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(100, TimeUnit.MILLISECONDS)); return listFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(105, TimeUnit.MILLISECONDS));
case SEARCH_FACE: case SEARCH_FACE:
return searchFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(config.getSearchQps())); return searchFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(config.getSearchQps()));
case DELETE_DB: case DELETE_DB:
return deleteDbLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(100, TimeUnit.MILLISECONDS)); return deleteDbLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(105, TimeUnit.MILLISECONDS));
case DELETE_ENTITY: case DELETE_ENTITY:
return deleteEntityLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(100, TimeUnit.MILLISECONDS)); return deleteEntityLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(105, TimeUnit.MILLISECONDS));
case DELETE_FACE: case DELETE_FACE:
return deleteFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(100, TimeUnit.MILLISECONDS)); return deleteFaceLimiters.computeIfAbsent(config.getAppId(), k -> new FixedRateLimiter(105, TimeUnit.MILLISECONDS));
default: default:
return new FixedRateLimiter(500, TimeUnit.MILLISECONDS); return new FixedRateLimiter(500, TimeUnit.MILLISECONDS);
} }