优化错误提示

This commit is contained in:
GoEdgeLab
2021-11-10 21:51:56 +08:00
parent 940517e660
commit 3dd47acb75
15 changed files with 112 additions and 31 deletions

View File

@@ -14,7 +14,7 @@ import (
var SharedActionManager = NewActionManager() var SharedActionManager = NewActionManager()
// 动作管理器定义 // ActionManager 动作管理器定义
type ActionManager struct { type ActionManager struct {
locker sync.Mutex locker sync.Mutex
@@ -23,7 +23,7 @@ type ActionManager struct {
instanceMap map[int64]ActionInterface // id => instance instanceMap map[int64]ActionInterface // id => instance
} }
// 获取动作管理对象 // NewActionManager 获取动作管理对象
func NewActionManager() *ActionManager { func NewActionManager() *ActionManager {
return &ActionManager{ return &ActionManager{
configMap: map[int64]*firewallconfigs.FirewallActionConfig{}, configMap: map[int64]*firewallconfigs.FirewallActionConfig{},
@@ -31,7 +31,7 @@ func NewActionManager() *ActionManager {
} }
} }
// 更新配置 // UpdateActions 更新配置
func (this *ActionManager) UpdateActions(actions []*firewallconfigs.FirewallActionConfig) { func (this *ActionManager) UpdateActions(actions []*firewallconfigs.FirewallActionConfig) {
this.locker.Lock() this.locker.Lock()
defer this.locker.Unlock() defer this.locker.Unlock()
@@ -108,14 +108,14 @@ func (this *ActionManager) UpdateActions(actions []*firewallconfigs.FirewallActi
} }
} }
// 查找事件对应的动作 // FindEventActions 查找事件对应的动作
func (this *ActionManager) FindEventActions(eventLevel string) []ActionInterface { func (this *ActionManager) FindEventActions(eventLevel string) []ActionInterface {
this.locker.Lock() this.locker.Lock()
defer this.locker.Unlock() defer this.locker.Unlock()
return this.eventMap[eventLevel] return this.eventMap[eventLevel]
} }
// 执行添加IP动作 // AddItem 执行添加IP动作
func (this *ActionManager) AddItem(listType IPListType, item *pb.IPItem) { func (this *ActionManager) AddItem(listType IPListType, item *pb.IPItem) {
instances, ok := this.eventMap[item.EventLevel] instances, ok := this.eventMap[item.EventLevel]
if ok { if ok {
@@ -128,7 +128,7 @@ func (this *ActionManager) AddItem(listType IPListType, item *pb.IPItem) {
} }
} }
// 执行删除IP动作 // DeleteItem 执行删除IP动作
func (this *ActionManager) DeleteItem(listType IPListType, item *pb.IPItem) { func (this *ActionManager) DeleteItem(listType IPListType, item *pb.IPItem) {
instances, ok := this.eventMap[item.EventLevel] instances, ok := this.eventMap[item.EventLevel]
if ok { if ok {

View File

@@ -21,7 +21,7 @@ func init() {
// 初始化 // 初始化
library, err := SharedManager.Load() library, err := SharedManager.Load()
if err != nil { if err != nil {
remotelogs.Error("IP_LIBRARY", err.Error()) remotelogs.ErrorObject("IP_LIBRARY", err)
return return
} }
SharedLibrary = library SharedLibrary = library

View File

@@ -46,13 +46,13 @@ func (this *CountryManager) Start() {
// 从缓存中读取 // 从缓存中读取
err := this.load() err := this.load()
if err != nil { if err != nil {
remotelogs.Error("COUNTRY_MANAGER", err.Error()) remotelogs.ErrorObject("COUNTRY_MANAGER", err)
} }
// 第一次更新 // 第一次更新
err = this.loop() err = this.loop()
if err != nil { if err != nil {
remotelogs.Error("COUNTRY_MANAGER", err.Error()) remotelogs.ErrorObject("COUNTRY_MANAGER", err)
} }
// 定时更新 // 定时更新
@@ -63,7 +63,7 @@ func (this *CountryManager) Start() {
for range ticker.C { for range ticker.C {
err := this.loop() err := this.loop()
if err != nil { if err != nil {
remotelogs.Error("COUNTRY_MANAGER", err.Error()) remotelogs.ErrorObject("COUNTRY_MANAGER", err)
} }
} }
} }

View File

@@ -48,7 +48,7 @@ func (this *IPListManager) Start() {
// 第一次读取 // 第一次读取
err := this.loop() err := this.loop()
if err != nil { if err != nil {
remotelogs.Error("IP_LIST_MANAGER", err.Error()) remotelogs.ErrorObject("IP_LIST_MANAGER", err)
} }
ticker := time.NewTicker(60 * time.Second) ticker := time.NewTicker(60 * time.Second)
@@ -65,7 +65,7 @@ func (this *IPListManager) Start() {
if err != nil { if err != nil {
countErrors++ countErrors++
remotelogs.Error("IP_LIST_MANAGER", err.Error()) remotelogs.ErrorObject("IP_LIST_MANAGER", err)
// 连续错误小于3次的我们立即重试 // 连续错误小于3次的我们立即重试
if countErrors <= 3 { if countErrors <= 3 {

View File

@@ -50,13 +50,13 @@ func (this *ProvinceManager) Start() {
// 从缓存中读取 // 从缓存中读取
err := this.load() err := this.load()
if err != nil { if err != nil {
remotelogs.Error("PROVINCE_MANAGER", err.Error()) remotelogs.ErrorObject("PROVINCE_MANAGER", err)
} }
// 第一次更新 // 第一次更新
err = this.loop() err = this.loop()
if err != nil { if err != nil {
remotelogs.Error("PROVINCE_MANAGER", err.Error()) remotelogs.ErrorObject("PROVINCE_MANAGER", err)
} }
// 定时更新 // 定时更新
@@ -67,7 +67,7 @@ func (this *ProvinceManager) Start() {
for range ticker.C { for range ticker.C {
err := this.loop() err := this.loop()
if err != nil { if err != nil {
remotelogs.Error("PROVINCE_MANAGER", err.Error()) remotelogs.ErrorObject("PROVINCE_MANAGER", err)
} }
} }
} }

View File

@@ -38,7 +38,7 @@ func (this *Updater) Start() {
for range ticker.C { for range ticker.C {
err := this.loop() err := this.loop()
if err != nil { if err != nil {
remotelogs.Error("IP_LIBRARY", err.Error()) remotelogs.ErrorObject("IP_LIBRARY", err)
} }
} }
}() }()

View File

@@ -36,7 +36,7 @@ func (this *ValueQueue) Start() {
// 这里单次循环就行因为Loop里已经使用了Range通道 // 这里单次循环就行因为Loop里已经使用了Range通道
err := this.Loop() err := this.Loop()
if err != nil { if err != nil {
remotelogs.Error("MONITOR_QUEUE", err.Error()) remotelogs.ErrorObject("MONITOR_QUEUE", err)
} }
} }
@@ -72,7 +72,11 @@ func (this *ValueQueue) Loop() error {
CreatedAt: value.CreatedAt, CreatedAt: value.CreatedAt,
}) })
if err != nil { if err != nil {
remotelogs.Error("MONITOR", err.Error()) if rpc.IsConnError(err) {
remotelogs.Warn("MONITOR", err.Error())
} else {
remotelogs.Error("MONITOR", err.Error())
}
continue continue
} }
} }

View File

@@ -0,0 +1,28 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package monitor
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeNode/internal/rpc"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/logs"
"google.golang.org/grpc/status"
"testing"
)
func TestValueQueue_RPC(t *testing.T) {
rpcClient, err := rpc.SharedRPC()
if err != nil {
t.Fatal(err)
}
_, err = rpcClient.NodeValueRPC().CreateNodeValue(rpcClient.Context(), &pb.CreateNodeValueRequest{})
if err != nil {
statusErr, ok:= status.FromError(err)
if ok {
logs.Println(statusErr.Code())
}
t.Fatal(err)
}
t.Log("ok")
}

View File

@@ -65,14 +65,14 @@ func (this *TCPListener) handleConn(conn net.Conn) error {
var serverName = tlsConn.ConnectionState().ServerName var serverName = tlsConn.ConnectionState().ServerName
if len(serverName) > 0 { if len(serverName) > 0 {
// 统计 // 统计
stats.SharedTrafficStatManager.Add(firstServer.Id, serverName, 0, 0, 1, 0, 0, 0, firstServer.ShouldCheckTrafficLimit()) stats.SharedTrafficStatManager.Add(firstServer.Id, serverName, 0, 0, 1, 0, 0, 0, firstServer.ShouldCheckTrafficLimit(), firstServer.PlanId())
recordStat = true recordStat = true
} }
} }
// 统计 // 统计
if !recordStat { if !recordStat {
stats.SharedTrafficStatManager.Add(firstServer.Id, "", 0, 0, 1, 0, 0, 0, firstServer.ShouldCheckTrafficLimit()) stats.SharedTrafficStatManager.Add(firstServer.Id, "", 0, 0, 1, 0, 0, 0, firstServer.ShouldCheckTrafficLimit(), firstServer.PlanId())
} }
originConn, err := this.connectOrigin(firstServer.ReverseProxy, conn.RemoteAddr().String()) originConn, err := this.connectOrigin(firstServer.ReverseProxy, conn.RemoteAddr().String())
@@ -126,7 +126,7 @@ func (this *TCPListener) handleConn(conn net.Conn) error {
// 记录流量 // 记录流量
if firstServer != nil { if firstServer != nil {
stats.SharedTrafficStatManager.Add(firstServer.Id, "", int64(n), 0, 0, 0, 0, 0, firstServer.ShouldCheckTrafficLimit()) stats.SharedTrafficStatManager.Add(firstServer.Id, "", int64(n), 0, 0, 0, 0, 0, firstServer.ShouldCheckTrafficLimit(), firstServer.PlanId())
} }
} }
if err != nil { if err != nil {

View File

@@ -185,7 +185,7 @@ func NewUDPConn(server *serverconfigs.ServerConfig, addr net.Addr, proxyConn *ne
// 统计 // 统计
if server != nil { if server != nil {
stats.SharedTrafficStatManager.Add(server.Id, "", 0, 0, 1, 0, 0, 0, server.ShouldCheckTrafficLimit()) stats.SharedTrafficStatManager.Add(server.Id, "", 0, 0, 1, 0, 0, 0, server.ShouldCheckTrafficLimit(), server.PlanId())
} }
go func() { go func() {
@@ -206,7 +206,7 @@ func NewUDPConn(server *serverconfigs.ServerConfig, addr net.Addr, proxyConn *ne
// 记录流量 // 记录流量
if server != nil { if server != nil {
stats.SharedTrafficStatManager.Add(server.Id, "", int64(n), 0, 0, 0, 0, 0, server.ShouldCheckTrafficLimit()) stats.SharedTrafficStatManager.Add(server.Id, "", int64(n), 0, 0, 0, 0, 0, server.ShouldCheckTrafficLimit(), server.PlanId())
} }
} }
if err != nil { if err != nil {

View File

@@ -101,7 +101,11 @@ func (this *NodeStatusExecutor) update() {
StatusJSON: jsonData, StatusJSON: jsonData,
}) })
if err != nil { if err != nil {
remotelogs.Error("NODE_STATUS", "rpc UpdateNodeStatus() failed: "+err.Error()) if rpc.IsConnError(err) {
remotelogs.Warn("NODE_STATUS", "rpc UpdateNodeStatus() failed: "+err.Error())
} else {
remotelogs.Error("NODE_STATUS", "rpc UpdateNodeStatus() failed: "+err.Error())
}
return return
} }
} }

View File

@@ -93,6 +93,18 @@ func Error(tag string, description string) {
} }
} }
// ErrorObject 打印错误对象
func ErrorObject(tag string, err error) {
if err == nil {
return
}
if rpc.IsConnError(err) {
Warn(tag, err.Error())
} else {
Error(tag, err.Error())
}
}
// ServerError 打印服务相关错误信息 // ServerError 打印服务相关错误信息
func ServerError(serverId int64, tag string, description string) { func ServerError(serverId int64, tag string, description string) {
logs.Println("[" + tag + "]" + description) logs.Println("[" + tag + "]" + description)

View File

@@ -2,12 +2,15 @@ package rpc
import ( import (
"github.com/TeaOSLab/EdgeNode/internal/configs" "github.com/TeaOSLab/EdgeNode/internal/configs"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sync" "sync"
) )
var sharedRPC *RPCClient = nil var sharedRPC *RPCClient = nil
var locker = &sync.Mutex{} var locker = &sync.Mutex{}
// SharedRPC RPC对象
func SharedRPC() (*RPCClient, error) { func SharedRPC() (*RPCClient, error) {
locker.Lock() locker.Lock()
defer locker.Unlock() defer locker.Unlock()
@@ -28,3 +31,18 @@ func SharedRPC() (*RPCClient, error) {
sharedRPC = client sharedRPC = client
return sharedRPC, nil return sharedRPC, nil
} }
// IsConnError 是否为连接错误
func IsConnError(err error) bool {
if err == nil {
return false
}
// 检查是否为连接错误
statusErr, ok := status.FromError(err)
if ok {
return statusErr.Code() == codes.Unavailable
}
return false
}

View File

@@ -81,13 +81,21 @@ func (this *HTTPRequestStatManager) Start() {
for range loopTicker.C { for range loopTicker.C {
err := this.Loop() err := this.Loop()
if err != nil { if err != nil {
remotelogs.Error("HTTP_REQUEST_STAT_MANAGER", err.Error()) if rpc.IsConnError(err) {
remotelogs.Warn("HTTP_REQUEST_STAT_MANAGER", err.Error())
} else {
remotelogs.Error("HTTP_REQUEST_STAT_MANAGER", err.Error())
}
} }
select { select {
case <-uploadTicker.C: case <-uploadTicker.C:
err := this.Upload() err := this.Upload()
if err != nil { if err != nil {
remotelogs.Error("HTTP_REQUEST_STAT_MANAGER", "upload failed: "+err.Error()) if !rpc.IsConnError(err) {
remotelogs.Error("HTTP_REQUEST_STAT_MANAGER", "upload failed: "+err.Error())
} else {
remotelogs.Warn("HTTP_REQUEST_STAT_MANAGER", "upload failed: "+err.Error())
}
} }
default: default:
@@ -166,10 +174,10 @@ Loop:
if iplibrary.SharedLibrary != nil { if iplibrary.SharedLibrary != nil {
result, err := iplibrary.SharedLibrary.Lookup(ip) result, err := iplibrary.SharedLibrary.Lookup(ip)
if err == nil && result != nil { if err == nil && result != nil {
this.cityMap[serverId+"@"+result.Country+"@"+result.Province+"@"+result.City] ++ this.cityMap[serverId+"@"+result.Country+"@"+result.Province+"@"+result.City]++
if len(result.ISP) > 0 { if len(result.ISP) > 0 {
this.providerMap[serverId+"@"+result.ISP] ++ this.providerMap[serverId+"@"+result.ISP]++
} }
} }
} }
@@ -197,7 +205,7 @@ Loop:
if dotIndex > -1 { if dotIndex > -1 {
browserVersion = browserVersion[:dotIndex] browserVersion = browserVersion[:dotIndex]
} }
this.browserMap[serverId+"@"+browser+"@"+browserVersion] ++ this.browserMap[serverId+"@"+browser+"@"+browserVersion]++
} }
case firewallRuleGroupString := <-this.firewallRuleGroupChan: case firewallRuleGroupString := <-this.firewallRuleGroupChan:
this.dailyFirewallRuleGroupMap[firewallRuleGroupString]++ this.dailyFirewallRuleGroupMap[firewallRuleGroupString]++

View File

@@ -26,6 +26,7 @@ type TrafficItem struct {
CountCachedRequests int64 CountCachedRequests int64
CountAttackRequests int64 CountAttackRequests int64
AttackBytes int64 AttackBytes int64
PlanId int64
CheckingTrafficLimit bool CheckingTrafficLimit bool
} }
@@ -81,13 +82,17 @@ func (this *TrafficStatManager) Start(configFunc func() *nodeconfigs.NodeConfig)
for range ticker.C { for range ticker.C {
err := this.Upload() err := this.Upload()
if err != nil { if err != nil {
remotelogs.Error("TRAFFIC_STAT_MANAGER", "upload stats failed: "+err.Error()) if !rpc.IsConnError(err) {
remotelogs.Error("TRAFFIC_STAT_MANAGER", "upload stats failed: "+err.Error())
} else {
remotelogs.Warn("TRAFFIC_STAT_MANAGER", "upload stats failed: "+err.Error())
}
} }
} }
} }
// Add 添加流量 // Add 添加流量
func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64, countAttacks int64, attackBytes int64, checkingTrafficLimit bool) { func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64, countAttacks int64, attackBytes int64, checkingTrafficLimit bool, planId int64) {
if bytes == 0 && countRequests == 0 { if bytes == 0 && countRequests == 0 {
return return
} }
@@ -112,6 +117,7 @@ func (this *TrafficStatManager) Add(serverId int64, domain string, bytes int64,
item.CountAttackRequests += countAttacks item.CountAttackRequests += countAttacks
item.AttackBytes += attackBytes item.AttackBytes += attackBytes
item.CheckingTrafficLimit = checkingTrafficLimit item.CheckingTrafficLimit = checkingTrafficLimit
item.PlanId = planId
// 单个域名流量 // 单个域名流量
var domainKey = strconv.FormatInt(timestamp, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + domain var domainKey = strconv.FormatInt(timestamp, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + domain
@@ -171,6 +177,7 @@ func (this *TrafficStatManager) Upload() error {
CountAttackRequests: item.CountAttackRequests, CountAttackRequests: item.CountAttackRequests,
AttackBytes: item.AttackBytes, AttackBytes: item.AttackBytes,
CheckTrafficLimiting: item.CheckingTrafficLimit, CheckTrafficLimiting: item.CheckingTrafficLimit,
PlanId: item.PlanId,
CreatedAt: timestamp, CreatedAt: timestamp,
}) })
} }