feat: 定期保存 API KEY 的最后一次调用时间

This commit is contained in:
RockYang
2023-05-06 09:33:26 +08:00
parent 21adfd441c
commit a897a755b6
3 changed files with 28 additions and 12 deletions

View File

@@ -332,7 +332,12 @@ func (s *Server) getApiKey(failedKey string) string {
rand.NewSource(time.Now().UnixNano())
if len(keys) > 0 {
key := keys[rand.Intn(len(keys))]
key.LastUsed = time.Now().Unix()
// 更新选中 Key 的最后使用时间
for i, item := range s.Config.Chat.ApiKeys {
if item.Value == key.Value {
s.Config.Chat.ApiKeys[i].LastUsed = time.Now().Unix()
}
}
return key.Value
}
return ""

View File

@@ -143,6 +143,8 @@ func (s *Server) Run(webRoot embed.FS, path string, debug bool) {
delete(s.ChatContexts, key)
}
}
// 保存配置文档
_ = utils.SaveConfig(s.Config, s.ConfigPath)
time.Sleep(time.Second * 5) // 每隔 5 秒钟清理一次
}
}()