Skip to content

Commit

Permalink
metric: clear useless proxy statistics data
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed May 25, 2017
1 parent 08b0885 commit f0dc3ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
49 changes: 40 additions & 9 deletions server/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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()
}
}
}

Expand Down

0 comments on commit f0dc3ed

Please sign in to comment.