mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
Start refactoring out a client and library
This commit is contained in:
parent
7662ccb00c
commit
cce7ee64a7
8 changed files with 120 additions and 105 deletions
95
client/lib/util.go
Normal file
95
client/lib/util.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/keroserene/go-webrtc"
|
||||
)
|
||||
|
||||
const (
|
||||
LogTimeInterval = 5
|
||||
)
|
||||
|
||||
type IceServerList []webrtc.ConfigurationOption
|
||||
|
||||
func (i *IceServerList) String() string {
|
||||
return fmt.Sprint(*i)
|
||||
}
|
||||
|
||||
type BytesLogger interface {
|
||||
Log()
|
||||
AddOutbound(int)
|
||||
AddInbound(int)
|
||||
}
|
||||
|
||||
// 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
|
||||
IsLogging bool
|
||||
}
|
||||
|
||||
func (b *BytesSyncLogger) Log() {
|
||||
b.IsLogging = true
|
||||
var amount 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
|
||||
}
|
||||
last := time.Now()
|
||||
for {
|
||||
select {
|
||||
case amount = <-b.OutboundChan:
|
||||
b.Outbound += amount
|
||||
b.OutEvents++
|
||||
last := time.Now()
|
||||
if time.Since(last) > time.Second*LogTimeInterval {
|
||||
last = time.Now()
|
||||
output()
|
||||
}
|
||||
case amount = <-b.InboundChan:
|
||||
b.Inbound += amount
|
||||
b.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 {
|
||||
output()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BytesSyncLogger) AddOutbound(amount int) {
|
||||
if !b.IsLogging {
|
||||
return
|
||||
}
|
||||
b.OutboundChan <- amount
|
||||
}
|
||||
|
||||
func (b *BytesSyncLogger) AddInbound(amount int) {
|
||||
if !b.IsLogging {
|
||||
return
|
||||
}
|
||||
b.InboundChan <- amount
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue