mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 11:11:30 -04:00
Zero bytesSyncLogger stats after reading them
This also makes the call to GetStat() more thread safe.
This commit is contained in:
parent
939062c7dd
commit
83a7422fe6
1 changed files with 19 additions and 4 deletions
|
@ -27,16 +27,22 @@ func (b bytesNullLogger) GetStat() (in int64, out int64) { return -1, -1 }
|
|||
// occuring at reasonable intervals.
|
||||
type bytesSyncLogger struct {
|
||||
outboundChan, inboundChan chan int64
|
||||
outbound, inbound int64
|
||||
statsChan chan bytesLoggerStats
|
||||
stats bytesLoggerStats
|
||||
outEvents, inEvents int
|
||||
start time.Time
|
||||
}
|
||||
|
||||
type bytesLoggerStats struct {
|
||||
outbound, inbound int64
|
||||
}
|
||||
|
||||
// newBytesSyncLogger returns a new bytesSyncLogger and starts it loggin.
|
||||
func newBytesSyncLogger() *bytesSyncLogger {
|
||||
b := &bytesSyncLogger{
|
||||
outboundChan: make(chan int64, 5),
|
||||
inboundChan: make(chan int64, 5),
|
||||
statsChan: make(chan bytesLoggerStats),
|
||||
}
|
||||
go b.log()
|
||||
b.start = time.Now()
|
||||
|
@ -47,11 +53,16 @@ func (b *bytesSyncLogger) log() {
|
|||
for {
|
||||
select {
|
||||
case amount := <-b.outboundChan:
|
||||
b.outbound += amount
|
||||
b.stats.outbound += amount
|
||||
b.outEvents++
|
||||
case amount := <-b.inboundChan:
|
||||
b.inbound += amount
|
||||
b.stats.inbound += amount
|
||||
b.inEvents++
|
||||
case b.statsChan <- b.stats:
|
||||
b.stats.inbound = 0
|
||||
b.stats.outbound = 0
|
||||
b.inEvents = 0
|
||||
b.outEvents = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,6 +77,10 @@ func (b *bytesSyncLogger) AddInbound(amount int64) {
|
|||
b.inboundChan <- amount
|
||||
}
|
||||
|
||||
func (b *bytesSyncLogger) GetStat() (in int64, out int64) { return b.inbound, b.outbound }
|
||||
// GetStat returns the current inbound and outbound stats from the logger and then zeros the counts
|
||||
func (b *bytesSyncLogger) GetStat() (in int64, out int64) {
|
||||
stats := <-b.statsChan
|
||||
return stats.inbound, stats.outbound
|
||||
}
|
||||
|
||||
func formatTraffic(amount int64) (value int64, unit string) { return amount / 1000, "KB" }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue