Simplify BytesSyncLogger.

This commit is contained in:
David Fifield 2020-04-23 21:26:19 -06:00
parent 2853fc9362
commit 73173cb698

View file

@ -6,7 +6,7 @@ import (
) )
const ( const (
LogTimeInterval = 5 LogTimeInterval = 5 * time.Second
) )
type BytesLogger interface { type BytesLogger interface {
@ -39,35 +39,24 @@ func NewBytesSyncLogger() *BytesSyncLogger {
func (b *BytesSyncLogger) log() { func (b *BytesSyncLogger) log() {
var outbound, inbound, outEvents, inEvents int var outbound, inbound, outEvents, inEvents int
output := func() { ticker := time.NewTicker(LogTimeInterval)
log.Printf("Traffic Bytes (in|out): %d | %d -- (%d OnMessages, %d Sends)",
inbound, outbound, inEvents, outEvents)
outbound = 0
outEvents = 0
inbound = 0
inEvents = 0
}
last := time.Now()
for { for {
select { select {
case <-ticker.C:
if outEvents > 0 || inEvents > 0 {
log.Printf("Traffic Bytes (in|out): %d | %d -- (%d OnMessages, %d Sends)",
inbound, outbound, inEvents, outEvents)
}
outbound = 0
outEvents = 0
inbound = 0
inEvents = 0
case amount := <-b.outboundChan: case amount := <-b.outboundChan:
outbound += amount outbound += amount
outEvents++ outEvents++
if time.Since(last) > time.Second*LogTimeInterval {
last = time.Now()
output()
}
case amount := <-b.inboundChan: case amount := <-b.inboundChan:
inbound += amount inbound += amount
inEvents++ inEvents++
if time.Since(last) > time.Second*LogTimeInterval {
last = time.Now()
output()
}
case <-time.After(time.Second * LogTimeInterval):
if inEvents > 0 || outEvents > 0 {
output()
}
} }
} }
} }