Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

1020 rader
30 KiB

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