@@ -888,12 +888,14 @@ func (js *jetStream) apiDispatch(sub *subscription, c *client, acc *Account, sub
888888	// Check pending and warn if getting backed up. 
889889	limit  :=  atomic .LoadInt64 (& js .queueLimit )
890890retry:
891+ 	atomic .AddInt64 (& js .apiInflight , 1 )
891892	pending , _  :=  s .jsAPIRoutedReqs .push (& jsAPIRoutedReq {jsub , sub , acc , subject , reply , copyBytes (rmsg ), c .pa })
892893	if  pending  >=  int (limit ) {
893894		if  _ , ok  :=  s .jsAPIRoutedReqs .popOne (); ok  {
894895			// If we were able to take one of the oldest items off the queue, then 
895896			// retry the insert. 
896897			s .rateLimitFormatWarnf ("JetStream API queue limit reached, dropping oldest request" )
898+ 			atomic .AddInt64 (& js .apiInflight , - 1 )
897899			s .publishAdvisory (nil , JSAdvisoryAPILimitReached , JSAPILimitReachedAdvisory {
898900				TypedEvent : TypedEvent {
899901					Type : JSAPILimitReachedAdvisoryType ,
@@ -911,7 +913,7 @@ retry:
911913		// then something is wrong for us to be both over the limit but unable to pull entries, so 
912914		// throw everything away and hope we recover from it. 
913915		s .rateLimitFormatWarnf ("JetStream API queue limit reached, dropping %d requests" , pending )
914- 		s .jsAPIRoutedReqs .drain ()
916+ 		atomic . AddInt64 ( & js . apiInflight ,  - int64 ( s .jsAPIRoutedReqs .drain ()) )
915917
916918		s .publishAdvisory (nil , JSAdvisoryAPILimitReached , JSAPILimitReachedAdvisory {
917919			TypedEvent : TypedEvent {
@@ -923,8 +925,6 @@ retry:
923925			Domain :  js .config .Domain ,
924926			Dropped : int64 (pending ),
925927		})
926- 	} else  {
927- 		atomic .StoreInt64 (& js .apiInflight , int64 (pending ))
928928	}
929929}
930930
0 commit comments