From c6b1264745df13f9cf227e4a40938255555f490d Mon Sep 17 00:00:00 2001 From: Justin Jung Date: Mon, 11 Sep 2023 16:09:02 -0700 Subject: [PATCH] Add read lock + increment right before sending series request Signed-off-by: Justin Jung --- pkg/storegateway/bucket_stores.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/storegateway/bucket_stores.go b/pkg/storegateway/bucket_stores.go index 45d3d9f743..6cc7d478d1 100644 --- a/pkg/storegateway/bucket_stores.go +++ b/pkg/storegateway/bucket_stores.go @@ -301,13 +301,8 @@ func (u *BucketStores) Series(req *storepb.SeriesRequest, srv storepb.Store_Seri defer spanLog.Span.Finish() maxInflightRequests := u.cfg.BucketStore.MaxInflightRequests - if maxInflightRequests > 0 { - if u.inflightRequestCnt >= maxInflightRequests { - return ErrTooManyInflightRequests - } - - u.incrementInflightRequestCnt() - defer u.decrementInflightRequestCnt() + if maxInflightRequests > 0 && u.getInflightRequestCnt() >= maxInflightRequests { + return ErrTooManyInflightRequests } userID := getUserIDFromGRPCContext(spanCtx) @@ -330,6 +325,11 @@ func (u *BucketStores) Series(req *storepb.SeriesRequest, srv storepb.Store_Seri return nil } + if maxInflightRequests > 0 { + u.incrementInflightRequestCnt() + defer u.decrementInflightRequestCnt() + } + err = store.Series(req, spanSeriesServer{ Store_SeriesServer: srv, ctx: spanCtx, @@ -338,6 +338,12 @@ func (u *BucketStores) Series(req *storepb.SeriesRequest, srv storepb.Store_Seri return err } +func (u *BucketStores) getInflightRequestCnt() int { + u.inflightRequestMu.RLock() + defer u.inflightRequestMu.RUnlock() + return u.inflightRequestCnt +} + func (u *BucketStores) incrementInflightRequestCnt() { u.inflightRequestMu.Lock() u.inflightRequestCnt++