channel.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "strings"
  9. "sync"
  10. "github.com/QuantumNous/new-api/common"
  11. "github.com/QuantumNous/new-api/constant"
  12. "github.com/QuantumNous/new-api/dto"
  13. "github.com/QuantumNous/new-api/types"
  14. "github.com/samber/lo"
  15. "gorm.io/gorm"
  16. )
  17. type Channel struct {
  18. Id int `json:"id"`
  19. Type int `json:"type" gorm:"default:0"`
  20. Key string `json:"key" gorm:"not null"`
  21. OpenAIOrganization *string `json:"openai_organization"`
  22. TestModel *string `json:"test_model"`
  23. Status int `json:"status" gorm:"default:1"`
  24. Name string `json:"name" gorm:"index"`
  25. Weight *uint `json:"weight" gorm:"default:0"`
  26. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  27. TestTime int64 `json:"test_time" gorm:"bigint"`
  28. ResponseTime int `json:"response_time"` // in milliseconds
  29. BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"`
  30. Other string `json:"other"`
  31. Balance float64 `json:"balance"` // in USD
  32. BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
  33. Models string `json:"models"`
  34. Group string `json:"group" gorm:"type:varchar(64);default:'default'"`
  35. UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
  36. ModelMapping *string `json:"model_mapping" gorm:"type:text"`
  37. //MaxInputTokens *int `json:"max_input_tokens" gorm:"default:0"`
  38. StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
  39. Priority *int64 `json:"priority" gorm:"bigint;default:0"`
  40. AutoBan *int `json:"auto_ban" gorm:"default:1"`
  41. OtherInfo string `json:"other_info"`
  42. Tag *string `json:"tag" gorm:"index"`
  43. Setting *string `json:"setting" gorm:"type:text"` // 渠道额外设置
  44. ParamOverride *string `json:"param_override" gorm:"type:text"`
  45. HeaderOverride *string `json:"header_override" gorm:"type:text"`
  46. Remark *string `json:"remark" gorm:"type:varchar(255)" validate:"max=255"`
  47. // add after v0.8.5
  48. ChannelInfo ChannelInfo `json:"channel_info" gorm:"type:json"`
  49. OtherSettings string `json:"settings" gorm:"column:settings"` // 其他设置,存储azure版本等不需要检索的信息,详见dto.ChannelOtherSettings
  50. // cache info
  51. Keys []string `json:"-" gorm:"-"`
  52. }
  53. type ChannelInfo struct {
  54. IsMultiKey bool `json:"is_multi_key"` // 是否多Key模式
  55. MultiKeySize int `json:"multi_key_size"` // 多Key模式下的Key数量
  56. MultiKeyStatusList map[int]int `json:"multi_key_status_list"` // key状态列表,key index -> status
  57. MultiKeyDisabledReason map[int]string `json:"multi_key_disabled_reason,omitempty"` // key禁用原因列表,key index -> reason
  58. MultiKeyDisabledTime map[int]int64 `json:"multi_key_disabled_time,omitempty"` // key禁用时间列表,key index -> time
  59. MultiKeyPollingIndex int `json:"multi_key_polling_index"` // 多Key模式下轮询的key索引
  60. MultiKeyMode constant.MultiKeyMode `json:"multi_key_mode"`
  61. }
  62. // Value implements driver.Valuer interface
  63. func (c ChannelInfo) Value() (driver.Value, error) {
  64. return common.Marshal(&c)
  65. }
  66. // Scan implements sql.Scanner interface
  67. func (c *ChannelInfo) Scan(value interface{}) error {
  68. bytesValue, _ := value.([]byte)
  69. return common.Unmarshal(bytesValue, c)
  70. }
  71. func (channel *Channel) GetKeys() []string {
  72. if channel.Key == "" {
  73. return []string{}
  74. }
  75. if len(channel.Keys) > 0 {
  76. return channel.Keys
  77. }
  78. trimmed := strings.TrimSpace(channel.Key)
  79. // If the key starts with '[', try to parse it as a JSON array (e.g., for Vertex AI scenarios)
  80. if strings.HasPrefix(trimmed, "[") {
  81. var arr []json.RawMessage
  82. if err := common.Unmarshal([]byte(trimmed), &arr); err == nil {
  83. res := make([]string, len(arr))
  84. for i, v := range arr {
  85. res[i] = string(v)
  86. }
  87. return res
  88. }
  89. }
  90. // Otherwise, fall back to splitting by newline
  91. keys := strings.Split(strings.Trim(channel.Key, "\n"), "\n")
  92. return keys
  93. }
  94. func (channel *Channel) GetNextEnabledKey() (string, int, *types.NewAPIError) {
  95. // If not in multi-key mode, return the original key string directly.
  96. if !channel.ChannelInfo.IsMultiKey {
  97. return channel.Key, 0, nil
  98. }
  99. // Obtain all keys (split by \n)
  100. keys := channel.GetKeys()
  101. if len(keys) == 0 {
  102. // No keys available, return error, should disable the channel
  103. return "", 0, types.NewError(errors.New("no keys available"), types.ErrorCodeChannelNoAvailableKey)
  104. }
  105. lock := GetChannelPollingLock(channel.Id)
  106. lock.Lock()
  107. defer lock.Unlock()
  108. statusList := channel.ChannelInfo.MultiKeyStatusList
  109. // helper to get key status, default to enabled when missing
  110. getStatus := func(idx int) int {
  111. if statusList == nil {
  112. return common.ChannelStatusEnabled
  113. }
  114. if status, ok := statusList[idx]; ok {
  115. return status
  116. }
  117. return common.ChannelStatusEnabled
  118. }
  119. // Collect indexes of enabled keys
  120. enabledIdx := make([]int, 0, len(keys))
  121. for i := range keys {
  122. if getStatus(i) == common.ChannelStatusEnabled {
  123. enabledIdx = append(enabledIdx, i)
  124. }
  125. }
  126. // If no specific status list or none enabled, fall back to first key
  127. if len(enabledIdx) == 0 {
  128. return keys[0], 0, nil
  129. }
  130. switch channel.ChannelInfo.MultiKeyMode {
  131. case constant.MultiKeyModeRandom:
  132. // Randomly pick one enabled key
  133. selectedIdx := enabledIdx[rand.Intn(len(enabledIdx))]
  134. return keys[selectedIdx], selectedIdx, nil
  135. case constant.MultiKeyModePolling:
  136. // Use channel-specific lock to ensure thread-safe polling
  137. channelInfo, err := CacheGetChannelInfo(channel.Id)
  138. if err != nil {
  139. return "", 0, types.NewError(err, types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
  140. }
  141. //println("before polling index:", channel.ChannelInfo.MultiKeyPollingIndex)
  142. defer func() {
  143. if common.DebugEnabled {
  144. println(fmt.Sprintf("channel %d polling index: %d", channel.Id, channel.ChannelInfo.MultiKeyPollingIndex))
  145. }
  146. if !common.MemoryCacheEnabled {
  147. _ = channel.SaveChannelInfo()
  148. } else {
  149. // CacheUpdateChannel(channel)
  150. }
  151. }()
  152. // Start from the saved polling index and look for the next enabled key
  153. start := channelInfo.MultiKeyPollingIndex
  154. if start < 0 || start >= len(keys) {
  155. start = 0
  156. }
  157. for i := 0; i < len(keys); i++ {
  158. idx := (start + i) % len(keys)
  159. if getStatus(idx) == common.ChannelStatusEnabled {
  160. // update polling index for next call (point to the next position)
  161. channel.ChannelInfo.MultiKeyPollingIndex = (idx + 1) % len(keys)
  162. return keys[idx], idx, nil
  163. }
  164. }
  165. // Fallback – should not happen, but return first enabled key
  166. return keys[enabledIdx[0]], enabledIdx[0], nil
  167. default:
  168. // Unknown mode, default to first enabled key (or original key string)
  169. return keys[enabledIdx[0]], enabledIdx[0], nil
  170. }
  171. }
  172. func (channel *Channel) SaveChannelInfo() error {
  173. return DB.Model(channel).Update("channel_info", channel.ChannelInfo).Error
  174. }
  175. func (channel *Channel) GetModels() []string {
  176. if channel.Models == "" {
  177. return []string{}
  178. }
  179. return strings.Split(strings.Trim(channel.Models, ","), ",")
  180. }
  181. func (channel *Channel) GetGroups() []string {
  182. if channel.Group == "" {
  183. return []string{}
  184. }
  185. groups := strings.Split(strings.Trim(channel.Group, ","), ",")
  186. for i, group := range groups {
  187. groups[i] = strings.TrimSpace(group)
  188. }
  189. return groups
  190. }
  191. func (channel *Channel) GetOtherInfo() map[string]interface{} {
  192. otherInfo := make(map[string]interface{})
  193. if channel.OtherInfo != "" {
  194. err := common.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
  195. if err != nil {
  196. common.SysLog(fmt.Sprintf("failed to unmarshal other info: channel_id=%d, tag=%s, name=%s, error=%v", channel.Id, channel.GetTag(), channel.Name, err))
  197. }
  198. }
  199. return otherInfo
  200. }
  201. func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
  202. otherInfoBytes, err := json.Marshal(otherInfo)
  203. if err != nil {
  204. common.SysLog(fmt.Sprintf("failed to marshal other info: channel_id=%d, tag=%s, name=%s, error=%v", channel.Id, channel.GetTag(), channel.Name, err))
  205. return
  206. }
  207. channel.OtherInfo = string(otherInfoBytes)
  208. }
  209. func (channel *Channel) GetTag() string {
  210. if channel.Tag == nil {
  211. return ""
  212. }
  213. return *channel.Tag
  214. }
  215. func (channel *Channel) SetTag(tag string) {
  216. channel.Tag = &tag
  217. }
  218. func (channel *Channel) GetAutoBan() bool {
  219. if channel.AutoBan == nil {
  220. return false
  221. }
  222. return *channel.AutoBan == 1
  223. }
  224. func (channel *Channel) Save() error {
  225. return DB.Save(channel).Error
  226. }
  227. func (channel *Channel) SaveWithoutKey() error {
  228. return DB.Omit("key").Save(channel).Error
  229. }
  230. func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
  231. var channels []*Channel
  232. var err error
  233. order := "priority desc"
  234. if idSort {
  235. order = "id desc"
  236. }
  237. if selectAll {
  238. err = DB.Order(order).Find(&channels).Error
  239. } else {
  240. err = DB.Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  241. }
  242. return channels, err
  243. }
  244. func GetChannelsByTag(tag string, idSort bool) ([]*Channel, error) {
  245. var channels []*Channel
  246. order := "priority desc"
  247. if idSort {
  248. order = "id desc"
  249. }
  250. err := DB.Where("tag = ?", tag).Order(order).Find(&channels).Error
  251. return channels, err
  252. }
  253. func SearchChannels(keyword string, group string, model string, idSort bool) ([]*Channel, error) {
  254. var channels []*Channel
  255. modelsCol := "`models`"
  256. // 如果是 PostgreSQL,使用双引号
  257. if common.UsingPostgreSQL {
  258. modelsCol = `"models"`
  259. }
  260. baseURLCol := "`base_url`"
  261. // 如果是 PostgreSQL,使用双引号
  262. if common.UsingPostgreSQL {
  263. baseURLCol = `"base_url"`
  264. }
  265. order := "priority desc"
  266. if idSort {
  267. order = "id desc"
  268. }
  269. // 构造基础查询
  270. baseQuery := DB.Model(&Channel{}).Omit("key")
  271. // 构造WHERE子句
  272. var whereClause string
  273. var args []interface{}
  274. if group != "" && group != "null" {
  275. var groupCondition string
  276. if common.UsingMySQL {
  277. groupCondition = `CONCAT(',', ` + commonGroupCol + `, ',') LIKE ?`
  278. } else {
  279. // sqlite, PostgreSQL
  280. groupCondition = `(',' || ` + commonGroupCol + ` || ',') LIKE ?`
  281. }
  282. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  283. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%", "%,"+group+",%")
  284. } else {
  285. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + " LIKE ?"
  286. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%")
  287. }
  288. // 执行查询
  289. err := baseQuery.Where(whereClause, args...).Order(order).Find(&channels).Error
  290. if err != nil {
  291. return nil, err
  292. }
  293. return channels, nil
  294. }
  295. func GetChannelById(id int, selectAll bool) (*Channel, error) {
  296. channel := &Channel{Id: id}
  297. var err error = nil
  298. if selectAll {
  299. err = DB.First(channel, "id = ?", id).Error
  300. } else {
  301. err = DB.Omit("key").First(channel, "id = ?", id).Error
  302. }
  303. if err != nil {
  304. return nil, err
  305. }
  306. if channel == nil {
  307. return nil, errors.New("channel not found")
  308. }
  309. return channel, nil
  310. }
  311. func BatchInsertChannels(channels []Channel) error {
  312. if len(channels) == 0 {
  313. return nil
  314. }
  315. tx := DB.Begin()
  316. if tx.Error != nil {
  317. return tx.Error
  318. }
  319. defer func() {
  320. if r := recover(); r != nil {
  321. tx.Rollback()
  322. }
  323. }()
  324. for _, chunk := range lo.Chunk(channels, 50) {
  325. if err := tx.Create(&chunk).Error; err != nil {
  326. tx.Rollback()
  327. return err
  328. }
  329. for _, channel_ := range chunk {
  330. if err := channel_.AddAbilities(tx); err != nil {
  331. tx.Rollback()
  332. return err
  333. }
  334. }
  335. }
  336. return tx.Commit().Error
  337. }
  338. func BatchDeleteChannels(ids []int) error {
  339. if len(ids) == 0 {
  340. return nil
  341. }
  342. // 使用事务 分批删除channel表和abilities表
  343. tx := DB.Begin()
  344. if tx.Error != nil {
  345. return tx.Error
  346. }
  347. for _, chunk := range lo.Chunk(ids, 200) {
  348. if err := tx.Where("id in (?)", chunk).Delete(&Channel{}).Error; err != nil {
  349. tx.Rollback()
  350. return err
  351. }
  352. if err := tx.Where("channel_id in (?)", chunk).Delete(&Ability{}).Error; err != nil {
  353. tx.Rollback()
  354. return err
  355. }
  356. }
  357. return tx.Commit().Error
  358. }
  359. func (channel *Channel) GetPriority() int64 {
  360. if channel.Priority == nil {
  361. return 0
  362. }
  363. return *channel.Priority
  364. }
  365. func (channel *Channel) GetWeight() int {
  366. if channel.Weight == nil {
  367. return 0
  368. }
  369. return int(*channel.Weight)
  370. }
  371. func (channel *Channel) GetBaseURL() string {
  372. if channel.BaseURL == nil {
  373. return ""
  374. }
  375. url := *channel.BaseURL
  376. if url == "" {
  377. url = constant.ChannelBaseURLs[channel.Type]
  378. }
  379. return url
  380. }
  381. func (channel *Channel) GetModelMapping() string {
  382. if channel.ModelMapping == nil {
  383. return ""
  384. }
  385. return *channel.ModelMapping
  386. }
  387. func (channel *Channel) GetStatusCodeMapping() string {
  388. if channel.StatusCodeMapping == nil {
  389. return ""
  390. }
  391. return *channel.StatusCodeMapping
  392. }
  393. func (channel *Channel) Insert() error {
  394. var err error
  395. err = DB.Create(channel).Error
  396. if err != nil {
  397. return err
  398. }
  399. err = channel.AddAbilities(nil)
  400. return err
  401. }
  402. func (channel *Channel) Update() error {
  403. // If this is a multi-key channel, recalculate MultiKeySize based on the current key list to avoid inconsistency after editing keys
  404. if channel.ChannelInfo.IsMultiKey {
  405. var keyStr string
  406. if channel.Key != "" {
  407. keyStr = channel.Key
  408. } else {
  409. // If key is not provided, read the existing key from the database
  410. if existing, err := GetChannelById(channel.Id, true); err == nil {
  411. keyStr = existing.Key
  412. }
  413. }
  414. // Parse the key list (supports newline separation or JSON array)
  415. keys := []string{}
  416. if keyStr != "" {
  417. trimmed := strings.TrimSpace(keyStr)
  418. if strings.HasPrefix(trimmed, "[") {
  419. var arr []json.RawMessage
  420. if err := common.Unmarshal([]byte(trimmed), &arr); err == nil {
  421. keys = make([]string, len(arr))
  422. for i, v := range arr {
  423. keys[i] = string(v)
  424. }
  425. }
  426. }
  427. if len(keys) == 0 { // fallback to newline split
  428. keys = strings.Split(strings.Trim(keyStr, "\n"), "\n")
  429. }
  430. }
  431. channel.ChannelInfo.MultiKeySize = len(keys)
  432. // Clean up status data that exceeds the new key count to prevent index out of range
  433. if channel.ChannelInfo.MultiKeyStatusList != nil {
  434. for idx := range channel.ChannelInfo.MultiKeyStatusList {
  435. if idx >= channel.ChannelInfo.MultiKeySize {
  436. delete(channel.ChannelInfo.MultiKeyStatusList, idx)
  437. }
  438. }
  439. }
  440. }
  441. var err error
  442. err = DB.Model(channel).Updates(channel).Error
  443. if err != nil {
  444. return err
  445. }
  446. DB.Model(channel).First(channel, "id = ?", channel.Id)
  447. err = channel.UpdateAbilities(nil)
  448. return err
  449. }
  450. func (channel *Channel) UpdateResponseTime(responseTime int64) {
  451. err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
  452. TestTime: common.GetTimestamp(),
  453. ResponseTime: int(responseTime),
  454. }).Error
  455. if err != nil {
  456. common.SysLog(fmt.Sprintf("failed to update response time: channel_id=%d, error=%v", channel.Id, err))
  457. }
  458. }
  459. func (channel *Channel) UpdateBalance(balance float64) {
  460. err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
  461. BalanceUpdatedTime: common.GetTimestamp(),
  462. Balance: balance,
  463. }).Error
  464. if err != nil {
  465. common.SysLog(fmt.Sprintf("failed to update balance: channel_id=%d, error=%v", channel.Id, err))
  466. }
  467. }
  468. func (channel *Channel) Delete() error {
  469. var err error
  470. err = DB.Delete(channel).Error
  471. if err != nil {
  472. return err
  473. }
  474. err = channel.DeleteAbilities()
  475. return err
  476. }
  477. var channelStatusLock sync.Mutex
  478. // channelPollingLocks stores locks for each channel.id to ensure thread-safe polling
  479. var channelPollingLocks sync.Map
  480. // GetChannelPollingLock returns or creates a mutex for the given channel ID
  481. func GetChannelPollingLock(channelId int) *sync.Mutex {
  482. if lock, exists := channelPollingLocks.Load(channelId); exists {
  483. return lock.(*sync.Mutex)
  484. }
  485. // Create new lock for this channel
  486. newLock := &sync.Mutex{}
  487. actual, _ := channelPollingLocks.LoadOrStore(channelId, newLock)
  488. return actual.(*sync.Mutex)
  489. }
  490. // CleanupChannelPollingLocks removes locks for channels that no longer exist
  491. // This is optional and can be called periodically to prevent memory leaks
  492. func CleanupChannelPollingLocks() {
  493. var activeChannelIds []int
  494. DB.Model(&Channel{}).Pluck("id", &activeChannelIds)
  495. activeChannelSet := make(map[int]bool)
  496. for _, id := range activeChannelIds {
  497. activeChannelSet[id] = true
  498. }
  499. channelPollingLocks.Range(func(key, value interface{}) bool {
  500. channelId := key.(int)
  501. if !activeChannelSet[channelId] {
  502. channelPollingLocks.Delete(channelId)
  503. }
  504. return true
  505. })
  506. }
  507. func handlerMultiKeyUpdate(channel *Channel, usingKey string, status int, reason string) {
  508. keys := channel.GetKeys()
  509. if len(keys) == 0 {
  510. channel.Status = status
  511. } else {
  512. var keyIndex int
  513. for i, key := range keys {
  514. if key == usingKey {
  515. keyIndex = i
  516. break
  517. }
  518. }
  519. if channel.ChannelInfo.MultiKeyStatusList == nil {
  520. channel.ChannelInfo.MultiKeyStatusList = make(map[int]int)
  521. }
  522. if status == common.ChannelStatusEnabled {
  523. delete(channel.ChannelInfo.MultiKeyStatusList, keyIndex)
  524. } else {
  525. channel.ChannelInfo.MultiKeyStatusList[keyIndex] = status
  526. if channel.ChannelInfo.MultiKeyDisabledReason == nil {
  527. channel.ChannelInfo.MultiKeyDisabledReason = make(map[int]string)
  528. }
  529. if channel.ChannelInfo.MultiKeyDisabledTime == nil {
  530. channel.ChannelInfo.MultiKeyDisabledTime = make(map[int]int64)
  531. }
  532. channel.ChannelInfo.MultiKeyDisabledReason[keyIndex] = reason
  533. channel.ChannelInfo.MultiKeyDisabledTime[keyIndex] = common.GetTimestamp()
  534. }
  535. if len(channel.ChannelInfo.MultiKeyStatusList) >= channel.ChannelInfo.MultiKeySize {
  536. channel.Status = common.ChannelStatusAutoDisabled
  537. info := channel.GetOtherInfo()
  538. info["status_reason"] = "All keys are disabled"
  539. info["status_time"] = common.GetTimestamp()
  540. channel.SetOtherInfo(info)
  541. }
  542. }
  543. }
  544. func UpdateChannelStatus(channelId int, usingKey string, status int, reason string) bool {
  545. if common.MemoryCacheEnabled {
  546. channelStatusLock.Lock()
  547. defer channelStatusLock.Unlock()
  548. channelCache, _ := CacheGetChannel(channelId)
  549. if channelCache == nil {
  550. return false
  551. }
  552. if channelCache.ChannelInfo.IsMultiKey {
  553. // Use per-channel lock to prevent concurrent map read/write with GetNextEnabledKey
  554. pollingLock := GetChannelPollingLock(channelId)
  555. pollingLock.Lock()
  556. // 如果是多Key模式,更新缓存中的状态
  557. handlerMultiKeyUpdate(channelCache, usingKey, status, reason)
  558. pollingLock.Unlock()
  559. //CacheUpdateChannel(channelCache)
  560. //return true
  561. } else {
  562. // 如果缓存渠道存在,且状态已是目标状态,直接返回
  563. if channelCache.Status == status {
  564. return false
  565. }
  566. CacheUpdateChannelStatus(channelId, status)
  567. }
  568. }
  569. shouldUpdateAbilities := false
  570. defer func() {
  571. if shouldUpdateAbilities {
  572. err := UpdateAbilityStatus(channelId, status == common.ChannelStatusEnabled)
  573. if err != nil {
  574. common.SysLog(fmt.Sprintf("failed to update ability status: channel_id=%d, error=%v", channelId, err))
  575. }
  576. }
  577. }()
  578. channel, err := GetChannelById(channelId, true)
  579. if err != nil {
  580. return false
  581. } else {
  582. if channel.Status == status {
  583. return false
  584. }
  585. if channel.ChannelInfo.IsMultiKey {
  586. beforeStatus := channel.Status
  587. // Protect map writes with the same per-channel lock used by readers
  588. pollingLock := GetChannelPollingLock(channelId)
  589. pollingLock.Lock()
  590. handlerMultiKeyUpdate(channel, usingKey, status, reason)
  591. pollingLock.Unlock()
  592. if beforeStatus != channel.Status {
  593. shouldUpdateAbilities = true
  594. }
  595. } else {
  596. info := channel.GetOtherInfo()
  597. info["status_reason"] = reason
  598. info["status_time"] = common.GetTimestamp()
  599. channel.SetOtherInfo(info)
  600. channel.Status = status
  601. shouldUpdateAbilities = true
  602. }
  603. err = channel.SaveWithoutKey()
  604. if err != nil {
  605. common.SysLog(fmt.Sprintf("failed to update channel status: channel_id=%d, status=%d, error=%v", channel.Id, status, err))
  606. return false
  607. }
  608. }
  609. return true
  610. }
  611. func EnableChannelByTag(tag string) error {
  612. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusEnabled).Error
  613. if err != nil {
  614. return err
  615. }
  616. err = UpdateAbilityStatusByTag(tag, true)
  617. return err
  618. }
  619. func DisableChannelByTag(tag string) error {
  620. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusManuallyDisabled).Error
  621. if err != nil {
  622. return err
  623. }
  624. err = UpdateAbilityStatusByTag(tag, false)
  625. return err
  626. }
  627. func EditChannelByTag(tag string, newTag *string, modelMapping *string, models *string, group *string, priority *int64, weight *uint) error {
  628. updateData := Channel{}
  629. shouldReCreateAbilities := false
  630. updatedTag := tag
  631. // 如果 newTag 不为空且不等于 tag,则更新 tag
  632. if newTag != nil && *newTag != tag {
  633. updateData.Tag = newTag
  634. updatedTag = *newTag
  635. }
  636. if modelMapping != nil && *modelMapping != "" {
  637. updateData.ModelMapping = modelMapping
  638. }
  639. if models != nil && *models != "" {
  640. shouldReCreateAbilities = true
  641. updateData.Models = *models
  642. }
  643. if group != nil && *group != "" {
  644. shouldReCreateAbilities = true
  645. updateData.Group = *group
  646. }
  647. if priority != nil {
  648. updateData.Priority = priority
  649. }
  650. if weight != nil {
  651. updateData.Weight = weight
  652. }
  653. err := DB.Model(&Channel{}).Where("tag = ?", tag).Updates(updateData).Error
  654. if err != nil {
  655. return err
  656. }
  657. if shouldReCreateAbilities {
  658. channels, err := GetChannelsByTag(updatedTag, false)
  659. if err == nil {
  660. for _, channel := range channels {
  661. err = channel.UpdateAbilities(nil)
  662. if err != nil {
  663. common.SysLog(fmt.Sprintf("failed to update abilities: channel_id=%d, tag=%s, error=%v", channel.Id, channel.GetTag(), err))
  664. }
  665. }
  666. }
  667. } else {
  668. err := UpdateAbilityByTag(tag, newTag, priority, weight)
  669. if err != nil {
  670. return err
  671. }
  672. }
  673. return nil
  674. }
  675. func UpdateChannelUsedQuota(id int, quota int) {
  676. if common.BatchUpdateEnabled {
  677. addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
  678. return
  679. }
  680. updateChannelUsedQuota(id, quota)
  681. }
  682. func updateChannelUsedQuota(id int, quota int) {
  683. err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
  684. if err != nil {
  685. common.SysLog(fmt.Sprintf("failed to update channel used quota: channel_id=%d, delta_quota=%d, error=%v", id, quota, err))
  686. }
  687. }
  688. func DeleteChannelByStatus(status int64) (int64, error) {
  689. result := DB.Where("status = ?", status).Delete(&Channel{})
  690. return result.RowsAffected, result.Error
  691. }
  692. func DeleteDisabledChannel() (int64, error) {
  693. result := DB.Where("status = ? or status = ?", common.ChannelStatusAutoDisabled, common.ChannelStatusManuallyDisabled).Delete(&Channel{})
  694. return result.RowsAffected, result.Error
  695. }
  696. func GetPaginatedTags(offset int, limit int) ([]*string, error) {
  697. var tags []*string
  698. err := DB.Model(&Channel{}).Select("DISTINCT tag").Where("tag != ''").Offset(offset).Limit(limit).Find(&tags).Error
  699. return tags, err
  700. }
  701. func SearchTags(keyword string, group string, model string, idSort bool) ([]*string, error) {
  702. var tags []*string
  703. modelsCol := "`models`"
  704. // 如果是 PostgreSQL,使用双引号
  705. if common.UsingPostgreSQL {
  706. modelsCol = `"models"`
  707. }
  708. baseURLCol := "`base_url`"
  709. // 如果是 PostgreSQL,使用双引号
  710. if common.UsingPostgreSQL {
  711. baseURLCol = `"base_url"`
  712. }
  713. order := "priority desc"
  714. if idSort {
  715. order = "id desc"
  716. }
  717. // 构造基础查询
  718. baseQuery := DB.Model(&Channel{}).Omit("key")
  719. // 构造WHERE子句
  720. var whereClause string
  721. var args []interface{}
  722. if group != "" && group != "null" {
  723. var groupCondition string
  724. if common.UsingMySQL {
  725. groupCondition = `CONCAT(',', ` + commonGroupCol + `, ',') LIKE ?`
  726. } else {
  727. // sqlite, PostgreSQL
  728. groupCondition = `(',' || ` + commonGroupCol + ` || ',') LIKE ?`
  729. }
  730. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  731. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%", "%,"+group+",%")
  732. } else {
  733. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + " LIKE ?"
  734. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%")
  735. }
  736. subQuery := baseQuery.Where(whereClause, args...).
  737. Select("tag").
  738. Where("tag != ''").
  739. Order(order)
  740. err := DB.Table("(?) as sub", subQuery).
  741. Select("DISTINCT tag").
  742. Find(&tags).Error
  743. if err != nil {
  744. return nil, err
  745. }
  746. return tags, nil
  747. }
  748. func (channel *Channel) ValidateSettings() error {
  749. channelParams := &dto.ChannelSettings{}
  750. if channel.Setting != nil && *channel.Setting != "" {
  751. err := common.Unmarshal([]byte(*channel.Setting), channelParams)
  752. if err != nil {
  753. return err
  754. }
  755. }
  756. return nil
  757. }
  758. func (channel *Channel) GetSetting() dto.ChannelSettings {
  759. setting := dto.ChannelSettings{}
  760. if channel.Setting != nil && *channel.Setting != "" {
  761. err := common.Unmarshal([]byte(*channel.Setting), &setting)
  762. if err != nil {
  763. common.SysLog(fmt.Sprintf("failed to unmarshal setting: channel_id=%d, error=%v", channel.Id, err))
  764. channel.Setting = nil // 清空设置以避免后续错误
  765. _ = channel.Save() // 保存修改
  766. }
  767. }
  768. return setting
  769. }
  770. func (channel *Channel) SetSetting(setting dto.ChannelSettings) {
  771. settingBytes, err := common.Marshal(setting)
  772. if err != nil {
  773. common.SysLog(fmt.Sprintf("failed to marshal setting: channel_id=%d, error=%v", channel.Id, err))
  774. return
  775. }
  776. channel.Setting = common.GetPointer[string](string(settingBytes))
  777. }
  778. func (channel *Channel) GetOtherSettings() dto.ChannelOtherSettings {
  779. setting := dto.ChannelOtherSettings{}
  780. if channel.OtherSettings != "" {
  781. err := common.UnmarshalJsonStr(channel.OtherSettings, &setting)
  782. if err != nil {
  783. common.SysLog(fmt.Sprintf("failed to unmarshal setting: channel_id=%d, error=%v", channel.Id, err))
  784. channel.OtherSettings = "{}" // 清空设置以避免后续错误
  785. _ = channel.Save() // 保存修改
  786. }
  787. }
  788. return setting
  789. }
  790. func (channel *Channel) SetOtherSettings(setting dto.ChannelOtherSettings) {
  791. settingBytes, err := common.Marshal(setting)
  792. if err != nil {
  793. common.SysLog(fmt.Sprintf("failed to marshal setting: channel_id=%d, error=%v", channel.Id, err))
  794. return
  795. }
  796. channel.OtherSettings = string(settingBytes)
  797. }
  798. func (channel *Channel) GetParamOverride() map[string]interface{} {
  799. paramOverride := make(map[string]interface{})
  800. if channel.ParamOverride != nil && *channel.ParamOverride != "" {
  801. err := common.Unmarshal([]byte(*channel.ParamOverride), &paramOverride)
  802. if err != nil {
  803. common.SysLog(fmt.Sprintf("failed to unmarshal param override: channel_id=%d, error=%v", channel.Id, err))
  804. }
  805. }
  806. return paramOverride
  807. }
  808. func (channel *Channel) GetHeaderOverride() map[string]interface{} {
  809. headerOverride := make(map[string]interface{})
  810. if channel.HeaderOverride != nil && *channel.HeaderOverride != "" {
  811. err := common.Unmarshal([]byte(*channel.HeaderOverride), &headerOverride)
  812. if err != nil {
  813. common.SysLog(fmt.Sprintf("failed to unmarshal header override: channel_id=%d, error=%v", channel.Id, err))
  814. }
  815. }
  816. return headerOverride
  817. }
  818. func GetChannelsByIds(ids []int) ([]*Channel, error) {
  819. var channels []*Channel
  820. err := DB.Where("id in (?)", ids).Find(&channels).Error
  821. return channels, err
  822. }
  823. func BatchSetChannelTag(ids []int, tag *string) error {
  824. // 开启事务
  825. tx := DB.Begin()
  826. if tx.Error != nil {
  827. return tx.Error
  828. }
  829. // 更新标签
  830. err := tx.Model(&Channel{}).Where("id in (?)", ids).Update("tag", tag).Error
  831. if err != nil {
  832. tx.Rollback()
  833. return err
  834. }
  835. // update ability status
  836. channels, err := GetChannelsByIds(ids)
  837. if err != nil {
  838. tx.Rollback()
  839. return err
  840. }
  841. for _, channel := range channels {
  842. err = channel.UpdateAbilities(tx)
  843. if err != nil {
  844. tx.Rollback()
  845. return err
  846. }
  847. }
  848. // 提交事务
  849. return tx.Commit().Error
  850. }
  851. // CountAllChannels returns total channels in DB
  852. func CountAllChannels() (int64, error) {
  853. var total int64
  854. err := DB.Model(&Channel{}).Count(&total).Error
  855. return total, err
  856. }
  857. // CountAllTags returns number of non-empty distinct tags
  858. func CountAllTags() (int64, error) {
  859. var total int64
  860. err := DB.Model(&Channel{}).Where("tag is not null AND tag != ''").Distinct("tag").Count(&total).Error
  861. return total, err
  862. }
  863. // Get channels of specified type with pagination
  864. func GetChannelsByType(startIdx int, num int, idSort bool, channelType int) ([]*Channel, error) {
  865. var channels []*Channel
  866. order := "priority desc"
  867. if idSort {
  868. order = "id desc"
  869. }
  870. err := DB.Where("type = ?", channelType).Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  871. return channels, err
  872. }
  873. // Count channels of specific type
  874. func CountChannelsByType(channelType int) (int64, error) {
  875. var count int64
  876. err := DB.Model(&Channel{}).Where("type = ?", channelType).Count(&count).Error
  877. return count, err
  878. }
  879. // Return map[type]count for all channels
  880. func CountChannelsGroupByType() (map[int64]int64, error) {
  881. type result struct {
  882. Type int64 `gorm:"column:type"`
  883. Count int64 `gorm:"column:count"`
  884. }
  885. var results []result
  886. err := DB.Model(&Channel{}).Select("type, count(*) as count").Group("type").Find(&results).Error
  887. if err != nil {
  888. return nil, err
  889. }
  890. counts := make(map[int64]int64)
  891. for _, r := range results {
  892. counts[r.Type] = r.Count
  893. }
  894. return counts, nil
  895. }