@@ -60,10 +60,15 @@ public boolean contains(ApiType apiTypes, String apikey) {
60
60
public String roundRobinGetByType (ApiType apiTypes ) {
61
61
String apiKeyName = null ;
62
62
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
+ }
66
70
71
+ synchronized (AdminApiKeyServiceImpl .class ) {
67
72
AdminApiKeyEntity adminApiKeyEntity = null ;
68
73
int index ;
69
74
@@ -81,7 +86,7 @@ public String roundRobinGetByType(ApiType apiTypes) {
81
86
82
87
roundRobinIndex [apiTypes .typeNo ] = ++index ;
83
88
roundTime ++;
84
- if (roundTime == adminApiKeyEntities .size ()){
89
+ if (roundTime > adminApiKeyEntities .size ()){
85
90
return null ;
86
91
}
87
92
}
@@ -101,8 +106,6 @@ public String roundRobinGetByType(ApiType apiTypes) {
101
106
return apiKeyName ;
102
107
}
103
108
104
-
105
-
106
109
/**
107
110
* 1 初始化数据并按照apikey的类型分组
108
111
* 2 重置轮询下标
@@ -123,7 +126,8 @@ public void load(){
123
126
return ;
124
127
}
125
128
126
- this .roundRobinIndex = new int [this .cache .size ()];
129
+ // 若apiKey类型一般不会超过26个,这边手动设置
130
+ this .roundRobinIndex = new int [26 ];
127
131
128
132
log .info ("加载AdminApiKey库缓存成功!, 有效的openai的apikey数量:{}" ,
129
133
cache .get (ApiType .OPENAI .typeNo ).size ());
0 commit comments