-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from nikita-volkov/settings
Introduce flexible config
- Loading branch information
Showing
7 changed files
with
167 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- | DSL for construction of configs. | ||
module Hasql.Pool.Config | ||
( Config.Config, | ||
settings, | ||
Setting.Setting, | ||
Setting.size, | ||
Setting.acquisitionTimeout, | ||
Setting.agingTimeout, | ||
Setting.idlenessTimeout, | ||
Setting.staticConnectionSettings, | ||
Setting.dynamicConnectionSettings, | ||
Setting.observationHandler, | ||
) | ||
where | ||
|
||
import qualified Hasql.Pool.Config.Config as Config | ||
import qualified Hasql.Pool.Config.Setting as Setting | ||
import Hasql.Pool.Prelude | ||
|
||
-- | Compile config from a list of settings. | ||
-- Latter settings override the preceding in cases of conflicts. | ||
settings :: [Setting.Setting] -> Config.Config | ||
settings = | ||
foldr ($) Config.defaults . fmap Setting.apply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Hasql.Pool.Config.Config where | ||
|
||
import qualified Hasql.Connection as Connection | ||
import Hasql.Pool.Observation (Observation) | ||
import Hasql.Pool.Prelude | ||
|
||
-- | Configufation for Hasql connection pool. | ||
data Config = Config | ||
{ size :: Int, | ||
acquisitionTimeout :: DiffTime, | ||
agingTimeout :: DiffTime, | ||
idlenessTimeout :: DiffTime, | ||
connectionSettingsProvider :: IO Connection.Settings, | ||
observationHandler :: Observation -> IO () | ||
} | ||
|
||
-- | Reasonable defaults, which can be built upon. | ||
defaults :: Config | ||
defaults = | ||
Config | ||
{ size = 3, | ||
acquisitionTimeout = 10, | ||
agingTimeout = 60 * 60 * 24, | ||
idlenessTimeout = 60 * 10, | ||
connectionSettingsProvider = pure "postgresql://postgres:postgres@localhost:5432/postgres", | ||
observationHandler = const (pure ()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module Hasql.Pool.Config.Setting where | ||
|
||
import qualified Hasql.Connection as Connection | ||
import Hasql.Pool.Config.Config (Config) | ||
import qualified Hasql.Pool.Config.Config as Config | ||
import Hasql.Pool.Observation (Observation) | ||
import Hasql.Pool.Prelude | ||
|
||
apply :: Setting -> Config -> Config | ||
apply (Setting run) = run | ||
|
||
-- | A single setting of a config. | ||
newtype Setting | ||
= Setting (Config -> Config) | ||
|
||
-- | Pool size. | ||
-- | ||
-- 3 by default. | ||
size :: Int -> Setting | ||
size x = | ||
Setting (\config -> config {Config.size = x}) | ||
|
||
-- | Connection acquisition timeout. | ||
-- | ||
-- 10 seconds by default. | ||
acquisitionTimeout :: DiffTime -> Setting | ||
acquisitionTimeout x = | ||
Setting (\config -> config {Config.acquisitionTimeout = x}) | ||
|
||
-- | Maximal connection lifetime. | ||
-- | ||
-- Determines how long is available for reuse. | ||
-- After the timeout passes and an active session is finished the connection will be closed releasing a slot in the pool for a fresh connection to be established. | ||
-- | ||
-- This is useful as a healthy measure for resetting the server-side caches. | ||
-- | ||
-- 1 day by default. | ||
agingTimeout :: DiffTime -> Setting | ||
agingTimeout x = | ||
Setting (\config -> config {Config.agingTimeout = x}) | ||
|
||
-- | Maximal connection idle time. | ||
-- | ||
-- How long to keep a connection open when it's not being used. | ||
-- | ||
-- 10 minutes by default. | ||
idlenessTimeout :: DiffTime -> Setting | ||
idlenessTimeout x = | ||
Setting (\config -> config {Config.idlenessTimeout = x}) | ||
|
||
-- | Connection string. | ||
-- | ||
-- You can use 'Hasql.Connection.settings' to construct it. | ||
-- | ||
-- @\"postgresql://postgres:postgres@localhost:5432/postgres\"@ by default. | ||
staticConnectionSettings :: Connection.Settings -> Setting | ||
staticConnectionSettings x = | ||
Setting (\config -> config {Config.connectionSettingsProvider = pure x}) | ||
|
||
-- | Action providing connection settings. | ||
-- | ||
-- Gets used each time a connection gets established by the pool. | ||
-- This may be useful for some authorization models. | ||
-- | ||
-- You can use 'Hasql.Connection.settings' to construct it. | ||
-- | ||
-- @pure \"postgresql://postgres:postgres@localhost:5432/postgres\"@ by default. | ||
dynamicConnectionSettings :: IO Connection.Settings -> Setting | ||
dynamicConnectionSettings x = | ||
Setting (\config -> config {Config.connectionSettingsProvider = x}) | ||
|
||
-- | Observation handler. | ||
-- | ||
-- Typically it's used for monitoring the state of the pool via metrics and logging. | ||
-- | ||
-- If the provided action is not lightweight, it's recommended to use intermediate bufferring via channels like TBQueue to avoid occupying the pool management thread for too long. | ||
-- E.g., if the action is @'atomically' . 'writeTBQueue' yourQueue@, then reading from it and processing can be done on a separate thread. | ||
-- | ||
-- @const (pure ())@ by default. | ||
observationHandler :: (Observation -> IO ()) -> Setting | ||
observationHandler x = | ||
Setting (\config -> config {Config.observationHandler = x}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters