@@ -19,6 +19,7 @@ import (
1919 "fmt"
2020 "strings"
2121 "sync"
22+ "time"
2223
2324 monitoring "cloud.google.com/go/monitoring/apiv3"
2425 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
@@ -44,7 +45,7 @@ type metricsBatcher struct {
4445 wg * sync.WaitGroup
4546}
4647
47- func newMetricsBatcher (ctx context.Context , projectID string , numWorkers int , mc * monitoring.MetricClient ) * metricsBatcher {
48+ func newMetricsBatcher (ctx context.Context , projectID string , numWorkers int , mc * monitoring.MetricClient , timeout time. Duration ) * metricsBatcher {
4849 if numWorkers < minNumWorkers {
4950 numWorkers = minNumWorkers
5051 }
@@ -58,7 +59,7 @@ func newMetricsBatcher(ctx context.Context, projectID string, numWorkers int, mc
5859 var wg sync.WaitGroup
5960 wg .Add (numWorkers )
6061 for i := 0 ; i < numWorkers ; i ++ {
61- w := newWorker (ctx , mc , reqsChan , respsChan , & wg )
62+ w := newWorker (ctx , mc , reqsChan , respsChan , & wg , timeout )
6263 workers = append (workers , w )
6364 go w .start ()
6465 }
@@ -143,8 +144,9 @@ func sendReq(ctx context.Context, c *monitoring.MetricClient, req *monitoringpb.
143144}
144145
145146type worker struct {
146- ctx context.Context
147- mc * monitoring.MetricClient
147+ ctx context.Context
148+ timeout time.Duration
149+ mc * monitoring.MetricClient
148150
149151 resp * response
150152
@@ -159,7 +161,8 @@ func newWorker(
159161 mc * monitoring.MetricClient ,
160162 reqsChan chan * monitoringpb.CreateTimeSeriesRequest ,
161163 respsChan chan * response ,
162- wg * sync.WaitGroup ) * worker {
164+ wg * sync.WaitGroup ,
165+ timeout time.Duration ) * worker {
163166 return & worker {
164167 ctx : ctx ,
165168 mc : mc ,
@@ -172,12 +175,19 @@ func newWorker(
172175
173176func (w * worker ) start () {
174177 for req := range w .reqsChan {
175- w .recordDroppedTimeseries ( sendReq ( w . ctx , w . mc , req ) )
178+ w .sendReqWithTimeout ( req )
176179 }
177180 w .respsChan <- w .resp
178181 w .wg .Done ()
179182}
180183
184+ func (w * worker ) sendReqWithTimeout (req * monitoringpb.CreateTimeSeriesRequest ) {
185+ ctx , cancel := newContextWithTimeout (w .ctx , w .timeout )
186+ defer cancel ()
187+
188+ w .recordDroppedTimeseries (sendReq (ctx , w .mc , req ))
189+ }
190+
181191func (w * worker ) recordDroppedTimeseries (numTimeSeries int , err error ) {
182192 w .resp .droppedTimeSeries += numTimeSeries
183193 if err != nil {
0 commit comments