push.go: add an ability to wait until push workers are stopped via PushOptions.WaitGroup
This commit is contained in:
parent
49f6df7219
commit
c45a8b1b70
13
push.go
13
push.go
@ -31,6 +31,9 @@ type PushOptions struct {
|
|||||||
//
|
//
|
||||||
// By default the compression is enabled.
|
// By default the compression is enabled.
|
||||||
DisableCompression bool
|
DisableCompression bool
|
||||||
|
|
||||||
|
// Optional WaitGroup for waiting until all the push workers created with this WaitGroup are stopped.
|
||||||
|
WaitGroup *sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitPushWithOptions sets up periodic push for globally registered metrics to the given pushURL with the given interval.
|
// InitPushWithOptions sets up periodic push for globally registered metrics to the given pushURL with the given interval.
|
||||||
@ -207,6 +210,13 @@ func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.D
|
|||||||
}
|
}
|
||||||
pushMetricsSet.GetOrCreateFloatCounter(fmt.Sprintf(`metrics_push_interval_seconds{url=%q}`, pc.pushURLRedacted)).Set(interval.Seconds())
|
pushMetricsSet.GetOrCreateFloatCounter(fmt.Sprintf(`metrics_push_interval_seconds{url=%q}`, pc.pushURLRedacted)).Set(interval.Seconds())
|
||||||
|
|
||||||
|
var wg *sync.WaitGroup
|
||||||
|
if opts != nil {
|
||||||
|
wg = opts.WaitGroup
|
||||||
|
if wg != nil {
|
||||||
|
wg.Add(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
go func() {
|
go func() {
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
@ -221,6 +231,9 @@ func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.D
|
|||||||
log.Printf("ERROR: metrics.push: %s", err)
|
log.Printf("ERROR: metrics.push: %s", err)
|
||||||
}
|
}
|
||||||
case <-stopCh:
|
case <-stopCh:
|
||||||
|
if wg != nil {
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -91,6 +92,10 @@ func TestInitPushWithOptions(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
if opts != nil {
|
||||||
|
opts.WaitGroup = &wg
|
||||||
|
}
|
||||||
if err := s.InitPushWithOptions(ctx, srv.URL, time.Millisecond, opts); err != nil {
|
if err := s.InitPushWithOptions(ctx, srv.URL, time.Millisecond, opts); err != nil {
|
||||||
t.Fatalf("unexpected error: %s", err)
|
t.Fatalf("unexpected error: %s", err)
|
||||||
}
|
}
|
||||||
@ -100,6 +105,7 @@ func TestInitPushWithOptions(t *testing.T) {
|
|||||||
case <-doneCh:
|
case <-doneCh:
|
||||||
// stop the periodic pusher
|
// stop the periodic pusher
|
||||||
cancel()
|
cancel()
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
if reqErr != nil {
|
if reqErr != nil {
|
||||||
t.Fatalf("unexpected error: %s", reqErr)
|
t.Fatalf("unexpected error: %s", reqErr)
|
||||||
|
Loading…
Reference in New Issue
Block a user