Skip to content

Commit

Permalink
Add support for dynamic connection configuration
Browse files Browse the repository at this point in the history
See #11.
  • Loading branch information
nikita-volkov committed Jun 14, 2022
1 parent eb0732f commit 84c1b8c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.7.2

Added support for dynamic connection configuration ([issue #11](https://github.com/nikita-volkov/hasql-pool/issues/11)).

# 0.7.1.2

Fixed connections not being released if they were in use during the call to `release`.
Expand Down
2 changes: 1 addition & 1 deletion hasql-pool.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name:
hasql-pool
version:
0.7.1.3
0.7.2
category:
Hasql, Database, PostgreSQL
synopsis:
Expand Down
22 changes: 18 additions & 4 deletions library/Hasql/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Hasql.Pool
( -- * Pool
Pool,
acquire,
acquireDynamically,
release,
use,

Expand All @@ -19,7 +20,7 @@ import qualified Hasql.Session as Session
-- | A pool of connections to DB.
data Pool = Pool
{ -- | Connection settings.
poolConnectionSettings :: Connection.Settings,
poolFetchConnectionSettings :: IO Connection.Settings,
-- | Avail connections.
poolConnectionQueue :: TQueue Connection,
-- | Capacity.
Expand All @@ -33,8 +34,20 @@ data Pool = Pool
-- No connections actually get established by this function. It is delegated
-- to 'use'.
acquire :: Int -> Connection.Settings -> IO Pool
acquire poolSize connectionSettings = do
Pool connectionSettings
acquire poolSize connectionSettings =
acquireDynamically poolSize (pure connectionSettings)

-- | Given the pool-size and connection settings constructor action
-- create a connection-pool.
--
-- No connections actually get established by this function. It is delegated
-- to 'use'.
--
-- In difference to 'acquire' new settings get fetched each time a connection
-- is created. This may be useful for some security models.
acquireDynamically :: Int -> IO Connection.Settings -> IO Pool
acquireDynamically poolSize fetchConnectionSettings = do
Pool fetchConnectionSettings
<$> newTQueueIO
<*> newTVarIO poolSize
<*> newTVarIO True
Expand Down Expand Up @@ -73,7 +86,8 @@ use Pool {..} sess =
else return . return . Left $ PoolIsReleasedUsageError
where
onNewConn = do
connRes <- Connection.acquire poolConnectionSettings
settings <- poolFetchConnectionSettings
connRes <- Connection.acquire settings
case connRes of
Left connErr -> do
atomically $ modifyTVar' poolCapacity succ
Expand Down

0 comments on commit 84c1b8c

Please sign in to comment.