mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	进程重启时,自动保存未保存的带宽统计数据到本地文件,以便于在重启后恢复
This commit is contained in:
		@@ -35,11 +35,13 @@ import (
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"os/signal"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"sort"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"syscall"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	// grpc decompression
 | 
			
		||||
@@ -92,6 +94,9 @@ func (this *APINode) Start() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 监听信号
 | 
			
		||||
	this.listenSignals()
 | 
			
		||||
 | 
			
		||||
	// 启动IP库
 | 
			
		||||
	this.setProgress("IP_LIBRARY", "开始初始化IP库")
 | 
			
		||||
	remotelogs.Println("API_NODE", "initializing ip library ...")
 | 
			
		||||
@@ -921,3 +926,16 @@ func (this *APINode) setupTimeZone() {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 监听一些信号
 | 
			
		||||
func (this *APINode) listenSignals() {
 | 
			
		||||
	var queue = make(chan os.Signal, 8)
 | 
			
		||||
	signal.Notify(queue, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT)
 | 
			
		||||
	goman.New(func() {
 | 
			
		||||
		for range queue {
 | 
			
		||||
			events.Notify(events.EventQuit)
 | 
			
		||||
			os.Exit(0)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,16 +4,21 @@ package services
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/db/models"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/events"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/goman"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	timeutil "github.com/iwind/TeaGo/utils/time"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -23,6 +28,29 @@ var serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{} // server key
 | 
			
		||||
var serverBandwidthStatsLocker = &sync.Mutex{}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	// 数据缓存
 | 
			
		||||
	if teaconst.IsMain {
 | 
			
		||||
		var cacheFile = Tea.Root + "/data/server_bandwidth_stats.cache"
 | 
			
		||||
 | 
			
		||||
		{
 | 
			
		||||
			data, err := os.ReadFile(cacheFile)
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				_ = os.Remove(cacheFile)
 | 
			
		||||
				serverBandwidthStatsLocker.Lock()
 | 
			
		||||
				_ = json.Unmarshal(data, &serverBandwidthStatsMap)
 | 
			
		||||
				serverBandwidthStatsLocker.Unlock()
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		events.On(events.EventQuit, func() {
 | 
			
		||||
			serverBandwidthStatsMapJSON, err := json.Marshal(serverBandwidthStatsMap)
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				_ = os.WriteFile(cacheFile, serverBandwidthStatsMapJSON, 0666)
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 定时处理数据
 | 
			
		||||
	var ticker = time.NewTicker(1 * time.Minute)
 | 
			
		||||
	var useTx = true
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user