@@ -50,6 +50,7 @@ runConsumer
5050 -- ^ The consumer.
5151 -> ConnectionSourceM m
5252 -> m (m () )
53+ {-# INLINEABLE runConsumer #-}
5354runConsumer cc cs = runConsumerWithMaybeIdleSignal cc cs Nothing
5455
5556runConsumerWithIdleSignal
@@ -67,6 +68,7 @@ runConsumerWithIdleSignal
6768 -> ConnectionSourceM m
6869 -> TMVar Bool
6970 -> m (m () )
71+ {-# INLINEABLE runConsumerWithIdleSignal #-}
7072runConsumerWithIdleSignal cc cs idleSignal = runConsumerWithMaybeIdleSignal cc cs (Just idleSignal)
7173
7274-- | Run the consumer and also signal whenever the consumer is waiting for
@@ -85,6 +87,7 @@ runConsumerWithMaybeIdleSignal
8587 -> ConnectionSourceM m
8688 -> Maybe (TMVar Bool )
8789 -> m (m () )
90+ {-# INLINEABLE runConsumerWithMaybeIdleSignal #-}
8891runConsumerWithMaybeIdleSignal cc0 cs mIdleSignal
8992 | ccMaxRunningJobs cc < 1 = do
9093 logInfo_ " ccMaxRunningJobs < 1, not starting the consumer"
@@ -186,6 +189,7 @@ spawnListener
186189 -> ConnectionSourceM m
187190 -> TriggerNotification m
188191 -> m ThreadId
192+ {-# INLINEABLE spawnListener #-}
189193spawnListener cc cs outbox =
190194 forkP " listener" $
191195 case ccNotificationChannel cc of
@@ -227,6 +231,7 @@ spawnMonitor
227231 -> ConnectionSourceM m
228232 -> ConsumerID
229233 -> m ThreadId
234+ {-# INLINEABLE spawnMonitor #-}
230235spawnMonitor ConsumerConfig {.. } cs cid = forkP " monitor" . forever $ do
231236 runDBT cs ts $ do
232237 now <- currentTime
@@ -313,9 +318,13 @@ spawnDispatcher
313318 -> TVar Int
314319 -> Maybe (TMVar Bool )
315320 -> m ThreadId
321+ {-# INLINEABLE spawnDispatcher #-}
316322spawnDispatcher ConsumerConfig {.. } cs cid inbox runningJobsInfo runningJobs mIdleSignal =
317323 forkP " dispatcher" . forever $ do
318324 listenNotification inbox
325+ -- When awoken, we always start slow, processing only a single job in a
326+ -- batch. Each time we can fill a batch completely with jobs, we grow the maximum
327+ -- batch size.
319328 someJobWasProcessed <- loop 1
320329 if someJobWasProcessed
321330 then setIdle False
@@ -350,9 +359,11 @@ spawnDispatcher ConsumerConfig {..} cs cid inbox runningJobsInfo runningJobs mId
350359 . forkP " batch processor"
351360 . (`finally` subtractJobs)
352361 . restore
353- $ do
354- mapM startJob batch >>= mapM joinJob >>= updateJobs
362+ $ mapM startJob batch >>= mapM joinJob >>= updateJobs
355363
364+ -- Induce some backpressure. If the number of running jobs by all batch
365+ -- processors exceed the global limit, we wait. If it does not, start a
366+ -- new iteration with a double the limit
356367 when (batchSize == limit) $ do
357368 maxBatchSize <- atomically $ do
358369 jobs <- readTVar runningJobs
@@ -434,6 +445,7 @@ updateJobsQuery
434445 -> [(idx , Result )]
435446 -> UTCTime
436447 -> SQL
448+ {-# INLINEABLE updateJobsQuery #-}
437449updateJobsQuery jobsTable results now =
438450 smconcat
439451 [ " WITH removed AS ("
0 commit comments