Extract Start() function from Collector

Makes it easier to test.
This commit is contained in:
Julian Kornberger 2016-12-22 03:06:46 +01:00
parent 252ce89fc0
commit a9cdc623ca
2 changed files with 24 additions and 13 deletions

View File

@ -50,7 +50,8 @@ func main() {
if config.Respondd.Enable {
collectInterval := time.Second * time.Duration(config.Respondd.CollectInterval)
collector = respond.NewCollector(db, nodes, collectInterval, config.Respondd.Interface)
collector = respond.NewCollector(db, nodes, config.Respondd.Interface)
collector.Start(collectInterval)
defer collector.Close()
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"compress/flate"
"encoding/json"
"io/ioutil"
"log"
"net"
"reflect"
@ -29,7 +30,7 @@ type Collector struct {
}
// Creates a Collector struct
func NewCollector(db *database.DB, nodes *models.Nodes, interval time.Duration, iface string) *Collector {
func NewCollector(db *database.DB, nodes *models.Nodes, iface string) *Collector {
// Parse address
addr, err := net.ResolveUDPAddr("udp", "[::]:0")
if err != nil {
@ -49,7 +50,6 @@ func NewCollector(db *database.DB, nodes *models.Nodes, interval time.Duration,
nodes: nodes,
multicastAddr: net.JoinHostPort(multiCastGroup+"%"+iface, port),
queue: make(chan *Response, 400),
ticker: time.NewTicker(interval),
stop: make(chan interface{}, 1),
}
@ -60,30 +60,40 @@ func NewCollector(db *database.DB, nodes *models.Nodes, interval time.Duration,
go collector.globalStatsWorker()
}
// Run senders
go func() {
collector.sendOnce() // immediately
collector.sender() // periodically
}()
return collector
}
// Start Collector
func (coll *Collector) Start(interval time.Duration) {
if coll.ticker != nil {
panic("already started")
}
coll.ticker = time.NewTicker(interval)
go func() {
coll.sendOnce() // immediately
coll.sender() // periodically
}()
}
// Close Collector
func (coll *Collector) Close() {
// stop ticker
if coll.ticker != nil {
coll.ticker.Stop()
close(coll.stop)
}
coll.connection.Close()
close(coll.queue)
}
func (coll *Collector) sendOnce() {
coll.sendPacket(coll.multicastAddr)
coll.SendPacket(coll.multicastAddr)
}
func (coll *Collector) sendPacket(address string) {
// Sends a UDP request to the given unicast or multicast address
func (coll *Collector) SendPacket(address string) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
log.Panic(err)