Skip to content

Commit e7900f7

Browse files
committed
refactor: change stateSchemaCache to TMVar
This commit changes implementation of stateSchemaCache from IORef to TMVar, which introduces a schema cache lifecycle: not initialized --> initialized (possibly empty). This in turn will allow implementing waiting for first schema cache load attempt at startup.
1 parent 98f8e52 commit e7900f7

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

src/PostgREST/AppState.hs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ import PostgREST.Observation
4444
import PostgREST.TimeIt (timeItT)
4545
import 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

5658
import PostgREST.Auth.JwtCache (JwtCacheState, update)
5759
import 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 ()
237239
putPgVersion = atomicWriteIORef . statePgVersion
238240

239241
getSchemaCache :: AppState -> IO (Maybe SchemaCache)
240-
getSchemaCache = readIORef . stateSchemaCache
242+
getSchemaCache = pure . join <=< atomically . tryReadTMVar . stateSchemaCache
241243

242244
putSchemaCache :: AppState -> Maybe SchemaCache -> IO ()
243-
putSchemaCache appState = atomicWriteIORef (stateSchemaCache appState)
245+
putSchemaCache AppState{stateSchemaCache} sc = atomically $
246+
tryTakeTMVar stateSchemaCache *> putTMVar stateSchemaCache sc
244247

245248
schemaCacheLoader :: AppState -> IO ()
246249
schemaCacheLoader = debouncedSCacheLoader

0 commit comments

Comments
 (0)