Avoid calling getPOSIXTime more than necessary

This commit is contained in:
Joe Nelson
2016-10-03 09:55:28 -07:00
parent 5a166e8e80
commit 9cd65a4033
4 changed files with 25 additions and 10 deletions
+7 -1
View File
@@ -11,9 +11,11 @@ import PostgREST.Config (AppConfig (..),
import PostgREST.OpenAPI (isMalformedProxyUri) import PostgREST.OpenAPI (isMalformedProxyUri)
import PostgREST.DbStructure import PostgREST.DbStructure
import Control.AutoUpdate
import Data.String (IsString (..)) import Data.String (IsString (..))
import Data.Text (stripPrefix) import Data.Text (stripPrefix)
import Data.Function (id) import Data.Function (id)
import Data.Time.Clock.POSIX (getPOSIXTime)
import qualified Hasql.Query as H import qualified Hasql.Query as H
import qualified Hasql.Session as H import qualified Hasql.Session as H
import qualified Hasql.Decoders as HD import qualified Hasql.Decoders as HD
@@ -83,7 +85,11 @@ main = do
) Nothing ) Nothing
#endif #endif
runSettings appSettings $ postgrest conf refDbStructure pool -- ask for the OS time at most once per second
getTime <- mkAutoUpdate
defaultUpdateSettings { updateAction = getPOSIXTime }
runSettings appSettings $ postgrest conf refDbStructure pool getTime
loadSecretFile :: AppConfig -> IO AppConfig loadSecretFile :: AppConfig -> IO AppConfig
loadSecretFile conf = do loadSecretFile conf = do
+2
View File
@@ -30,6 +30,7 @@ executable postgrest
"-with-rtsopts=-N -I2" "-with-rtsopts=-N -I2"
default-language: Haskell2010 default-language: Haskell2010
build-depends: aeson (>= 0.8 && < 0.10) || (>= 0.11 && < 0.12) build-depends: aeson (>= 0.8 && < 0.10) || (>= 0.11 && < 0.12)
, auto-update
, base >= 4.8 && < 6 , base >= 4.8 && < 6
, bytestring , bytestring
, bytestring-tree-builder == 0.2.7 , bytestring-tree-builder == 0.2.7
@@ -152,6 +153,7 @@ Test-Suite spec
, SpecHelper , SpecHelper
, TestTypes , TestTypes
Build-Depends: aeson Build-Depends: aeson
, auto-update
, aeson-qq , aeson-qq
, async , async
, base , base
+5 -4
View File
@@ -13,6 +13,7 @@ import Data.List (delete, lookup)
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.Ranged.Ranges (emptyRange) import Data.Ranged.Ranges (emptyRange)
import Data.Text (replace, strip, isInfixOf, dropWhile, drop, intercalate) import Data.Text (replace, strip, isInfixOf, dropWhile, drop, intercalate)
import Data.Time.Clock.POSIX (POSIXTime)
import Data.Tree import Data.Tree
import qualified Hasql.Pool as P import qualified Hasql.Pool as P
@@ -32,7 +33,6 @@ import Web.JWT (secret)
import Data.Aeson import Data.Aeson
import Data.Aeson.Types (emptyArray) import Data.Aeson.Types (emptyArray)
import Data.Time.Clock.POSIX (getPOSIXTime)
import qualified Data.Vector as V import qualified Data.Vector as V
import qualified Hasql.Transaction as H import qualified Hasql.Transaction as H
@@ -69,12 +69,13 @@ import Data.Foldable (foldr1)
import Data.Function (id) import Data.Function (id)
import Protolude hiding (dropWhile, drop, intercalate, Proxy) import Protolude hiding (dropWhile, drop, intercalate, Proxy)
postgrest :: AppConfig -> IORef DbStructure -> P.Pool -> Application postgrest :: AppConfig -> IORef DbStructure -> P.Pool -> IO POSIXTime ->
postgrest conf refDbStructure pool = Application
postgrest conf refDbStructure pool getTime =
let middle = (if configQuiet conf then id else logStdout) . defaultMiddle in let middle = (if configQuiet conf then id else logStdout) . defaultMiddle in
middle $ \ req respond -> do middle $ \ req respond -> do
time <- getPOSIXTime time <- getTime
body <- strictRequestBody req body <- strictRequestBody req
dbStructure <- readIORef refDbStructure dbStructure <- readIORef refDbStructure
+11 -5
View File
@@ -7,8 +7,10 @@ import qualified Hasql.Pool as P
import PostgREST.DbStructure (getDbStructure) import PostgREST.DbStructure (getDbStructure)
import PostgREST.App (postgrest) import PostgREST.App (postgrest)
import Control.AutoUpdate
import Data.IORef import Data.IORef
import Data.String.Conversions (cs) import Data.String.Conversions (cs)
import Data.Time.Clock.POSIX (getPOSIXTime)
import qualified Feature.AuthSpec import qualified Feature.AuthSpec
import qualified Feature.ConcurrentSpec import qualified Feature.ConcurrentSpec
@@ -28,14 +30,18 @@ main = do
setupDb setupDb
pool <- P.acquire (3, 10, cs testDbConn) pool <- P.acquire (3, 10, cs testDbConn)
-- ask for the OS time at most once per second
getTime <- mkAutoUpdate
defaultUpdateSettings { updateAction = getPOSIXTime }
result <- P.use pool $ getDbStructure "test" result <- P.use pool $ getDbStructure "test"
refDbStructure <- newIORef $ either (error.show) id result refDbStructure <- newIORef $ either (error.show) id result
let withApp = return $ postgrest testCfg refDbStructure pool let withApp = return $ postgrest testCfg refDbStructure pool getTime
ltdApp = return $ postgrest testLtdRowsCfg refDbStructure pool ltdApp = return $ postgrest testLtdRowsCfg refDbStructure pool getTime
unicodeApp = return $ postgrest testUnicodeCfg refDbStructure pool unicodeApp = return $ postgrest testUnicodeCfg refDbStructure pool getTime
proxyApp = return $ postgrest testProxyCfg refDbStructure pool proxyApp = return $ postgrest testProxyCfg refDbStructure pool getTime
noJwtApp = return $ postgrest testCfgNoJWT refDbStructure pool noJwtApp = return $ postgrest testCfgNoJWT refDbStructure pool getTime
hspec $ do hspec $ do
mapM_ (beforeAll_ resetDb . before withApp) specs mapM_ (beforeAll_ resetDb . before withApp) specs