@@ -44,14 +44,16 @@ import PostgREST.Observation
4444import PostgREST.TimeIt (timeItT )
4545import PostgREST.Version (prettyVersion )
4646
47- import Control.AutoUpdate (defaultUpdateSettings , mkAutoUpdate ,
48- updateAction )
49- import Control.Retry (RetryPolicy , RetryStatus (.. ), capDelay ,
50- exponentialBackoff , retrying ,
51- rsPreviousDelay )
52- import Data.IORef (IORef , atomicWriteIORef , newIORef ,
53- readIORef )
54- import Data.Time.Clock (UTCTime , getCurrentTime )
47+ import Control.AutoUpdate (defaultUpdateSettings , mkAutoUpdate ,
48+ updateAction )
49+ import Control.Concurrent.STM (TMVar , newEmptyTMVarIO , putTMVar ,
50+ tryReadTMVar , tryTakeTMVar )
51+ import Control.Retry (RetryPolicy , RetryStatus (.. ),
52+ capDelay , exponentialBackoff , retrying ,
53+ rsPreviousDelay )
54+ import Data.IORef (IORef , atomicWriteIORef , newIORef ,
55+ readIORef )
56+ import Data.Time.Clock (UTCTime , getCurrentTime )
5557
5658import PostgREST.Auth.JwtCache (JwtCacheState , update )
5759import PostgREST.Config (AppConfig (.. ),
@@ -76,7 +78,7 @@ data AppState = AppState
7678 -- | Database server version
7779 , statePgVersion :: IORef PgVersion
7880 -- | Schema cache
79- , stateSchemaCache :: IORef (Maybe SchemaCache )
81+ , stateSchemaCache :: TMVar (Maybe SchemaCache )
8082 -- | The schema cache status
8183 , stateSCacheStatus :: SchemaCacheStatus
8284 -- | State of the LISTEN channel
@@ -123,7 +125,7 @@ initWithPool pool conf loggerState metricsState observer = mdo
123125
124126 appState <- AppState pool
125127 <$> newIORef minimumPgVersion -- assume we're in a supported version when starting, this will be corrected on a later step
126- <*> newIORef Nothing
128+ <*> newEmptyTMVarIO
127129 <*> newSchemaCacheStatus
128130 <*> newIORef False
129131 <*> makeDebouncer (retryingSchemaCacheLoad appState *> threadDelay 100000 ) -- 100ms cooldown
@@ -237,10 +239,11 @@ putPgVersion :: AppState -> PgVersion -> IO ()
237239putPgVersion = atomicWriteIORef . statePgVersion
238240
239241getSchemaCache :: AppState -> IO (Maybe SchemaCache )
240- getSchemaCache = readIORef . stateSchemaCache
242+ getSchemaCache = pure . join <=< atomically . tryReadTMVar . stateSchemaCache
241243
242244putSchemaCache :: AppState -> Maybe SchemaCache -> IO ()
243- putSchemaCache appState = atomicWriteIORef (stateSchemaCache appState)
245+ putSchemaCache AppState {stateSchemaCache} sc = atomically $
246+ tryTakeTMVar stateSchemaCache *> putTMVar stateSchemaCache sc
244247
245248schemaCacheLoader :: AppState -> IO ()
246249schemaCacheLoader = debouncedSCacheLoader
0 commit comments