From 381e12efa20e08f7c6fbd624a67290c5f2bcb629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C5=82eczek?= Date: Thu, 9 Apr 2026 14:57:18 +0200 Subject: [PATCH] refactor: remove usage of Control.Debounce This change replaces usage of Control.Debounce in Logger with makeDebouncer function extracted from AppState to a new module PostgREST.Debounce. --- postgrest.cabal | 1 + src/PostgREST/AppState.hs | 15 +-------------- src/PostgREST/Debounce.hs | 19 +++++++++++++++++++ src/PostgREST/Logger.hs | 9 +++------ 4 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 src/PostgREST/Debounce.hs diff --git a/postgrest.cabal b/postgrest.cabal index 188653a41..e07f69c03 100644 --- a/postgrest.cabal +++ b/postgrest.cabal @@ -55,6 +55,7 @@ library PostgREST.Client PostgREST.Config PostgREST.Config.Database + PostgREST.Debounce PostgREST.Config.JSPath PostgREST.Config.PgVersion PostgREST.Config.Proxy diff --git a/src/PostgREST/AppState.hs b/src/PostgREST/AppState.hs index e8c591b3f..e8424fb72 100644 --- a/src/PostgREST/AppState.hs +++ b/src/PostgREST/AppState.hs @@ -62,6 +62,7 @@ import PostgREST.Config.Database (queryDbSettings, queryRoleSettings) import PostgREST.Config.PgVersion (PgVersion (..), minimumPgVersion) +import PostgREST.Debounce (makeDebouncer) import PostgREST.SchemaCache (SchemaCache (..), querySchemaCache, showSummary) @@ -117,20 +118,6 @@ init conf@AppConfig{configLogLevel, configDbPoolSize} = do pool <- initPool conf observer initWithPool pool conf loggerState metricsState observer --- Make a new debouncer action. An internal "worker" thread runs forever ensuring "action" runs when the "trigger" is called. The "action" is only executed once over a burst of calls. -makeDebouncer :: IO () -> IO (IO ()) -makeDebouncer action = do - flag <- newEmptyMVar - - let worker = forever $ do - takeMVar flag - action - - let trigger = void $ tryPutMVar flag () - - void $ forkIO worker - pure trigger - initWithPool :: SQL.Pool -> AppConfig -> Logger.LoggerState -> Metrics.MetricsState -> ObservationHandler -> IO AppState initWithPool pool conf loggerState metricsState observer = mdo diff --git a/src/PostgREST/Debounce.hs b/src/PostgREST/Debounce.hs new file mode 100644 index 000000000..e2516873c --- /dev/null +++ b/src/PostgREST/Debounce.hs @@ -0,0 +1,19 @@ +module PostgREST.Debounce + ( makeDebouncer) where + +import Protolude + +-- | Make a new debouncer action. An internal "worker" thread runs forever +-- ensuring "action" runs when the "trigger" is called. The "action" is only +-- executed once over a burst of calls. +makeDebouncer :: IO () -> IO (IO ()) +makeDebouncer action = do + flag <- newEmptyMVar + + let worker = forever $ do + takeMVar flag + action + trigger = void $ tryPutMVar flag () + + void $ forkIO worker + pure trigger diff --git a/src/PostgREST/Logger.hs b/src/PostgREST/Logger.hs index 18ced8654..748a1bdc6 100644 --- a/src/PostgREST/Logger.hs +++ b/src/PostgREST/Logger.hs @@ -16,7 +16,6 @@ module PostgREST.Logger import Control.AutoUpdate (defaultUpdateSettings, mkAutoUpdate, updateAction) -import Control.Debounce import qualified Data.ByteString.Char8 as BS import qualified Data.Text.Encoding as T import qualified Hasql.Decoders as HD @@ -34,6 +33,7 @@ import Network.HTTP.Types.Status (Status, status400, status500) import System.IO.Unsafe (unsafePerformIO) import PostgREST.Config (LogLevel (..), Verbosity (..)) +import PostgREST.Debounce (makeDebouncer) import PostgREST.Observation import PostgREST.Query (MainQuery (..)) @@ -58,11 +58,8 @@ init = mdo oneSecond = 1000000 loggerState = LoggerState zTime debouncePoolTimeout zTime <- mkAutoUpdate defaultUpdateSettings { updateAction = getZonedTime } - debouncePoolTimeout <- mkDebounce defaultDebounceSettings - { debounceAction = logWithZTime loggerState $ observationMessages PoolAcqTimeoutObs - , debounceFreq = 5*oneSecond - , debounceEdge = leadingEdge -- logs at the start and the end - } + debouncePoolTimeout <- makeDebouncer $ + logWithZTime loggerState (observationMessages PoolAcqTimeoutObs) *> threadDelay (5 * oneSecond) pure loggerState -- TODO stop using this middleware to reuse the same "observer" pattern for all our logs