mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-12 04:03:42 +08:00
update the data struct of API KEYS
This commit is contained in:
@@ -215,7 +215,7 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol
|
||||
logger.Infof("API Key %s is deactivated", apiKey)
|
||||
// 移除当前 API key
|
||||
for i, v := range s.Config.Chat.ApiKeys {
|
||||
if v == apiKey {
|
||||
if v.Value == apiKey {
|
||||
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys[:i], s.Config.Chat.ApiKeys[i+1:]...)
|
||||
}
|
||||
}
|
||||
@@ -313,30 +313,26 @@ func (s *Server) sendMessage(ctx context.Context, session types.ChatSession, rol
|
||||
|
||||
// 随机获取一个 API Key,如果请求失败,则更换 API Key 重试
|
||||
func (s *Server) getApiKey(failedKey string) string {
|
||||
var keys = make([]string, 0)
|
||||
for _, v := range s.Config.Chat.ApiKeys {
|
||||
var keys = make([]types.APIKey, 0)
|
||||
for _, key := range s.Config.Chat.ApiKeys {
|
||||
// 过滤掉刚刚失败的 Key
|
||||
if v == failedKey {
|
||||
if key.Value == failedKey {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取 API Key 的上次调用时间,控制调用频率
|
||||
var lastAccess int64
|
||||
if t, ok := s.ApiKeyAccessStat[v]; ok {
|
||||
lastAccess = t
|
||||
}
|
||||
// 保持每分钟访问不超过 15 次
|
||||
if time.Now().Unix()-lastAccess <= 4 {
|
||||
// 保持每分钟访问不超过 15 次,控制调用频率
|
||||
if key.LastUsed > 0 && time.Now().Unix()-key.LastUsed <= 4 {
|
||||
continue
|
||||
}
|
||||
|
||||
keys = append(keys, v)
|
||||
keys = append(keys, key)
|
||||
}
|
||||
// 从可用的 Key 中随机选一个
|
||||
rand.NewSource(time.Now().UnixNano())
|
||||
if len(keys) > 0 {
|
||||
key := keys[rand.Intn(len(keys))]
|
||||
s.ApiKeyAccessStat[key] = time.Now().Unix()
|
||||
return key
|
||||
key.LastUsed = time.Now().Unix()
|
||||
return key.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ type Server struct {
|
||||
// 保存 Websocket 会话 Username, 每个 Username 只能连接一次
|
||||
// 防止第三方直接连接 socket 调用 OpenAI API
|
||||
ChatSession map[string]types.ChatSession //map[sessionId]User
|
||||
ApiKeyAccessStat map[string]int64 // 记录每个 API Key 的最后访问之间,保持在 15/min 之内
|
||||
ChatClients map[string]*WsClient // Websocket 连接集合
|
||||
ReqCancelFunc map[string]context.CancelFunc // HttpClient 请求取消 handle function
|
||||
DebugMode bool // 是否开启调试模式
|
||||
@@ -70,7 +69,6 @@ func NewServer(configPath string) (*Server, error) {
|
||||
ChatSession: make(map[string]types.ChatSession),
|
||||
ChatClients: make(map[string]*WsClient),
|
||||
ReqCancelFunc: make(map[string]context.CancelFunc),
|
||||
ApiKeyAccessStat: make(map[string]int64),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user