[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake/master] Make BytesSyncLogger's implementation details internal.



commit 2853fc93628bf997903de33cd7aebd1420e5975f
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Thu Apr 23 21:20:16 2020 -0600

    Make BytesSyncLogger's implementation details internal.
    
    Provide NewBytesSyncLogger that returns an opaque data structure.
    Automatically start up the logging loop goroutine in NewBytesSyncLogger.
---
 client/lib/util.go  | 52 ++++++++++++++++++++++++++++------------------------
 client/snowflake.go | 10 +---------
 2 files changed, 29 insertions(+), 33 deletions(-)

diff --git a/client/lib/util.go b/client/lib/util.go
index 8ac0213..44df031 100644
--- a/client/lib/util.go
+++ b/client/lib/util.go
@@ -10,7 +10,6 @@ const (
 )
 
 type BytesLogger interface {
-	Log()
 	AddOutbound(int)
 	AddInbound(int)
 }
@@ -18,50 +17,55 @@ type BytesLogger interface {
 // Default BytesLogger does nothing.
 type BytesNullLogger struct{}
 
-func (b BytesNullLogger) Log()                   {}
 func (b BytesNullLogger) AddOutbound(amount int) {}
 func (b BytesNullLogger) AddInbound(amount int)  {}
 
 // BytesSyncLogger uses channels to safely log from multiple sources with output
 // occuring at reasonable intervals.
 type BytesSyncLogger struct {
-	OutboundChan chan int
-	InboundChan  chan int
-	Outbound     int
-	Inbound      int
-	OutEvents    int
-	InEvents     int
+	outboundChan chan int
+	inboundChan  chan int
 }
 
-func (b *BytesSyncLogger) Log() {
-	var amount int
+// NewBytesSyncLogger returns a new BytesSyncLogger and starts it loggin.
+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() {
 		log.Printf("Traffic Bytes (in|out): %d | %d -- (%d OnMessages, %d Sends)",
-			b.Inbound, b.Outbound, b.InEvents, b.OutEvents)
-		b.Outbound = 0
-		b.OutEvents = 0
-		b.Inbound = 0
-		b.InEvents = 0
+			inbound, outbound, inEvents, outEvents)
+		outbound = 0
+		outEvents = 0
+		inbound = 0
+		inEvents = 0
 	}
 	last := time.Now()
 	for {
 		select {
-		case amount = <-b.OutboundChan:
-			b.Outbound += amount
-			b.OutEvents++
+		case amount := <-b.outboundChan:
+			outbound += amount
+			outEvents++
 			if time.Since(last) > time.Second*LogTimeInterval {
 				last = time.Now()
 				output()
 			}
-		case amount = <-b.InboundChan:
-			b.Inbound += amount
-			b.InEvents++
+		case amount := <-b.inboundChan:
+			inbound += amount
+			inEvents++
 			if time.Since(last) > time.Second*LogTimeInterval {
 				last = time.Now()
 				output()
 			}
 		case <-time.After(time.Second * LogTimeInterval):
-			if b.InEvents > 0 || b.OutEvents > 0 {
+			if inEvents > 0 || outEvents > 0 {
 				output()
 			}
 		}
@@ -69,9 +73,9 @@ func (b *BytesSyncLogger) Log() {
 }
 
 func (b *BytesSyncLogger) AddOutbound(amount int) {
-	b.OutboundChan <- amount
+	b.outboundChan <- amount
 }
 
 func (b *BytesSyncLogger) AddInbound(amount int) {
-	b.InboundChan <- amount
+	b.inboundChan <- amount
 }
diff --git a/client/snowflake.go b/client/snowflake.go
index dda59ae..d66225d 100644
--- a/client/snowflake.go
+++ b/client/snowflake.go
@@ -161,15 +161,7 @@ func main() {
 	snowflakes.Tongue = sf.NewWebRTCDialer(broker, iceServers)
 
 	// Use a real logger to periodically output how much traffic is happening.
-	snowflakes.BytesLogger = &sf.BytesSyncLogger{
-		InboundChan:  make(chan int, 5),
-		OutboundChan: make(chan int, 5),
-		Inbound:      0,
-		Outbound:     0,
-		InEvents:     0,
-		OutEvents:    0,
-	}
-	go snowflakes.BytesLogger.Log()
+	snowflakes.BytesLogger = sf.NewBytesSyncLogger()
 
 	go ConnectLoop(snowflakes)
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits