Skip to content

CloudwatchService.Stop should wait for batcher #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions aws/cwatch/service.go
Original file line number Diff line number Diff line change
@@ -17,7 +17,9 @@ type Service struct {
Client Client
namespace string
deployment string
batcher *syncx.Batcher[types.MetricDatum]

batcher *syncx.Batcher[types.MetricDatum]
batcherWG *sync.WaitGroup
}

// NewService creates a new Cloudwatch service with the given credentials and configuration. Some behaviours depend on
@@ -41,11 +43,13 @@ func NewService(accessKey, secretKey, region, namespace, deployment string) (*Se
return &Service{Client: client, namespace: namespace, deployment: deployment}, nil
}

func (s *Service) StartQueue(wg *sync.WaitGroup, maxAge time.Duration) {
func (s *Service) StartQueue(maxAge time.Duration) {
if s.batcher != nil {
panic("queue already started")
}
s.batcher = syncx.NewBatcher(s.processBatch, 100, maxAge, 1000, wg)

s.batcherWG = &sync.WaitGroup{}
s.batcher = syncx.NewBatcher(s.processBatch, 100, maxAge, 1000, s.batcherWG)
s.batcher.Start()
}

@@ -54,6 +58,7 @@ func (s *Service) StopQueue() {
panic("queue wasn't started")
}
s.batcher.Stop()
s.batcherWG.Wait()
}

func (s *Service) Queue(data ...types.MetricDatum) {
5 changes: 1 addition & 4 deletions aws/cwatch/service_test.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ package cwatch_test

import (
"context"
"sync"
"testing"
"time"

@@ -29,8 +28,7 @@ func TestService(t *testing.T) {
svc, err = cwatch.NewService("root", "key", "us-east-1", "Foo", "dev")
assert.NoError(t, err)

wg := &sync.WaitGroup{}
svc.StartQueue(wg, time.Millisecond*100)
svc.StartQueue(time.Millisecond * 100)

svc.Queue(cwatch.Datum("NumGoats", 10, types.StandardUnitCount, cwatch.Dimension("Host", "foo1")))
svc.Queue(cwatch.Datum("NumSheep", 20, types.StandardUnitCount))
@@ -43,7 +41,6 @@ func TestService(t *testing.T) {
svc.Queue(cwatch.Datum("SleepTime", 30, types.StandardUnitSeconds))

svc.StopQueue()
wg.Wait()

// check the queued metric was sent
assert.Equal(t, 2, svc.Client.(*cwatch.DevClient).CallCount())