Make BytesSyncLogger's implementation details internal.

Provide NewBytesSyncLogger that returns an opaque data structure.
Automatically start up the logging loop goroutine in NewBytesSyncLogger.
This commit is contained in:
David Fifield 2020-04-23 21:20:16 -06:00
parent 9a4e3e7bd9
commit 2853fc9362
2 changed files with 29 additions and 33 deletions

View file

@ -10,7 +10,6 @@ const (
) )
type BytesLogger interface { type BytesLogger interface {
Log()
AddOutbound(int) AddOutbound(int)
AddInbound(int) AddInbound(int)
} }
@ -18,50 +17,55 @@ type BytesLogger interface {
// Default BytesLogger does nothing. // Default BytesLogger does nothing.
type BytesNullLogger struct{} type BytesNullLogger struct{}
func (b BytesNullLogger) Log() {}
func (b BytesNullLogger) AddOutbound(amount int) {} func (b BytesNullLogger) AddOutbound(amount int) {}
func (b BytesNullLogger) AddInbound(amount int) {} func (b BytesNullLogger) AddInbound(amount int) {}
// BytesSyncLogger uses channels to safely log from multiple sources with output // BytesSyncLogger uses channels to safely log from multiple sources with output
// occuring at reasonable intervals. // occuring at reasonable intervals.
type BytesSyncLogger struct { type BytesSyncLogger struct {
OutboundChan chan int outboundChan chan int
InboundChan chan int inboundChan chan int
Outbound int
Inbound int
OutEvents int
InEvents int
} }
func (b *BytesSyncLogger) Log() { // NewBytesSyncLogger returns a new BytesSyncLogger and starts it loggin.
var amount int func NewBytesSyncLogger() *BytesSyncLogger {
b := &BytesSyncLogger{
outboundChan: make(chan int, 5),
inboundChan: make(chan int, 5),
}
go b.log()
return b
}
func (b *BytesSyncLogger) log() {
var outbound, inbound, outEvents, inEvents int
output := func() { output := func() {
log.Printf("Traffic Bytes (in|out): %d | %d -- (%d OnMessages, %d Sends)", log.Printf("Traffic Bytes (in|out): %d | %d -- (%d OnMessages, %d Sends)",
b.Inbound, b.Outbound, b.InEvents, b.OutEvents) inbound, outbound, inEvents, outEvents)
b.Outbound = 0 outbound = 0
b.OutEvents = 0 outEvents = 0
b.Inbound = 0 inbound = 0
b.InEvents = 0 inEvents = 0
} }
last := time.Now() last := time.Now()
for { for {
select { select {
case amount = <-b.OutboundChan: case amount := <-b.outboundChan:
b.Outbound += amount outbound += amount
b.OutEvents++ outEvents++
if time.Since(last) > time.Second*LogTimeInterval { if time.Since(last) > time.Second*LogTimeInterval {
last = time.Now() last = time.Now()
output() output()
} }
case amount = <-b.InboundChan: case amount := <-b.inboundChan:
b.Inbound += amount inbound += amount
b.InEvents++ inEvents++
if time.Since(last) > time.Second*LogTimeInterval { if time.Since(last) > time.Second*LogTimeInterval {
last = time.Now() last = time.Now()
output() output()
} }
case <-time.After(time.Second * LogTimeInterval): case <-time.After(time.Second * LogTimeInterval):
if b.InEvents > 0 || b.OutEvents > 0 { if inEvents > 0 || outEvents > 0 {
output() output()
} }
} }
@ -69,9 +73,9 @@ func (b *BytesSyncLogger) Log() {
} }
func (b *BytesSyncLogger) AddOutbound(amount int) { func (b *BytesSyncLogger) AddOutbound(amount int) {
b.OutboundChan <- amount b.outboundChan <- amount
} }
func (b *BytesSyncLogger) AddInbound(amount int) { func (b *BytesSyncLogger) AddInbound(amount int) {
b.InboundChan <- amount b.inboundChan <- amount
} }

View file

@ -161,15 +161,7 @@ func main() {
snowflakes.Tongue = sf.NewWebRTCDialer(broker, iceServers) snowflakes.Tongue = sf.NewWebRTCDialer(broker, iceServers)
// Use a real logger to periodically output how much traffic is happening. // Use a real logger to periodically output how much traffic is happening.
snowflakes.BytesLogger = &sf.BytesSyncLogger{ snowflakes.BytesLogger = sf.NewBytesSyncLogger()
InboundChan: make(chan int, 5),
OutboundChan: make(chan int, 5),
Inbound: 0,
Outbound: 0,
InEvents: 0,
OutEvents: 0,
}
go snowflakes.BytesLogger.Log()
go ConnectLoop(snowflakes) go ConnectLoop(snowflakes)