From d32f373e1ee95ba68141a3f3a48ab8c94ea2453a Mon Sep 17 00:00:00 2001 From: Xavier Francisco Date: Thu, 25 Apr 2019 18:23:20 +0100 Subject: [PATCH] Add config option for pool timeout (#1280) --- CHANGELOG.md | 2 ++ main/Main.hs | 4 ++-- src/PostgREST/Config.hs | 9 +++++++++ test/SpecHelper.hs | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3647e2c8..b3dc859be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #690, Add `?columns` query parameter for faster bulk inserts, also ignores unspecified json keys in a payload - @steve-chavez - #1239, Add support for resource embedding on materialized views - @vitorbaptista - #1264, Add support for bulk RPC call - @steve-chavez +- #1278, Add db-pool-timeout config option - @qu4tro ### Fixed @@ -19,6 +20,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #1238, Fix PostgreSQL to OpenAPI type mappings for numeric and character types - @fpusch - #1265, Fix query generated on bulk upsert with an empty array - @qu4tro - #1273, Fix RPC ignoring unknown arguments by default - @steve-chavez +- #1257, Fix incorrect status when a PATCH request doesn't find rows to change - @qu4tro ## [5.2.0] - 2018-12-12 diff --git a/main/Main.hs b/main/Main.hs index 7d3989236..32f8d1e17 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -4,7 +4,7 @@ module Main where import PostgREST.App (postgrest) -import PostgREST.Config (AppConfig (..), +import PostgREST.Config (AppConfig (..), configPoolTimeout', prettyVersion, readOptions) import PostgREST.DbStructure (getDbStructure, getPgVersion) import PostgREST.Error (encodeError) @@ -164,7 +164,7 @@ main = do -- -- create connection pool with the provided settings, returns either -- a 'Connection' or a 'ConnectionError'. Does not throw. - pool <- P.acquire (configPool conf, 10, pgSettings) + pool <- P.acquire (configPool conf, configPoolTimeout' conf, pgSettings) -- -- To be filled in by connectionWorker refDbStructure <- newIORef Nothing diff --git a/src/PostgREST/Config.hs b/src/PostgREST/Config.hs index 39ba4a355..1b18e6b76 100644 --- a/src/PostgREST/Config.hs +++ b/src/PostgREST/Config.hs @@ -19,6 +19,7 @@ module PostgREST.Config ( prettyVersion , readOptions , corsPolicy , AppConfig (..) + , configPoolTimeout' ) where @@ -73,6 +74,7 @@ data AppConfig = AppConfig { , configJwtAudience :: Maybe StringOrURI , configPool :: Int + , configPoolTimeout :: Int , configMaxRows :: Maybe Integer , configReqCheck :: Maybe Text , configQuiet :: Bool @@ -81,6 +83,11 @@ data AppConfig = AppConfig { , configExtraSearchPath :: [Text] } +configPoolTimeout' :: (Fractional a) => AppConfig -> a +configPoolTimeout' = + fromRational . toRational . configPoolTimeout + + defaultCorsPolicy :: CorsResourcePolicy defaultCorsPolicy = CorsResourcePolicy Nothing ["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"] ["Authorization"] Nothing @@ -136,6 +143,7 @@ readOptions = do <*> (fromMaybe False . join . fmap coerceBool <$> C.key "secret-is-base64") <*> parseJwtAudience "jwt-aud" <*> (fromMaybe 10 . join . fmap coerceInt <$> C.key "db-pool") + <*> (fromMaybe 10 . join . fmap coerceInt <$> C.key "db-pool-timeout") <*> (join . fmap coerceInt <$> C.key "max-rows") <*> (mfilter (/= "") <$> C.key "pre-request") <*> pure False @@ -208,6 +216,7 @@ readOptions = do |db-schema = "public" # this schema gets added to the search_path of every request |db-anon-role = "postgres" |db-pool = 10 + |db-pool-timeout = 10 | |server-host = "127.0.0.1" |server-port = 3000 diff --git a/test/SpecHelper.hs b/test/SpecHelper.hs index 403b5104d..5a2c3b9df 100644 --- a/test/SpecHelper.hs +++ b/test/SpecHelper.hs @@ -70,7 +70,7 @@ _baseCfg = -- Connection Settings -- Jwt settings (Just $ encodeUtf8 "reallyreallyreallyreallyverysafe") False Nothing -- Connection Modifiers - 10 Nothing (Just "test.switch_role") + 10 10 Nothing (Just "test.switch_role") -- Debug Settings True [ ("app.settings.app_host", "localhost")