github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/client_conn_traffic.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package nodes
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
     7  	teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
     8  	"github.com/TeaOSLab/EdgeNode/internal/events"
     9  	"github.com/TeaOSLab/EdgeNode/internal/goman"
    10  	"github.com/TeaOSLab/EdgeNode/internal/monitor"
    11  	"github.com/iwind/TeaGo/maps"
    12  	"sync/atomic"
    13  	"time"
    14  )
    15  
    16  // 发送监控流量
    17  func init() {
    18  	if !teaconst.IsMain {
    19  		return
    20  	}
    21  
    22  	events.On(events.EventStart, func() {
    23  		var ticker = time.NewTicker(1 * time.Minute)
    24  		goman.New(func() {
    25  			for range ticker.C {
    26  				// 加入到数据队列中
    27  				var inBytes = atomic.LoadUint64(&teaconst.InTrafficBytes)
    28  				atomic.StoreUint64(&teaconst.InTrafficBytes, 0) // 重置数据
    29  				if inBytes > 0 {
    30  					monitor.SharedValueQueue.Add(nodeconfigs.NodeValueItemTrafficIn, maps.Map{
    31  						"total": inBytes,
    32  					})
    33  				}
    34  
    35  				var outBytes = atomic.LoadUint64(&teaconst.OutTrafficBytes)
    36  				atomic.StoreUint64(&teaconst.OutTrafficBytes, 0) // 重置数据
    37  				if outBytes > 0 {
    38  					monitor.SharedValueQueue.Add(nodeconfigs.NodeValueItemTrafficOut, maps.Map{
    39  						"total": outBytes,
    40  					})
    41  				}
    42  			}
    43  		})
    44  	})
    45  }