summaryrefslogtreecommitdiff
path: root/pkg/snowflake/lib/util.go
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2021-11-29 18:12:40 +0100
committerkali kaneko (leap communications) <kali@leap.se>2021-11-29 18:14:21 +0100
commitb25ec7c923924e53ddb65f9a34e9a669dcf066c7 (patch)
tree5ddabd18ad1cb880ee07ae8c950b99b99f150012 /pkg/snowflake/lib/util.go
parentc7148d9559dab0e1cdbc6dd5306a3c852615560e (diff)
[feat] snowflake client support
Diffstat (limited to 'pkg/snowflake/lib/util.go')
-rw-r--r--pkg/snowflake/lib/util.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/pkg/snowflake/lib/util.go b/pkg/snowflake/lib/util.go
new file mode 100644
index 0000000..0eb8ddd
--- /dev/null
+++ b/pkg/snowflake/lib/util.go
@@ -0,0 +1,70 @@
+package lib
+
+import (
+ "log"
+ "time"
+)
+
+const (
+ LogTimeInterval = 5 * time.Second
+)
+
+type BytesLogger interface {
+ AddOutbound(int)
+ AddInbound(int)
+}
+
+// Default BytesLogger does nothing.
+type BytesNullLogger struct{}
+
+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
+}
+
+// NewBytesSyncLogger returns a new BytesSyncLogger and starts it loggin.
+func NewBytesSyncLogger() *BytesSyncLogger {
+ b := &BytesSyncLogger{
+ outboundChan: make(chan int, 5),
+ inboundChan: make(chan int, 5),
+ }
+ go b.log()
+ return b
+}
+
+func (b *BytesSyncLogger) log() {
+ var outbound, inbound, outEvents, inEvents int
+ 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++
+ case amount := <-b.inboundChan:
+ inbound += amount
+ inEvents++
+ }
+ }
+}
+
+func (b *BytesSyncLogger) AddOutbound(amount int) {
+ b.outboundChan <- amount
+}
+
+func (b *BytesSyncLogger) AddInbound(amount int) {
+ b.inboundChan <- amount
+}