File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -158,6 +158,7 @@ library
158158 , stm-hamt >= 1.2 && < 2
159159 , focus >= 1.0 && < 2
160160 , some >= 1.0.4.1 && < 2
161+ , MonadRandom >= 0.6.2 && < 0.7
161162 -- -fno-spec-constr may help keep compile time memory use in check,
162163 -- see https://gitlab.haskell.org/ghc/ghc/issues/16017#note_219304
163164 -- -optP-Wno-nonportable-include-path
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import qualified PostgREST.AppState as AppState
1818import qualified PostgREST.Config as Config
1919
2020import Control.Arrow ((&&&) )
21+ import Control.Monad.Random
2122import Data.Bitraversable (bisequence )
2223import Data.Either.Combinators (whenRight )
2324import qualified Database.PostgreSQL.LibPQ as LibPQ
@@ -102,7 +103,13 @@ retryingListen appState = do
102103 | msg == " reload config" -> observer (DBListenerGotConfigMsg channel) >> AppState. readInDbConfig False appState
103104 | otherwise -> pure () -- Do nothing if anything else than an empty message is sent
104105
105- cacheReloader =
106+ -- add a random delay between 0 and 1 second to avoid thundering herd problem
107+ -- if multiple listeners receive the same notification at the same time
108+ -- the cacheReloader is launched in a separate thread to avoid blocking the listener
109+ -- during the random delay
110+ cacheReloader = void $ forkIO $ do
111+ delay <- getRandomR (0 , 1000000 )
112+ threadDelay delay
106113 AppState. schemaCacheLoader appState
107114
108115 releaseConnection = void . forkIO . handle (observer . DBListenerConnectionCleanupFail ) . SQL. release
Original file line number Diff line number Diff line change 1818
1919def sleep_until_postgrest_scache_reload ():
2020 "Sleep until schema cache reload"
21- time .sleep (0.3 )
21+ time .sleep (1 )
2222
2323
2424def sleep_until_postgrest_config_reload ():
@@ -28,7 +28,7 @@ def sleep_until_postgrest_config_reload():
2828
2929def sleep_until_postgrest_full_reload ():
3030 "Sleep until schema cache plus config reload"
31- time .sleep (0.3 )
31+ time .sleep (1 )
3232
3333
3434class PostgrestTimedOut (Exception ):
You can’t perform that action at this time.
0 commit comments