[tor-commits] [snowflake/main] Extract traffic formatter

shelikhoo at torproject.org shelikhoo at torproject.org
Mon Jan 31 12:04:25 UTC 2022


commit 9208364475bddb186cb9d6fba6508a420ba1734d
Author: Shelikhoo <xiaokangwang at outlook.com>
Date:   Mon Dec 20 14:46:18 2021 +0000

    Extract traffic formatter
---
 proxy/lib/util.go | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/proxy/lib/util.go b/proxy/lib/util.go
index a9ac8aa..c7b3684 100644
--- a/proxy/lib/util.go
+++ b/proxy/lib/util.go
@@ -72,28 +72,28 @@ func (b *bytesSyncLogger) AddInbound(amount int) {
 
 // ThroughputSummary view a formatted summary of the throughput totals
 func (b *bytesSyncLogger) ThroughputSummary() string {
-	var inUnit, outUnit string
-	units := []string{"B", "KB", "MB", "GB"}
-
 	inbound := b.inbound
 	outbound := b.outbound
 
-	for i, u := range units {
-		inUnit = u
-		if (inbound < 1000) || (i == len(units)-1) {
-			break
-		}
-		inbound = inbound / 1000
-	}
-	for i, u := range units {
-		outUnit = u
-		if (outbound < 1000) || (i == len(units)-1) {
-			break
-		}
-		outbound = outbound / 1000
-	}
+	inbound, inUnit := formatTraffic(inbound)
+	outbound, outUnit := formatTraffic(outbound)
+
 	t := time.Now()
 	return fmt.Sprintf("Traffic throughput (up|down): %d %s|%d %s -- (%d OnMessages, %d Sends, over %d seconds)", inbound, inUnit, outbound, outUnit, b.outEvents, b.inEvents, int(t.Sub(b.start).Seconds()))
 }
 
 func (b *bytesSyncLogger) GetStat() (in int, out int) { return b.inbound, b.outbound }
+
+func formatTraffic(amount int) (value int, unit string) {
+	value = amount
+	units := []string{"B", "KB", "MB", "GB"}
+	for i, u := range units {
+		unit = u
+		if (value < 1000) || (i == len(units)-1) {
+			break
+		}
+		value = value / 1000
+	}
+	return
+
+}





More information about the tor-commits mailing list