Move creation of periodic stats task inside proxy library

This adds a new type of SnowflakeEvent. EventOnProxyStats is triggered
by the periodic task run at SummaryInterval and produces an event with a
proxy stats output string.
This commit is contained in:
Cecylia Bocovich 2023-10-28 16:04:09 -04:00
parent 83a7422fe6
commit 354cb65432
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90
4 changed files with 54 additions and 19 deletions

View file

@ -1,54 +1,67 @@
package snowflake_proxy
import (
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/task"
"fmt"
"io"
"log"
"time"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/task"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/event"
)
func NewProxyEventLogger(logPeriod time.Duration, output io.Writer) event.SnowflakeEventReceiver {
func NewProxyEventLogger(output io.Writer) event.SnowflakeEventReceiver {
logger := log.New(output, "", log.LstdFlags|log.LUTC)
el := &logEventLogger{logPeriod: logPeriod, logger: logger}
el.task = &task.Periodic{Interval: logPeriod, Execute: el.logTick}
el.task.WaitThenStart()
return el
return &proxyEventLogger{logger: logger}
}
type logEventLogger struct {
type proxyEventLogger struct {
logger *log.Logger
}
func (p *proxyEventLogger) OnNewSnowflakeEvent(e event.SnowflakeEvent) {
p.logger.Println(e.String())
}
type periodicProxyStats struct {
inboundSum int64
outboundSum int64
connectionCount int
logPeriod time.Duration
task *task.Periodic
logger *log.Logger
dispatcher event.SnowflakeEventDispatcher
}
func (p *logEventLogger) OnNewSnowflakeEvent(e event.SnowflakeEvent) {
func newPeriodicProxyStats(logPeriod time.Duration, dispatcher event.SnowflakeEventDispatcher) *periodicProxyStats {
el := &periodicProxyStats{logPeriod: logPeriod, dispatcher: dispatcher}
el.task = &task.Periodic{Interval: logPeriod, Execute: el.logTick}
el.task.WaitThenStart()
return el
}
func (p *periodicProxyStats) OnNewSnowflakeEvent(e event.SnowflakeEvent) {
switch e.(type) {
case event.EventOnProxyConnectionOver:
e := e.(event.EventOnProxyConnectionOver)
p.inboundSum += e.InboundTraffic
p.outboundSum += e.OutboundTraffic
p.connectionCount += 1
default:
p.logger.Println(e.String())
}
}
func (p *logEventLogger) logTick() error {
func (p *periodicProxyStats) logTick() error {
inbound, inboundUnit := formatTraffic(p.inboundSum)
outbound, outboundUnit := formatTraffic(p.outboundSum)
p.logger.Printf("In the last %v, there were %v connections. Traffic Relayed ↓ %v %v, ↑ %v %v.\n",
statString := fmt.Sprintf("In the last %v, there were %v connections. Traffic Relayed ↓ %v %v, ↑ %v %v.\n",
p.logPeriod.String(), p.connectionCount, inbound, inboundUnit, outbound, outboundUnit)
p.dispatcher.OnNewSnowflakeEvent(&event.EventOnProxyStats{StatString: statString})
p.outboundSum = 0
p.inboundSum = 0
p.connectionCount = 0
return nil
}
func (p *logEventLogger) Close() error {
func (p *periodicProxyStats) Close() error {
return p.task.Close()
}