mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-10-14 06:20:27 +00:00
feat: able to disable channel by success rate
This commit is contained in:
46
monitor/channel.go
Normal file
46
monitor/channel.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/songquanpeng/one-api/common"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
"github.com/songquanpeng/one-api/model"
|
||||
)
|
||||
|
||||
func notifyRootUser(subject string, content string) {
|
||||
if config.RootUserEmail == "" {
|
||||
config.RootUserEmail = model.GetRootUserEmail()
|
||||
}
|
||||
err := common.SendEmail(subject, config.RootUserEmail, content)
|
||||
if err != nil {
|
||||
logger.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
// DisableChannel disable & notify
|
||||
func DisableChannel(channelId int, channelName string, reason string) {
|
||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
||||
logger.SysLog(fmt.Sprintf("channel #%d has been disabled: %s", channelId, reason))
|
||||
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
||||
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
||||
notifyRootUser(subject, content)
|
||||
}
|
||||
|
||||
func MetricDisableChannel(channelId int, successRate float64) {
|
||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
||||
logger.SysLog(fmt.Sprintf("channel #%d has been disabled due to low success rate: %.2f", channelId, successRate*100))
|
||||
subject := fmt.Sprintf("通道 #%d 已被禁用", channelId)
|
||||
content := fmt.Sprintf("该渠道在最近 %d 次调用中成功率为 %.2f%%,低于阈值 %.2f%%,因此被系统自动禁用。",
|
||||
config.MetricQueueSize, successRate*100, config.MetricSuccessRateThreshold*100)
|
||||
notifyRootUser(subject, content)
|
||||
}
|
||||
|
||||
// EnableChannel enable & notify
|
||||
func EnableChannel(channelId int, channelName string) {
|
||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
|
||||
logger.SysLog(fmt.Sprintf("channel #%d has been enabled", channelId))
|
||||
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||||
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||||
notifyRootUser(subject, content)
|
||||
}
|
79
monitor/metric.go
Normal file
79
monitor/metric.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package monitor
|
||||
|
||||
import (
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
)
|
||||
|
||||
var store = make(map[int][]bool)
|
||||
var metricSuccessChan = make(chan int, config.MetricSuccessChanSize)
|
||||
var metricFailChan = make(chan int, config.MetricFailChanSize)
|
||||
|
||||
func consumeSuccess(channelId int) {
|
||||
if len(store[channelId]) > config.MetricQueueSize {
|
||||
store[channelId] = store[channelId][1:]
|
||||
}
|
||||
store[channelId] = append(store[channelId], true)
|
||||
}
|
||||
|
||||
func consumeFail(channelId int) (bool, float64) {
|
||||
if len(store[channelId]) > config.MetricQueueSize {
|
||||
store[channelId] = store[channelId][1:]
|
||||
}
|
||||
store[channelId] = append(store[channelId], false)
|
||||
successCount := 0
|
||||
for _, success := range store[channelId] {
|
||||
if success {
|
||||
successCount++
|
||||
}
|
||||
}
|
||||
successRate := float64(successCount) / float64(len(store[channelId]))
|
||||
if len(store[channelId]) < config.MetricQueueSize {
|
||||
return false, successRate
|
||||
}
|
||||
if successRate < config.MetricSuccessRateThreshold {
|
||||
store[channelId] = make([]bool, 0)
|
||||
return true, successRate
|
||||
}
|
||||
return false, successRate
|
||||
}
|
||||
|
||||
func metricSuccessConsumer() {
|
||||
for {
|
||||
select {
|
||||
case channelId := <-metricSuccessChan:
|
||||
consumeSuccess(channelId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func metricFailConsumer() {
|
||||
for {
|
||||
select {
|
||||
case channelId := <-metricFailChan:
|
||||
disable, successRate := consumeFail(channelId)
|
||||
if disable {
|
||||
go MetricDisableChannel(channelId, successRate)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
if config.EnableMetric {
|
||||
go metricSuccessConsumer()
|
||||
go metricFailConsumer()
|
||||
}
|
||||
}
|
||||
|
||||
func Emit(channelId int, success bool) {
|
||||
if !config.EnableMetric {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
if success {
|
||||
metricSuccessChan <- channelId
|
||||
} else {
|
||||
metricFailChan <- channelId
|
||||
}
|
||||
}()
|
||||
}
|
Reference in New Issue
Block a user