mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Simplify BytesSyncLogger.
This commit is contained in:
parent
2853fc9362
commit
73173cb698
1 changed files with 11 additions and 22 deletions
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue