From f0dc3ed47bebb9731323c2450a490ac239c8c6d0 Mon Sep 17 00:00:00 2001 From: fatedier Date: Fri, 26 May 2017 02:00:00 +0800 Subject: [PATCH] metric: clear useless proxy statistics data --- server/control.go | 2 +- server/metric.go | 49 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/server/control.go b/server/control.go index 9e772f33..f2ee04af 100644 --- a/server/control.go +++ b/server/control.go @@ -268,7 +268,7 @@ func (ctl *Control) stoper() { for _, pxy := range ctl.proxies { pxy.Close() ctl.svr.DelProxy(pxy.GetName()) - StatsCloseProxy(pxy.GetConf().GetBaseInfo().ProxyType) + StatsCloseProxy(pxy.GetName(), pxy.GetConf().GetBaseInfo().ProxyType) } ctl.allShutdown.Done() diff --git a/server/metric.go b/server/metric.go index 569330ea..ebd94894 100644 --- a/server/metric.go +++ b/server/metric.go @@ -16,8 +16,10 @@ package server import ( "sync" + "time" "github.com/fatedier/frp/models/config" + "github.com/fatedier/frp/utils/log" "github.com/fatedier/frp/utils/metric" ) @@ -46,10 +48,13 @@ type ServerStatistics struct { } type ProxyStatistics struct { - ProxyType string - TrafficIn metric.DateCounter - TrafficOut metric.DateCounter - CurConns metric.Counter + Name string + ProxyType string + TrafficIn metric.DateCounter + TrafficOut metric.DateCounter + CurConns metric.Counter + LastStartTime time.Time + LastCloseTime time.Time } func init() { @@ -63,6 +68,27 @@ func init() { ProxyStatistics: make(map[string]*ProxyStatistics), } + + go func() { + for { + time.Sleep(12 * time.Hour) + log.Debug("start to clear useless proxy statistics data...") + StatsClearUselessInfo() + log.Debug("finish to clear useless proxy statistics data") + } + }() +} + +func StatsClearUselessInfo() { + // To check if there are proxies that closed than 7 days and drop them. + globalStats.mu.Lock() + defer globalStats.mu.Unlock() + for name, data := range globalStats.ProxyStatistics { + if !data.LastCloseTime.IsZero() && time.Since(data.LastCloseTime) > time.Duration(7*24)*time.Hour { + delete(globalStats.ProxyStatistics, name) + log.Trace("clear proxy [%s]'s statistics data, lastCloseTime: [%s]", name, data.LastCloseTime.String()) + } + } } func StatsNewClient() { @@ -91,23 +117,28 @@ func StatsNewProxy(name string, proxyType string) { proxyStats, ok := globalStats.ProxyStatistics[name] if !(ok && proxyStats.ProxyType == proxyType) { proxyStats = &ProxyStatistics{ - ProxyType: proxyType, - CurConns: metric.NewCounter(), - TrafficIn: metric.NewDateCounter(ReserveDays), - TrafficOut: metric.NewDateCounter(ReserveDays), + Name: name, + ProxyType: proxyType, + CurConns: metric.NewCounter(), + TrafficIn: metric.NewDateCounter(ReserveDays), + TrafficOut: metric.NewDateCounter(ReserveDays), + LastStartTime: time.Now(), } globalStats.ProxyStatistics[name] = proxyStats } } } -func StatsCloseProxy(proxyType string) { +func StatsCloseProxy(proxyName string, proxyType string) { if config.ServerCommonCfg.DashboardPort != 0 { globalStats.mu.Lock() defer globalStats.mu.Unlock() if counter, ok := globalStats.ProxyTypeCounts[proxyType]; ok { counter.Dec(1) } + if proxyStats, ok := globalStats.ProxyStatistics[proxyName]; ok { + proxyStats.LastCloseTime = time.Now() + } } }