2
0

cache.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. package model
  2. import (
  3. "context"
  4. "encoding"
  5. "errors"
  6. "math/rand/v2"
  7. "slices"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "github.com/bytedance/sonic"
  14. "github.com/labring/aiproxy/core/common"
  15. "github.com/labring/aiproxy/core/common/config"
  16. "github.com/labring/aiproxy/core/common/conv"
  17. "github.com/labring/aiproxy/core/common/notify"
  18. "github.com/maruel/natural"
  19. "github.com/redis/go-redis/v9"
  20. log "github.com/sirupsen/logrus"
  21. "gorm.io/gorm"
  22. )
  23. const (
  24. SyncFrequency = time.Minute * 3
  25. TokenCacheKey = "token:%s"
  26. GroupCacheKey = "group:%s"
  27. GroupModelTPMKey = "group:%s:model_tpm"
  28. )
  29. var (
  30. _ encoding.BinaryMarshaler = (*redisStringSlice)(nil)
  31. _ redis.Scanner = (*redisStringSlice)(nil)
  32. )
  33. type redisStringSlice []string
  34. func (r *redisStringSlice) ScanRedis(value string) error {
  35. return sonic.Unmarshal(conv.StringToBytes(value), r)
  36. }
  37. func (r redisStringSlice) MarshalBinary() ([]byte, error) {
  38. return sonic.Marshal(r)
  39. }
  40. type redisTime time.Time
  41. var (
  42. _ redis.Scanner = (*redisTime)(nil)
  43. _ encoding.BinaryMarshaler = (*redisTime)(nil)
  44. )
  45. func (t *redisTime) ScanRedis(value string) error {
  46. return (*time.Time)(t).UnmarshalBinary(conv.StringToBytes(value))
  47. }
  48. func (t redisTime) MarshalBinary() ([]byte, error) {
  49. return time.Time(t).MarshalBinary()
  50. }
  51. type TokenCache struct {
  52. Group string `json:"group" redis:"g"`
  53. Key string `json:"-" redis:"-"`
  54. Name string `json:"name" redis:"n"`
  55. Subnets redisStringSlice `json:"subnets" redis:"s"`
  56. Models redisStringSlice `json:"models" redis:"m"`
  57. ID int `json:"id" redis:"i"`
  58. Status int `json:"status" redis:"st"`
  59. UsedAmount float64 `json:"used_amount" redis:"u"`
  60. // Quota system
  61. Quota float64 `json:"quota" redis:"q"`
  62. PeriodQuota float64 `json:"period_quota" redis:"pq"`
  63. PeriodType string `json:"period_type" redis:"pt"`
  64. PeriodLastUpdateTime redisTime `json:"period_last_update_time" redis:"plut"`
  65. PeriodLastUpdateAmount float64 `json:"period_last_update_amount" redis:"plua"`
  66. availableSets []string
  67. modelsBySet map[string][]string
  68. }
  69. func (t *TokenCache) SetAvailableSets(availableSets []string) {
  70. t.availableSets = availableSets
  71. }
  72. func (t *TokenCache) SetModelsBySet(modelsBySet map[string][]string) {
  73. t.modelsBySet = modelsBySet
  74. }
  75. func (t *TokenCache) FindModel(model string) string {
  76. var findModel string
  77. if len(t.Models) != 0 {
  78. if !slices.ContainsFunc(t.Models, func(e string) bool {
  79. ok := strings.EqualFold(e, model)
  80. if ok {
  81. findModel = e
  82. }
  83. return ok
  84. }) {
  85. return findModel
  86. }
  87. }
  88. return containsModel(model, t.availableSets, t.modelsBySet)
  89. }
  90. func containsModel(model string, sets []string, modelsBySet map[string][]string) string {
  91. var findModel string
  92. for _, set := range sets {
  93. if slices.ContainsFunc(modelsBySet[set], func(e string) bool {
  94. ok := strings.EqualFold(e, model)
  95. if ok {
  96. findModel = e
  97. }
  98. return ok
  99. }) {
  100. return findModel
  101. }
  102. }
  103. return findModel
  104. }
  105. func (t *TokenCache) Range(fn func(model string) bool) {
  106. ranged := make(map[string]struct{})
  107. if len(t.Models) != 0 {
  108. for _, model := range t.Models {
  109. if _, ok := ranged[model]; ok {
  110. continue
  111. }
  112. model = containsModel(model, t.availableSets, t.modelsBySet)
  113. if model == "" {
  114. continue
  115. }
  116. ranged[model] = struct{}{}
  117. if !fn(model) {
  118. return
  119. }
  120. }
  121. return
  122. }
  123. for _, set := range t.availableSets {
  124. for _, model := range t.modelsBySet[set] {
  125. if _, ok := ranged[model]; !ok {
  126. if !fn(model) {
  127. return
  128. }
  129. }
  130. ranged[model] = struct{}{}
  131. }
  132. }
  133. }
  134. func (t *Token) ToTokenCache() *TokenCache {
  135. return &TokenCache{
  136. ID: t.ID,
  137. Group: t.GroupID,
  138. Key: t.Key,
  139. Name: string(t.Name),
  140. Models: t.Models,
  141. Subnets: t.Subnets,
  142. Status: t.Status,
  143. UsedAmount: t.UsedAmount,
  144. Quota: t.Quota,
  145. PeriodQuota: t.PeriodQuota,
  146. PeriodType: string(t.PeriodType),
  147. PeriodLastUpdateTime: redisTime(t.PeriodLastUpdateTime),
  148. PeriodLastUpdateAmount: t.PeriodLastUpdateAmount,
  149. }
  150. }
  151. func CacheDeleteToken(key string) error {
  152. if !common.RedisEnabled {
  153. return nil
  154. }
  155. return common.RDB.Del(context.Background(), common.RedisKeyf(TokenCacheKey, key)).Err()
  156. }
  157. func CacheSetToken(token *TokenCache) error {
  158. if !common.RedisEnabled {
  159. return nil
  160. }
  161. key := common.RedisKeyf(TokenCacheKey, token.Key)
  162. pipe := common.RDB.Pipeline()
  163. pipe.HSet(context.Background(), key, token)
  164. expireTime := SyncFrequency + time.Duration(rand.Int64N(60)-30)*time.Second
  165. pipe.Expire(context.Background(), key, expireTime)
  166. _, err := pipe.Exec(context.Background())
  167. return err
  168. }
  169. func CacheGetTokenByKey(key string) (*TokenCache, error) {
  170. if !common.RedisEnabled {
  171. token, err := GetTokenByKey(key)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return token.ToTokenCache(), nil
  176. }
  177. cacheKey := common.RedisKeyf(TokenCacheKey, key)
  178. tokenCache := &TokenCache{}
  179. err := common.RDB.HGetAll(context.Background(), cacheKey).Scan(tokenCache)
  180. if err == nil && tokenCache.ID != 0 {
  181. tokenCache.Key = key
  182. return tokenCache, nil
  183. } else if err != nil && !errors.Is(err, redis.Nil) {
  184. log.Errorf("get token (%s) from redis error: %s", key, err.Error())
  185. }
  186. token, err := GetTokenByKey(key)
  187. if err != nil {
  188. return nil, err
  189. }
  190. tc := token.ToTokenCache()
  191. if err := CacheSetToken(tc); err != nil {
  192. log.Error("redis set token error: " + err.Error())
  193. }
  194. return tc, nil
  195. }
  196. var updateTokenUsedAmountOnlyIncreaseScript = redis.NewScript(`
  197. local used_amount = redis.call("HGet", KEYS[1], "ua")
  198. if used_amount == false then
  199. return redis.status_reply("ok")
  200. end
  201. if ARGV[1] < used_amount then
  202. return redis.status_reply("ok")
  203. end
  204. redis.call("HSet", KEYS[1], "ua", ARGV[1])
  205. return redis.status_reply("ok")
  206. `)
  207. func CacheUpdateTokenUsedAmountOnlyIncrease(key string, amount float64) error {
  208. if !common.RedisEnabled {
  209. return nil
  210. }
  211. return updateTokenUsedAmountOnlyIncreaseScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(TokenCacheKey, key)}, amount).
  212. Err()
  213. }
  214. // CacheResetTokenPeriodUsage resets period usage in cache and updates period last update info
  215. func CacheResetTokenPeriodUsage(
  216. key string,
  217. periodLastUpdateTime time.Time,
  218. periodLastUpdateAmount float64,
  219. ) error {
  220. if !common.RedisEnabled {
  221. return nil
  222. }
  223. cacheKey := common.RedisKeyf(TokenCacheKey, key)
  224. pipe := common.RDB.Pipeline()
  225. periodLastUpdateTimeBytes, _ := periodLastUpdateTime.MarshalBinary()
  226. pipe.HSet(context.Background(), cacheKey, "plut", periodLastUpdateTimeBytes)
  227. pipe.HSet(context.Background(), cacheKey, "plua", periodLastUpdateAmount)
  228. _, err := pipe.Exec(context.Background())
  229. return err
  230. }
  231. var updateTokenNameScript = redis.NewScript(`
  232. if redis.call("HExists", KEYS[1], "n") then
  233. redis.call("HSet", KEYS[1], "n", ARGV[1])
  234. end
  235. return redis.status_reply("ok")
  236. `)
  237. func CacheUpdateTokenName(key, name string) error {
  238. if !common.RedisEnabled {
  239. return nil
  240. }
  241. return updateTokenNameScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(TokenCacheKey, key)}, name).
  242. Err()
  243. }
  244. var updateTokenStatusScript = redis.NewScript(`
  245. if redis.call("HExists", KEYS[1], "st") then
  246. redis.call("HSet", KEYS[1], "st", ARGV[1])
  247. end
  248. return redis.status_reply("ok")
  249. `)
  250. func CacheUpdateTokenStatus(key string, status int) error {
  251. if !common.RedisEnabled {
  252. return nil
  253. }
  254. return updateTokenStatusScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(TokenCacheKey, key)}, status).
  255. Err()
  256. }
  257. type redisMap[K comparable, V any] map[K]V
  258. var (
  259. _ redis.Scanner = (*redisMap[string, any])(nil)
  260. _ encoding.BinaryMarshaler = (*redisMap[string, any])(nil)
  261. )
  262. func (r *redisMap[K, V]) ScanRedis(value string) error {
  263. return sonic.UnmarshalString(value, r)
  264. }
  265. func (r redisMap[K, V]) MarshalBinary() ([]byte, error) {
  266. return sonic.Marshal(r)
  267. }
  268. type (
  269. redisGroupModelConfigMap = redisMap[string, GroupModelConfig]
  270. )
  271. type GroupCache struct {
  272. ID string `json:"-" redis:"-"`
  273. Status int `json:"status" redis:"st"`
  274. UsedAmount float64 `json:"used_amount" redis:"ua"`
  275. RPMRatio float64 `json:"rpm_ratio" redis:"rpm_r"`
  276. TPMRatio float64 `json:"tpm_ratio" redis:"tpm_r"`
  277. AvailableSets redisStringSlice `json:"available_sets" redis:"ass"`
  278. ModelConfigs redisGroupModelConfigMap `json:"model_configs" redis:"mc"`
  279. BalanceAlertEnabled bool `json:"balance_alert_enabled" redis:"bae"`
  280. BalanceAlertThreshold float64 `json:"balance_alert_threshold" redis:"bat"`
  281. }
  282. func (g *GroupCache) GetAvailableSets() []string {
  283. if len(g.AvailableSets) == 0 {
  284. return []string{ChannelDefaultSet}
  285. }
  286. return g.AvailableSets
  287. }
  288. func (g *Group) ToGroupCache() *GroupCache {
  289. modelConfigs := make(redisGroupModelConfigMap, len(g.GroupModelConfigs))
  290. for _, modelConfig := range g.GroupModelConfigs {
  291. modelConfigs[modelConfig.Model] = modelConfig
  292. }
  293. return &GroupCache{
  294. ID: g.ID,
  295. Status: g.Status,
  296. UsedAmount: g.UsedAmount,
  297. RPMRatio: g.RPMRatio,
  298. TPMRatio: g.TPMRatio,
  299. AvailableSets: g.AvailableSets,
  300. ModelConfigs: modelConfigs,
  301. BalanceAlertEnabled: g.BalanceAlertEnabled,
  302. BalanceAlertThreshold: g.BalanceAlertThreshold,
  303. }
  304. }
  305. func CacheDeleteGroup(id string) error {
  306. if !common.RedisEnabled {
  307. return nil
  308. }
  309. return common.RDB.Del(context.Background(), common.RedisKeyf(GroupCacheKey, id)).Err()
  310. }
  311. var updateGroupRPMRatioScript = redis.NewScript(`
  312. if redis.call("HExists", KEYS[1], "rpm_r") then
  313. redis.call("HSet", KEYS[1], "rpm_r", ARGV[1])
  314. end
  315. return redis.status_reply("ok")
  316. `)
  317. func CacheUpdateGroupRPMRatio(id string, rpmRatio float64) error {
  318. if !common.RedisEnabled {
  319. return nil
  320. }
  321. return updateGroupRPMRatioScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(GroupCacheKey, id)}, rpmRatio).
  322. Err()
  323. }
  324. var updateGroupTPMRatioScript = redis.NewScript(`
  325. if redis.call("HExists", KEYS[1], "tpm_r") then
  326. redis.call("HSet", KEYS[1], "tpm_r", ARGV[1])
  327. end
  328. return redis.status_reply("ok")
  329. `)
  330. func CacheUpdateGroupTPMRatio(id string, tpmRatio float64) error {
  331. if !common.RedisEnabled {
  332. return nil
  333. }
  334. return updateGroupTPMRatioScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(GroupCacheKey, id)}, tpmRatio).
  335. Err()
  336. }
  337. var updateGroupStatusScript = redis.NewScript(`
  338. if redis.call("HExists", KEYS[1], "st") then
  339. redis.call("HSet", KEYS[1], "st", ARGV[1])
  340. end
  341. return redis.status_reply("ok")
  342. `)
  343. func CacheUpdateGroupStatus(id string, status int) error {
  344. if !common.RedisEnabled {
  345. return nil
  346. }
  347. return updateGroupStatusScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(GroupCacheKey, id)}, status).
  348. Err()
  349. }
  350. func CacheSetGroup(group *GroupCache) error {
  351. if !common.RedisEnabled {
  352. return nil
  353. }
  354. key := common.RedisKeyf(GroupCacheKey, group.ID)
  355. pipe := common.RDB.Pipeline()
  356. pipe.HSet(context.Background(), key, group)
  357. expireTime := SyncFrequency + time.Duration(rand.Int64N(60)-30)*time.Second
  358. pipe.Expire(context.Background(), key, expireTime)
  359. _, err := pipe.Exec(context.Background())
  360. return err
  361. }
  362. func CacheGetGroup(id string) (*GroupCache, error) {
  363. if !common.RedisEnabled {
  364. group, err := GetGroupByID(id, true)
  365. if err != nil {
  366. return nil, err
  367. }
  368. return group.ToGroupCache(), nil
  369. }
  370. cacheKey := common.RedisKeyf(GroupCacheKey, id)
  371. groupCache := &GroupCache{}
  372. err := common.RDB.HGetAll(context.Background(), cacheKey).Scan(groupCache)
  373. if err == nil && groupCache.Status != 0 {
  374. groupCache.ID = id
  375. return groupCache, nil
  376. } else if err != nil && !errors.Is(err, redis.Nil) {
  377. log.Errorf("get group (%s) from redis error: %s", id, err.Error())
  378. }
  379. group, err := GetGroupByID(id, true)
  380. if err != nil {
  381. return nil, err
  382. }
  383. gc := group.ToGroupCache()
  384. if err := CacheSetGroup(gc); err != nil {
  385. log.Error("redis set group error: " + err.Error())
  386. }
  387. return gc, nil
  388. }
  389. var updateGroupUsedAmountOnlyIncreaseScript = redis.NewScript(`
  390. local used_amount = redis.call("HGet", KEYS[1], "ua")
  391. if used_amount == false then
  392. return redis.status_reply("ok")
  393. end
  394. if ARGV[1] < used_amount then
  395. return redis.status_reply("ok")
  396. end
  397. redis.call("HSet", KEYS[1], "ua", ARGV[1])
  398. return redis.status_reply("ok")
  399. `)
  400. func CacheUpdateGroupUsedAmountOnlyIncrease(id string, amount float64) error {
  401. if !common.RedisEnabled {
  402. return nil
  403. }
  404. return updateGroupUsedAmountOnlyIncreaseScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(GroupCacheKey, id)}, amount).
  405. Err()
  406. }
  407. type GroupMCPCache struct {
  408. ID string `json:"id" redis:"i"`
  409. GroupID string `json:"group_id" redis:"g"`
  410. Status GroupMCPStatus `json:"status" redis:"s"`
  411. Type GroupMCPType `json:"type" redis:"t"`
  412. ProxyConfig *GroupMCPProxyConfig `json:"proxy_config" redis:"pc"`
  413. OpenAPIConfig *MCPOpenAPIConfig `json:"openapi_config" redis:"oc"`
  414. }
  415. func (g *GroupMCP) ToGroupMCPCache() *GroupMCPCache {
  416. return &GroupMCPCache{
  417. ID: g.ID,
  418. GroupID: g.GroupID,
  419. Status: g.Status,
  420. Type: g.Type,
  421. ProxyConfig: g.ProxyConfig,
  422. OpenAPIConfig: g.OpenAPIConfig,
  423. }
  424. }
  425. const (
  426. GroupMCPCacheKey = "group_mcp:%s:%s" // group_id:mcp_id
  427. )
  428. func CacheDeleteGroupMCP(groupID, mcpID string) error {
  429. if !common.RedisEnabled {
  430. return nil
  431. }
  432. return common.RDB.Del(context.Background(), common.RedisKeyf(GroupMCPCacheKey, groupID, mcpID)).
  433. Err()
  434. }
  435. func CacheSetGroupMCP(groupMCP *GroupMCPCache) error {
  436. if !common.RedisEnabled {
  437. return nil
  438. }
  439. key := common.RedisKeyf(GroupMCPCacheKey, groupMCP.GroupID, groupMCP.ID)
  440. pipe := common.RDB.Pipeline()
  441. pipe.HSet(context.Background(), key, groupMCP)
  442. expireTime := SyncFrequency + time.Duration(rand.Int64N(60)-30)*time.Second
  443. pipe.Expire(context.Background(), key, expireTime)
  444. _, err := pipe.Exec(context.Background())
  445. return err
  446. }
  447. func CacheGetGroupMCP(groupID, mcpID string) (*GroupMCPCache, error) {
  448. if !common.RedisEnabled {
  449. groupMCP, err := GetGroupMCPByID(mcpID, groupID)
  450. if err != nil {
  451. return nil, err
  452. }
  453. return groupMCP.ToGroupMCPCache(), nil
  454. }
  455. cacheKey := common.RedisKeyf(GroupMCPCacheKey, groupID, mcpID)
  456. groupMCPCache := &GroupMCPCache{}
  457. err := common.RDB.HGetAll(context.Background(), cacheKey).Scan(groupMCPCache)
  458. if err == nil && groupMCPCache.ID != "" {
  459. return groupMCPCache, nil
  460. } else if err != nil && !errors.Is(err, redis.Nil) {
  461. log.Errorf("get group mcp (%s:%s) from redis error: %s", groupID, mcpID, err.Error())
  462. }
  463. groupMCP, err := GetGroupMCPByID(mcpID, groupID)
  464. if err != nil {
  465. return nil, err
  466. }
  467. gmc := groupMCP.ToGroupMCPCache()
  468. if err := CacheSetGroupMCP(gmc); err != nil {
  469. log.Error("redis set group mcp error: " + err.Error())
  470. }
  471. return gmc, nil
  472. }
  473. var updateGroupMCPStatusScript = redis.NewScript(`
  474. if redis.call("HExists", KEYS[1], "s") then
  475. redis.call("HSet", KEYS[1], "s", ARGV[1])
  476. end
  477. return redis.status_reply("ok")
  478. `)
  479. func CacheUpdateGroupMCPStatus(groupID, mcpID string, status GroupMCPStatus) error {
  480. if !common.RedisEnabled {
  481. return nil
  482. }
  483. return updateGroupMCPStatusScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(GroupMCPCacheKey, groupID, mcpID)}, status).
  484. Err()
  485. }
  486. type PublicMCPCache struct {
  487. ID string `json:"id" redis:"i"`
  488. Status PublicMCPStatus `json:"status" redis:"s"`
  489. Type PublicMCPType `json:"type" redis:"t"`
  490. Price MCPPrice `json:"price" redis:"p"`
  491. ProxyConfig *PublicMCPProxyConfig `json:"proxy_config" redis:"pc"`
  492. OpenAPIConfig *MCPOpenAPIConfig `json:"openapi_config" redis:"oc"`
  493. EmbedConfig *MCPEmbeddingConfig `json:"embed_config" redis:"ec"`
  494. }
  495. func (p *PublicMCP) ToPublicMCPCache() *PublicMCPCache {
  496. return &PublicMCPCache{
  497. ID: p.ID,
  498. Status: p.Status,
  499. Type: p.Type,
  500. Price: p.Price,
  501. ProxyConfig: p.ProxyConfig,
  502. OpenAPIConfig: p.OpenAPIConfig,
  503. EmbedConfig: p.EmbedConfig,
  504. }
  505. }
  506. const (
  507. PublicMCPCacheKey = "public_mcp:%s" // mcp_id
  508. )
  509. func CacheDeletePublicMCP(mcpID string) error {
  510. if !common.RedisEnabled {
  511. return nil
  512. }
  513. return common.RDB.Del(context.Background(), common.RedisKeyf(PublicMCPCacheKey, mcpID)).Err()
  514. }
  515. func CacheSetPublicMCP(publicMCP *PublicMCPCache) error {
  516. if !common.RedisEnabled {
  517. return nil
  518. }
  519. key := common.RedisKeyf(PublicMCPCacheKey, publicMCP.ID)
  520. pipe := common.RDB.Pipeline()
  521. pipe.HSet(context.Background(), key, publicMCP)
  522. expireTime := SyncFrequency + time.Duration(rand.Int64N(60)-30)*time.Second
  523. pipe.Expire(context.Background(), key, expireTime)
  524. _, err := pipe.Exec(context.Background())
  525. return err
  526. }
  527. func CacheGetPublicMCP(mcpID string) (*PublicMCPCache, error) {
  528. if !common.RedisEnabled {
  529. publicMCP, err := GetPublicMCPByID(mcpID)
  530. if err != nil {
  531. return nil, err
  532. }
  533. return publicMCP.ToPublicMCPCache(), nil
  534. }
  535. cacheKey := common.RedisKeyf(PublicMCPCacheKey, mcpID)
  536. publicMCPCache := &PublicMCPCache{}
  537. err := common.RDB.HGetAll(context.Background(), cacheKey).Scan(publicMCPCache)
  538. if err == nil && publicMCPCache.ID != "" {
  539. return publicMCPCache, nil
  540. } else if err != nil && !errors.Is(err, redis.Nil) {
  541. log.Errorf("get public mcp (%s) from redis error: %s", mcpID, err.Error())
  542. }
  543. publicMCP, err := GetPublicMCPByID(mcpID)
  544. if err != nil {
  545. return nil, err
  546. }
  547. pmc := publicMCP.ToPublicMCPCache()
  548. if err := CacheSetPublicMCP(pmc); err != nil {
  549. log.Error("redis set public mcp error: " + err.Error())
  550. }
  551. return pmc, nil
  552. }
  553. var updatePublicMCPStatusScript = redis.NewScript(`
  554. if redis.call("HExists", KEYS[1], "s") then
  555. redis.call("HSet", KEYS[1], "s", ARGV[1])
  556. end
  557. return redis.status_reply("ok")
  558. `)
  559. func CacheUpdatePublicMCPStatus(mcpID string, status PublicMCPStatus) error {
  560. if !common.RedisEnabled {
  561. return nil
  562. }
  563. return updatePublicMCPStatusScript.Run(context.Background(), common.RDB, []string{common.RedisKeyf(PublicMCPCacheKey, mcpID)}, status).
  564. Err()
  565. }
  566. const (
  567. PublicMCPReusingParamCacheKey = "public_mcp_param:%s:%s" // mcp_id:group_id
  568. )
  569. type PublicMCPReusingParamCache struct {
  570. MCPID string `json:"mcp_id" redis:"m"`
  571. GroupID string `json:"group_id" redis:"g"`
  572. Params redisMap[string, string] `json:"params" redis:"p"`
  573. }
  574. func (p *PublicMCPReusingParam) ToPublicMCPReusingParamCache() PublicMCPReusingParamCache {
  575. return PublicMCPReusingParamCache{
  576. MCPID: p.MCPID,
  577. GroupID: p.GroupID,
  578. Params: p.Params,
  579. }
  580. }
  581. func CacheDeletePublicMCPReusingParam(mcpID, groupID string) error {
  582. if !common.RedisEnabled {
  583. return nil
  584. }
  585. return common.RDB.Del(context.Background(), common.RedisKeyf(PublicMCPReusingParamCacheKey, mcpID, groupID)).
  586. Err()
  587. }
  588. func CacheSetPublicMCPReusingParam(param PublicMCPReusingParamCache) error {
  589. if !common.RedisEnabled {
  590. return nil
  591. }
  592. key := common.RedisKeyf(PublicMCPReusingParamCacheKey, param.MCPID, param.GroupID)
  593. pipe := common.RDB.Pipeline()
  594. pipe.HSet(context.Background(), key, param)
  595. expireTime := SyncFrequency + time.Duration(rand.Int64N(60)-30)*time.Second
  596. pipe.Expire(context.Background(), key, expireTime)
  597. _, err := pipe.Exec(context.Background())
  598. return err
  599. }
  600. func CacheGetPublicMCPReusingParam(mcpID, groupID string) (PublicMCPReusingParamCache, error) {
  601. if groupID == "" {
  602. return PublicMCPReusingParamCache{
  603. MCPID: mcpID,
  604. GroupID: groupID,
  605. Params: make(map[string]string),
  606. }, nil
  607. }
  608. if !common.RedisEnabled {
  609. param, err := GetPublicMCPReusingParam(mcpID, groupID)
  610. if err != nil {
  611. return PublicMCPReusingParamCache{}, err
  612. }
  613. return param.ToPublicMCPReusingParamCache(), nil
  614. }
  615. cacheKey := common.RedisKeyf(PublicMCPReusingParamCacheKey, mcpID, groupID)
  616. paramCache := PublicMCPReusingParamCache{}
  617. err := common.RDB.HGetAll(context.Background(), cacheKey).Scan(&paramCache)
  618. if err == nil && paramCache.MCPID != "" {
  619. return paramCache, nil
  620. } else if err != nil && !errors.Is(err, redis.Nil) {
  621. log.Errorf("get public mcp reusing param (%s:%s) from redis error: %s", mcpID, groupID, err.Error())
  622. }
  623. param, err := GetPublicMCPReusingParam(mcpID, groupID)
  624. if err != nil {
  625. return PublicMCPReusingParamCache{}, err
  626. }
  627. prc := param.ToPublicMCPReusingParamCache()
  628. if err := CacheSetPublicMCPReusingParam(prc); err != nil {
  629. log.Error("redis set public mcp reusing param error: " + err.Error())
  630. }
  631. return prc, nil
  632. }
  633. const (
  634. StoreCacheKey = "storev2:%s:%d:%s" // store_id
  635. )
  636. type StoreCache struct {
  637. ID string `json:"id" redis:"i"`
  638. GroupID string `json:"group_id" redis:"g"`
  639. TokenID int `json:"token_id" redis:"t"`
  640. ChannelID int `json:"channel_id" redis:"c"`
  641. Model string `json:"model" redis:"m"`
  642. ExpiresAt time.Time `json:"expires_at" redis:"e"`
  643. }
  644. func (s *StoreV2) ToStoreCache() *StoreCache {
  645. return &StoreCache{
  646. ID: s.ID,
  647. GroupID: s.GroupID,
  648. TokenID: s.TokenID,
  649. ChannelID: s.ChannelID,
  650. Model: s.Model,
  651. ExpiresAt: s.ExpiresAt,
  652. }
  653. }
  654. func CacheSetStore(store *StoreCache) error {
  655. if !common.RedisEnabled {
  656. return nil
  657. }
  658. key := common.RedisKeyf(StoreCacheKey, store.GroupID, store.TokenID, store.ID)
  659. pipe := common.RDB.Pipeline()
  660. pipe.HSet(context.Background(), key, store)
  661. expireTime := SyncFrequency + time.Duration(rand.Int64N(60)-30)*time.Second
  662. pipe.Expire(context.Background(), key, expireTime)
  663. _, err := pipe.Exec(context.Background())
  664. return err
  665. }
  666. func CacheGetStore(group string, tokenID int, id string) (*StoreCache, error) {
  667. if !common.RedisEnabled {
  668. store, err := GetStore(group, tokenID, id)
  669. if err != nil {
  670. return nil, err
  671. }
  672. return store.ToStoreCache(), nil
  673. }
  674. cacheKey := common.RedisKeyf(StoreCacheKey, group, tokenID, id)
  675. storeCache := &StoreCache{}
  676. err := common.RDB.HGetAll(context.Background(), cacheKey).Scan(storeCache)
  677. if err == nil && storeCache.ID != "" {
  678. return storeCache, nil
  679. }
  680. store, err := GetStore(group, tokenID, id)
  681. if err != nil {
  682. return nil, err
  683. }
  684. sc := store.ToStoreCache()
  685. if err := CacheSetStore(sc); err != nil {
  686. log.Error("redis set store error: " + err.Error())
  687. }
  688. return sc, nil
  689. }
  690. type ModelConfigCache interface {
  691. GetModelConfig(model string) (ModelConfig, bool)
  692. }
  693. // read-only cache
  694. //
  695. type ModelCaches struct {
  696. ModelConfig ModelConfigCache
  697. // map[set][]model
  698. EnabledModelsBySet map[string][]string
  699. // map[set][]modelconfig
  700. EnabledModelConfigsBySet map[string][]ModelConfig
  701. // map[model]modelconfig
  702. EnabledModelConfigsMap map[string]ModelConfig
  703. // map[set]map[model][]channel
  704. EnabledModel2ChannelsBySet map[string]map[string][]*Channel
  705. // map[set]map[model][]channel
  706. DisabledModel2ChannelsBySet map[string]map[string][]*Channel
  707. }
  708. var modelCaches atomic.Pointer[ModelCaches]
  709. func init() {
  710. modelCaches.Store(new(ModelCaches))
  711. }
  712. func LoadModelCaches() *ModelCaches {
  713. return modelCaches.Load()
  714. }
  715. // InitModelConfigAndChannelCache initializes the channel cache from database
  716. func InitModelConfigAndChannelCache() error {
  717. modelConfig, err := initializeModelConfigCache()
  718. if err != nil {
  719. return err
  720. }
  721. // Apply YAML config overrides to model configs
  722. modelConfig = applyYAMLConfigToModelConfigCache(modelConfig)
  723. // Load enabled channels from database
  724. enabledChannels, err := LoadEnabledChannels()
  725. if err != nil {
  726. return err
  727. }
  728. // Build model to channels map by set
  729. enabledModel2ChannelsBySet := buildModelToChannelsBySetMap(enabledChannels)
  730. // Sort channels by priority within each set
  731. sortChannelsByPriorityBySet(enabledModel2ChannelsBySet)
  732. // Build enabled models and configs by set
  733. enabledModelsBySet, enabledModelConfigsBySet, enabledModelConfigsMap := buildEnabledModelsBySet(
  734. enabledModel2ChannelsBySet,
  735. modelConfig,
  736. )
  737. // Load disabled channels
  738. disabledChannels, err := LoadDisabledChannels()
  739. if err != nil {
  740. return err
  741. }
  742. // Build disabled model to channels map by set
  743. disabledModel2ChannelsBySet := buildModelToChannelsBySetMap(disabledChannels)
  744. // Update global cache atomically
  745. modelCaches.Store(&ModelCaches{
  746. ModelConfig: modelConfig,
  747. EnabledModelsBySet: enabledModelsBySet,
  748. EnabledModelConfigsBySet: enabledModelConfigsBySet,
  749. EnabledModelConfigsMap: enabledModelConfigsMap,
  750. EnabledModel2ChannelsBySet: enabledModel2ChannelsBySet,
  751. DisabledModel2ChannelsBySet: disabledModel2ChannelsBySet,
  752. })
  753. return nil
  754. }
  755. func LoadEnabledChannels() ([]*Channel, error) {
  756. var channels []*Channel
  757. err := DB.Where("status = ?", ChannelStatusEnabled).Find(&channels).Error
  758. if err != nil {
  759. return nil, err
  760. }
  761. configChannels := NewConfigChannels(LoadYAMLConfig(), ChannelStatusEnabled)
  762. if len(configChannels) != 0 {
  763. log.Infof("added %d channels from config", len(configChannels))
  764. channels = append(channels, configChannels...)
  765. }
  766. for _, channel := range channels {
  767. initializeChannelModels(channel)
  768. initializeChannelModelMapping(channel)
  769. }
  770. return channels, nil
  771. }
  772. func LoadDisabledChannels() ([]*Channel, error) {
  773. var channels []*Channel
  774. err := DB.Where("status = ?", ChannelStatusDisabled).Find(&channels).Error
  775. if err != nil {
  776. return nil, err
  777. }
  778. configChannels := NewConfigChannels(LoadYAMLConfig(), ChannelStatusDisabled)
  779. if len(configChannels) != 0 {
  780. log.Infof("added %d channels from config", len(configChannels))
  781. channels = append(channels, configChannels...)
  782. }
  783. for _, channel := range channels {
  784. initializeChannelModels(channel)
  785. initializeChannelModelMapping(channel)
  786. }
  787. return channels, nil
  788. }
  789. func LoadChannels() ([]*Channel, error) {
  790. var channels []*Channel
  791. err := DB.Find(&channels).Error
  792. if err != nil {
  793. return nil, err
  794. }
  795. configChannels := NewConfigChannels(LoadYAMLConfig(), 0)
  796. if len(configChannels) != 0 {
  797. log.Infof("added %d channels from config", len(configChannels))
  798. channels = append(channels, configChannels...)
  799. }
  800. for _, channel := range channels {
  801. initializeChannelModels(channel)
  802. initializeChannelModelMapping(channel)
  803. }
  804. return channels, nil
  805. }
  806. func LoadChannelByID(id int) (*Channel, error) {
  807. var channel Channel
  808. err := DB.First(&channel, id).Error
  809. if err != nil {
  810. if !errors.Is(err, gorm.ErrRecordNotFound) {
  811. return nil, err
  812. }
  813. chs, err := LoadChannels()
  814. if err != nil {
  815. return nil, err
  816. }
  817. for _, c := range chs {
  818. if c.ID == id {
  819. return c, nil
  820. }
  821. }
  822. return nil, gorm.ErrRecordNotFound
  823. }
  824. initializeChannelModels(&channel)
  825. initializeChannelModelMapping(&channel)
  826. return &channel, nil
  827. }
  828. var _ ModelConfigCache = (*modelConfigMapCache)(nil)
  829. type modelConfigMapCache struct {
  830. modelConfigMap map[string]ModelConfig
  831. }
  832. func (m *modelConfigMapCache) GetModelConfig(model string) (ModelConfig, bool) {
  833. config, ok := m.modelConfigMap[model]
  834. return config, ok
  835. }
  836. var _ ModelConfigCache = (*disabledModelConfigCache)(nil)
  837. type disabledModelConfigCache struct {
  838. modelConfigs ModelConfigCache
  839. }
  840. func (d *disabledModelConfigCache) GetModelConfig(model string) (ModelConfig, bool) {
  841. if config, ok := d.modelConfigs.GetModelConfig(model); ok {
  842. return config, true
  843. }
  844. return NewDefaultModelConfig(model), true
  845. }
  846. func initializeModelConfigCache() (ModelConfigCache, error) {
  847. modelConfigs, err := GetAllModelConfigs()
  848. if err != nil {
  849. return nil, err
  850. }
  851. newModelConfigMap := make(map[string]ModelConfig)
  852. for _, modelConfig := range modelConfigs {
  853. newModelConfigMap[modelConfig.Model] = modelConfig
  854. }
  855. configs := &modelConfigMapCache{modelConfigMap: newModelConfigMap}
  856. if config.DisableModelConfig {
  857. return &disabledModelConfigCache{modelConfigs: configs}, nil
  858. }
  859. return configs, nil
  860. }
  861. func initializeChannelModels(channel *Channel) {
  862. if len(channel.Models) == 0 {
  863. channel.Models = config.GetDefaultChannelModels()[int(channel.Type)]
  864. return
  865. }
  866. findedModels, missingModels, err := GetModelConfigWithModels(channel.Models)
  867. if err != nil {
  868. return
  869. }
  870. if len(missingModels) > 0 {
  871. slices.Sort(missingModels)
  872. log.Errorf("model config not found: %v", missingModels)
  873. }
  874. slices.Sort(findedModels)
  875. channel.Models = findedModels
  876. }
  877. func initializeChannelModelMapping(channel *Channel) {
  878. if len(channel.ModelMapping) == 0 {
  879. channel.ModelMapping = config.GetDefaultChannelModelMapping()[int(channel.Type)]
  880. }
  881. }
  882. func buildModelToChannelsBySetMap(channels []*Channel) map[string]map[string][]*Channel {
  883. modelMapBySet := make(map[string]map[string][]*Channel)
  884. for _, channel := range channels {
  885. sets := channel.GetSets()
  886. for _, set := range sets {
  887. if _, ok := modelMapBySet[set]; !ok {
  888. modelMapBySet[set] = make(map[string][]*Channel)
  889. }
  890. for _, model := range channel.Models {
  891. modelMapBySet[set][model] = append(modelMapBySet[set][model], channel)
  892. }
  893. }
  894. }
  895. return modelMapBySet
  896. }
  897. func sortChannelsByPriorityBySet(modelMapBySet map[string]map[string][]*Channel) {
  898. for _, modelMap := range modelMapBySet {
  899. for _, channels := range modelMap {
  900. sort.Slice(channels, func(i, j int) bool {
  901. return channels[i].GetPriority() > channels[j].GetPriority()
  902. })
  903. }
  904. }
  905. }
  906. func buildEnabledModelsBySet(
  907. modelMapBySet map[string]map[string][]*Channel,
  908. modelConfigCache ModelConfigCache,
  909. ) (
  910. map[string][]string,
  911. map[string][]ModelConfig,
  912. map[string]ModelConfig,
  913. ) {
  914. modelsBySet := make(map[string][]string)
  915. modelConfigsBySet := make(map[string][]ModelConfig)
  916. modelConfigsMap := make(map[string]ModelConfig)
  917. for set, modelMap := range modelMapBySet {
  918. models := make([]string, 0)
  919. configs := make([]ModelConfig, 0)
  920. appended := make(map[string]struct{})
  921. for model := range modelMap {
  922. if _, ok := appended[model]; ok {
  923. continue
  924. }
  925. if config, ok := modelConfigCache.GetModelConfig(model); ok {
  926. models = append(models, model)
  927. configs = append(configs, config)
  928. appended[model] = struct{}{}
  929. modelConfigsMap[model] = config
  930. }
  931. }
  932. slices.Sort(models)
  933. slices.SortStableFunc(configs, SortModelConfigsFunc)
  934. modelsBySet[set] = models
  935. modelConfigsBySet[set] = configs
  936. }
  937. return modelsBySet, modelConfigsBySet, modelConfigsMap
  938. }
  939. func SortModelConfigsFunc(i, j ModelConfig) int {
  940. if i.Owner != j.Owner {
  941. if natural.Less(string(i.Owner), string(j.Owner)) {
  942. return -1
  943. }
  944. return 1
  945. }
  946. if i.Type != j.Type {
  947. if i.Type < j.Type {
  948. return -1
  949. }
  950. return 1
  951. }
  952. if i.Model == j.Model {
  953. return 0
  954. }
  955. if natural.Less(i.Model, j.Model) {
  956. return -1
  957. }
  958. return 1
  959. }
  960. func SyncModelConfigAndChannelCache(
  961. ctx context.Context,
  962. wg *sync.WaitGroup,
  963. frequency time.Duration,
  964. ) {
  965. defer wg.Done()
  966. ticker := time.NewTicker(frequency)
  967. defer ticker.Stop()
  968. for {
  969. select {
  970. case <-ctx.Done():
  971. return
  972. case <-ticker.C:
  973. err := InitModelConfigAndChannelCache()
  974. if err != nil {
  975. notify.ErrorThrottle(
  976. "syncModelChannel",
  977. time.Minute*5,
  978. "failed to sync channels",
  979. err.Error(),
  980. )
  981. }
  982. }
  983. }
  984. }