Really shut down

This commit is contained in:
Ian Gulliver
2023-06-17 14:21:32 -07:00
parent a33043d40c
commit 152b30a10a
2 changed files with 9 additions and 1 deletions

View File

@@ -83,7 +83,7 @@ func (c *Client) Close() {
defer c.mu.Unlock() defer c.mu.Unlock()
for _, target := range c.targets { for _, target := range c.targets {
close(target.stop) target.close()
} }
} }

View File

@@ -18,6 +18,7 @@ type Target struct {
windowSeconds float64 windowSeconds float64
rateClasses []*rateClass rateClasses []*rateClass
stop chan bool stop chan bool
done chan bool
lastEvent time.Time lastEvent time.Time
events []*Event events []*Event
} }
@@ -39,6 +40,11 @@ func (target *Target) AddRateClass(grantRate float64, vals ...any) {
target.rateClasses = append(target.rateClasses, erc) target.rateClasses = append(target.rateClasses, erc)
} }
func (target *Target) close() {
close(target.stop)
<-target.done
}
func (target *Target) writeEvent(ev *Event) { func (target *Target) writeEvent(ev *Event) {
now := time.Now() now := time.Now()
secondsSinceLastEvent := now.Sub(target.lastEvent).Seconds() secondsSinceLastEvent := now.Sub(target.lastEvent).Seconds()
@@ -74,6 +80,8 @@ func (target *Target) writeEvent(ev *Event) {
} }
func (target *Target) flushLoop(c *Client) { func (target *Target) flushLoop(c *Client) {
defer close(target.done)
t := time.NewTicker(time.Duration(target.writePeriodSeconds * float64(time.Second))) t := time.NewTicker(time.Duration(target.writePeriodSeconds * float64(time.Second)))
defer t.Stop() defer t.Stop()