Skip to content
Snippets Groups Projects
Commit c2c769ac authored by ale's avatar ale
Browse files

make the BandwidthMonitor exit along with everything else

parent bc86eb31
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
) )
...@@ -43,8 +44,9 @@ type BandwidthMonitor struct { ...@@ -43,8 +44,9 @@ type BandwidthMonitor struct {
counter uint64 counter uint64
stamp time.Time stamp time.Time
period time.Duration period time.Duration
stop chan bool
rate float64 rate float64
lock sync.Mutex
} }
func NewBandwidthMonitor(dev string) *BandwidthMonitor { func NewBandwidthMonitor(dev string) *BandwidthMonitor {
...@@ -52,32 +54,30 @@ func NewBandwidthMonitor(dev string) *BandwidthMonitor { ...@@ -52,32 +54,30 @@ func NewBandwidthMonitor(dev string) *BandwidthMonitor {
device: dev, device: dev,
stamp: time.Now(), stamp: time.Now(),
period: 30 * time.Second, period: 30 * time.Second,
stop: make(chan bool),
} }
go bw.run()
return bw return bw
} }
func (bw *BandwidthMonitor) Close() {
close(bw.stop)
}
func (bw *BandwidthMonitor) GetRate() float64 { func (bw *BandwidthMonitor) GetRate() float64 {
bw.lock.Lock()
defer bw.lock.Unlock()
return bw.rate return bw.rate
} }
func (bw *BandwidthMonitor) run() { func (bw *BandwidthMonitor) Run(stop chan bool) {
t := time.NewTicker(bw.period) t := time.NewTicker(bw.period)
for { for {
select { select {
case <-t.C: case <-t.C:
if c, err := getBytesSentForDevice(bw.device); err == nil { if c, err := getBytesSentForDevice(bw.device); err == nil {
now := time.Now() now := time.Now()
bw.rate = float64(c - bw.counter) / now.Sub(bw.stamp).Seconds() bw.lock.Lock()
bw.rate = float64(c-bw.counter) / now.Sub(bw.stamp).Seconds()
bw.counter = c bw.counter = c
bw.stamp = now bw.stamp = now
bw.lock.Unlock()
} }
case <-bw.stop: case <-stop:
return return
} }
} }
......
...@@ -391,6 +391,9 @@ func (rc *RadioNode) Start() { ...@@ -391,6 +391,9 @@ func (rc *RadioNode) Start() {
// Icecast status checker. // Icecast status checker.
rc.icecast.Run, rc.icecast.Run,
// Bandwidth monitor.
rc.bw.Run,
} }
for _, fn := range bgfuncs { for _, fn := range bgfuncs {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment