module Hasql.Pool.Config.Setting whereimport Hasql.Connection.Settings qualified as Connection.Settingsimport Hasql.Pool.Config.Config (Config)import Hasql.Pool.Config.Config qualified as Configimport Hasql.Pool.Config.ReuseOrder (ReuseOrder (..))import Hasql.Pool.Observation (Observation)import Hasql.Pool.Preludeimport Hasql.Session qualified as Sessionapply :: Setting -> Config -> Configapply (Setting run) = run-- | A single setting of a config.newtype Setting = Setting (Config -> Config)-- | Pool size.---- 3 by default.size :: Int -> Settingsize x = Setting (\config -> config {Config.size = x})-- | Connection acquisition timeout.---- 10 seconds by default.acquisitionTimeout :: DiffTime -> SettingacquisitionTimeout 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 -> SettingagingTimeout 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 -> SettingidlenessTimeout x = Setting (\config -> config {Config.idlenessTimeout = x})-- | Connection string.---- By default it is:---- > "postgresql://postgres:postgres@localhost:5432/postgres"staticConnectionSettings :: Connection.Settings.Settings -> SettingstaticConnectionSettings 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.---- By default it is:---- > pure "postgresql://postgres:postgres@localhost:5432/postgres"dynamicConnectionSettings :: IO Connection.Settings.Settings -> SettingdynamicConnectionSettings x = Setting (\config -> config {Config.connectionSettingsProvider = x})-- | Observation handler.---- Typically it's used for monitoring the state of the pool via metrics and logging.---- An observation handler must not throw. Synchronous exceptions from it are discarded; asynchronous exceptions are honored. Its failure never affects the session result or the pool's state.---- 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.---- By default it is:---- > const (pure ())observationHandler :: (Observation -> IO ()) -> SettingobservationHandler x = Setting (\config -> config {Config.observationHandler = x})-- | Initial session.---- Gets executed on every connection upon acquisition.-- Lets you specify the connection-wide settings.---- E.g., you can set the search path for all the sessions executed by the pool by executing the following:---- > initSession (Session.sql "SET search_path TO schema1, schema2, public;")---- The state it establishes survives an ordinary session error (a bad query, a-- constraint violation), since those leave the connection reusable. It does-- not survive an asynchronous exception escaping the session: any such-- exception spends the connection, so the next use runs 'initSession' again-- on a fresh one.initSession :: Session.Session () -> SettinginitSession x = Setting (\config -> config {Config.initSession = x})-- | Which of the idle connections a pool request gets.---- With 'LIFO', the pool shrinks to the connections the load needs: the rest-- time out as idle (see 'idlenessTimeout'). With 'FIFO', steady load keeps-- every connection open.---- 'FIFO' by default.reuseOrder :: ReuseOrder -> SettingreuseOrder x = Setting (\config -> config {Config.reuseOrder = x})