| 370 | var testAllChannelsRunning bool = false |
| 371 | |
| 372 | func testAllChannels(notify bool) error { |
| 373 | |
| 374 | testAllChannelsLock.Lock() |
| 375 | if testAllChannelsRunning { |
| 376 | testAllChannelsLock.Unlock() |
| 377 | return errors.New("测试已在运行中") |
| 378 | } |
| 379 | testAllChannelsRunning = true |
| 380 | testAllChannelsLock.Unlock() |
| 381 | channels, getChannelErr := model.GetAllChannels(0, 0, true, false) |
| 382 | if getChannelErr != nil { |
| 383 | return getChannelErr |
| 384 | } |
| 385 | var disableThreshold = int64(common.ChannelDisableThreshold * 1000) |
| 386 | if disableThreshold == 0 { |
| 387 | disableThreshold = 10000000 // a impossible value |
| 388 | } |
| 389 | gopool.Go(func() { |
| 390 | // 使用 defer 确保无论如何都会重置运行状态,防止死锁 |
| 391 | defer func() { |
| 392 | testAllChannelsLock.Lock() |
| 393 | testAllChannelsRunning = false |
| 394 | testAllChannelsLock.Unlock() |
| 395 | }() |
| 396 | |
| 397 | for _, channel := range channels { |
| 398 | isChannelEnabled := channel.Status == common.ChannelStatusEnabled |
| 399 | tik := time.Now() |
| 400 | result := testChannel(channel, "") |
| 401 | tok := time.Now() |
| 402 | milliseconds := tok.Sub(tik).Milliseconds() |
| 403 | |
| 404 | shouldBanChannel := false |
| 405 | newAPIError := result.newAPIError |
| 406 | // request error disables the channel |
| 407 | if newAPIError != nil { |
| 408 | shouldBanChannel = service.ShouldDisableChannel(channel.Type, result.newAPIError) |
| 409 | } |
| 410 | |
| 411 | // 当错误检查通过,才检查响应时间 |
| 412 | if common.AutomaticDisableChannelEnabled && !shouldBanChannel { |
| 413 | if milliseconds > disableThreshold { |
| 414 | err := errors.New(fmt.Sprintf("响应时间 %.2fs 超过阈值 %.2fs", float64(milliseconds)/1000.0, float64(disableThreshold)/1000.0)) |
| 415 | newAPIError = types.NewError(err, types.ErrorCodeChannelResponseTimeExceeded) |
| 416 | shouldBanChannel = true |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // disable channel |
| 421 | if isChannelEnabled && shouldBanChannel && channel.GetAutoBan() { |
| 422 | go processChannelError(result.context, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(result.context, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError) |
| 423 | } |
| 424 | |
| 425 | // enable channel |
| 426 | if !isChannelEnabled && service.ShouldEnableChannel(newAPIError, channel.Status) { |
| 427 | service.EnableChannel(channel.Id, common.GetContextKeyString(result.context, constant.ContextKeyChannelKey), channel.Name) |
| 428 | } |
| 429 | |