[tor-commits] [snowflake/master] Simplify BytesSyncLogger.

dcf at torproject.org dcf at torproject.org
Fri Apr 24 16:27:06 UTC 2020


commit 73173cb6987dbf26fdb1036e4b7710c200f87141
Author: David Fifield <david at bamsoftware.com>
Date:   Thu Apr 23 21:26:19 2020 -0600

    Simplify BytesSyncLogger.
---
 client/lib/util.go | 33 +++++++++++----------------------
 1 file changed, 11 insertions(+), 22 deletions(-)

diff --git a/client/lib/util.go b/client/lib/util.go
index 44df031..0eb8ddd 100644
--- a/client/lib/util.go
+++ b/client/lib/util.go
@@ -6,7 +6,7 @@ import (
 )
 
 const (
-	LogTimeInterval = 5
+	LogTimeInterval = 5 * time.Second
 )
 
 type BytesLogger interface {
@@ -39,35 +39,24 @@ func NewBytesSyncLogger() *BytesSyncLogger {
 
 func (b *BytesSyncLogger) log() {
 	var outbound, inbound, outEvents, inEvents int
-	output := func() {
-		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()
+	ticker := time.NewTicker(LogTimeInterval)
 	for {
 		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:
 			outbound += amount
 			outEvents++
-			if time.Since(last) > time.Second*LogTimeInterval {
-				last = time.Now()
-				output()
-			}
 		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 inEvents > 0 || outEvents > 0 {
-				output()
-			}
 		}
 	}
 }



More information about the tor-commits mailing list