refactor(llm_factory): 新增 GLM 客户端未使用资源释放功能
- 在 NlpController 和 RagController 中添加了释放 GLM 客户端资源的逻辑 - 在 GlmClientHub 中增加了 UnavtiveOneGlmClient 方法,用于将客户端标记为未使用状态 - 优化了资源管理,提高了 GLM 客户端的利用率
This commit is contained in:
parent
ea102eef60
commit
f263ead51d
@ -22,6 +22,7 @@ func (n *Nlp) Title(context *gin.Context) {
|
|||||||
if ercode > 0 {
|
if ercode > 0 {
|
||||||
response.Fail(context, ercode, errcode.ErrMsg[ercode], errcode.ErrMsgForUser[ercode])
|
response.Fail(context, ercode, errcode.ErrMsg[ercode], errcode.ErrMsgForUser[ercode])
|
||||||
}
|
}
|
||||||
|
defer variable.GlmClientHub.UnavtiveOneGlmClient(tempGlmKey)
|
||||||
defer variable.GlmClientHub.ReleaseOneGlmClient(tempGlmKey) // 临时使用,用完就释放。
|
defer variable.GlmClientHub.ReleaseOneGlmClient(tempGlmKey) // 临时使用,用完就释放。
|
||||||
|
|
||||||
newTitle := nlp.GenerateTitle(content, client)
|
newTitle := nlp.GenerateTitle(content, client)
|
||||||
|
@ -87,6 +87,7 @@ func (r *Rag) ChatSSE(context *gin.Context) {
|
|||||||
response.Fail(context, ercode, errcode.ErrMsg[ercode], errcode.ErrMsgForUser[ercode])
|
response.Fail(context, ercode, errcode.ErrMsg[ercode], errcode.ErrMsgForUser[ercode])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer variable.GlmClientHub.UnavtiveOneGlmClient(token) // INFO ws 结束时,取消 Avtive 的占用。
|
||||||
|
|
||||||
// 1. query embedding
|
// 1. query embedding
|
||||||
embedding, ok := nlp.GetEmbedding([]string{query})
|
embedding, ok := nlp.GetEmbedding([]string{query})
|
||||||
@ -176,6 +177,7 @@ func (r *Rag) ChatWebSocket(context *gin.Context) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer variable.GlmClientHub.UnavtiveOneGlmClient(token) // INFO ws 结束时,取消 Avtive 的占用。
|
||||||
|
|
||||||
// 1. query embedding
|
// 1. query embedding
|
||||||
clientInfo.AddQuery(query)
|
clientInfo.AddQuery(query)
|
||||||
|
@ -11,12 +11,12 @@ import (
|
|||||||
|
|
||||||
type GlmClientHub struct {
|
type GlmClientHub struct {
|
||||||
Idle int // 最大连接数
|
Idle int // 最大连接数
|
||||||
Active int
|
Active int // 最大活跃数
|
||||||
ApiKey string
|
ApiKey string
|
||||||
DefaultModelName string
|
DefaultModelName string
|
||||||
InitPrompt string
|
InitPrompt string
|
||||||
Clients map[string]*ClientInfo
|
Clients map[string]*ClientInfo
|
||||||
LifeTime time.Duration
|
LifeTime time.Duration // 最长待机周期
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientInfo struct {
|
type ClientInfo struct {
|
||||||
@ -58,8 +58,9 @@ func (g *GlmClientHub) GetOneGlmClientInfo(token string, mode int) (clientInfo *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 空闲数检查
|
// 空闲数检查
|
||||||
if g.Idle > 0 {
|
if g.Idle > 0 && g.Active > 0 {
|
||||||
g.Idle -= 1
|
g.Idle -= 1
|
||||||
|
g.Active -= 1
|
||||||
} else {
|
} else {
|
||||||
code = errcode.ErrGlmBusy
|
code = errcode.ErrGlmBusy
|
||||||
return
|
return
|
||||||
@ -121,6 +122,21 @@ func (g *GlmClientHub) cleanupClients() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: ws 服务完毕,进入待机状态。
|
||||||
|
* @param {string} token
|
||||||
|
* @return {*}
|
||||||
|
* @Tip 对于临时使用的小功能,需要依次 defer 下面两个函数。
|
||||||
|
*/
|
||||||
|
func (g *GlmClientHub) UnavtiveOneGlmClient(token string) bool {
|
||||||
|
if clientInfo, exists := g.Clients[token]; exists {
|
||||||
|
g.Active -= 1
|
||||||
|
clientInfo.LastUsed = time.Now()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 显式地释放资源。
|
* @description: 显式地释放资源。
|
||||||
* @param {string} token
|
* @param {string} token
|
||||||
|
Loading…
x
Reference in New Issue
Block a user