Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 11, 2024. It is now read-only.

Commit ea73207

Browse files
fix bug
1 parent 89a1ce0 commit ea73207

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

‎src/main/java/com/gzhu/funai/service/impl/AdminApiKeyServiceImpl.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ public boolean contains(ApiType apiTypes, String apikey) {
6060
public String roundRobinGetByType(ApiType apiTypes) {
6161
String apiKeyName = null;
6262
if(!CollectionUtils.isEmpty(cache)) {
63-
synchronized (AdminApiKeyServiceImpl.class) {
64-
// 根据apikey类型获取对应的apikey列表
65-
List<AdminApiKeyEntity> adminApiKeyEntities = cache.get(apiTypes.typeNo);
63+
// 根据apikey类型获取对应的apikey列表
64+
List<AdminApiKeyEntity> adminApiKeyEntities = cache.get(apiTypes.typeNo);
65+
66+
// 只有一个key,无需轮询
67+
if(adminApiKeyEntities.size() == 1){
68+
return adminApiKeyEntities.get(0).getName();
69+
}
6670

71+
synchronized (AdminApiKeyServiceImpl.class) {
6772
AdminApiKeyEntity adminApiKeyEntity = null;
6873
int index;
6974

@@ -81,7 +86,7 @@ public String roundRobinGetByType(ApiType apiTypes) {
8186

8287
roundRobinIndex[apiTypes.typeNo] = ++index;
8388
roundTime++;
84-
if(roundTime == adminApiKeyEntities.size()){
89+
if(roundTime > adminApiKeyEntities.size()){
8590
return null;
8691
}
8792
}
@@ -101,8 +106,6 @@ public String roundRobinGetByType(ApiType apiTypes) {
101106
return apiKeyName;
102107
}
103108

104-
105-
106109
/**
107110
* 1 初始化数据并按照apikey的类型分组
108111
* 2 重置轮询下标
@@ -123,7 +126,8 @@ public void load(){
123126
return;
124127
}
125128

126-
this.roundRobinIndex = new int[this.cache.size()];
129+
// 若apiKey类型一般不会超过26个,这边手动设置
130+
this.roundRobinIndex = new int[26];
127131

128132
log.info("加载AdminApiKey库缓存成功!, 有效的openai的apikey数量:{}",
129133
cache.get(ApiType.OPENAI.typeNo).size());

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /