@@ -883,14 +883,13 @@ func (js *jetStream) apiDispatch(sub *subscription, c *client, acc *Account, sub
883883 // If we are here we have received this request over a non-client connection.
884884 // We need to make sure not to block. We will send the request to a long-lived
885885 // pool of go routines.
886-
887- // Increment inflight. Do this before queueing.
888- atomic .AddInt64 (& js .apiInflight , 1 )
889-
890886 // Copy the state. Note the JSAPI only uses the hdr index to piece apart the
891887 // header from the msg body. No other references are needed.
892888 // Check pending and warn if getting backed up.
893- pending , _ := s .jsAPIRoutedReqs .push (& jsAPIRoutedReq {jsub , sub , acc , subject , reply , copyBytes (rmsg ), c .pa })
889+ pending , err := s .jsAPIRoutedReqs .push (& jsAPIRoutedReq {jsub , sub , acc , subject , reply , copyBytes (rmsg ), c .pa })
890+ if err == nil {
891+ atomic .AddInt64 (& js .apiInflight , 1 )
892+ }
894893 limit := atomic .LoadInt64 (& js .queueLimit )
895894 if pending >= int (limit ) {
896895 s .rateLimitFormatWarnf ("JetStream API queue limit reached, dropping %d requests" , pending )
@@ -926,7 +925,7 @@ func (s *Server) processJSAPIRoutedRequests() {
926925 // Only pop one item at a time here, otherwise if the system is recovering
927926 // from queue buildup, then one worker will pull off all the tasks and the
928927 // others will be starved of work.
929- for r , ok := queue .popOne (); ok && r != nil ; r , ok = queue .popOne () {
928+ for r , ok := queue .popOneLast (); ok && r != nil ; r , ok = queue .popOneLast () {
930929 client .pa = r .pa
931930 start := time .Now ()
932931 r .jsub .icb (r .sub , client , r .acc , r .subject , r .reply , r .msg )
0 commit comments