Skip to content

Commit

Permalink
Allow custo configuration of Datasync limits
Browse files Browse the repository at this point in the history
mpscholten committed Feb 8, 2022

Verified

This commit was signed with the committer’s verified signature.
larshelge Lars Helge Øverland
1 parent 7e621c2 commit 02441a6
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions IHP/DataSync/Controller.hs
Original file line number Diff line number Diff line change
@@ -386,22 +386,30 @@ findTransactionById transactionId = do
-- concurrent transactions. Then all database connections are removed from the connection pool and further database
-- queries for other users will fail.
--
ensureBelowTransactionLimit :: (?state :: IORef DataSyncController) => IO ()
ensureBelowTransactionLimit :: (?state :: IORef DataSyncController, ?context :: ControllerContext) => IO ()
ensureBelowTransactionLimit = do
transactions <- get #transactions <$> readIORef ?state
let transactionCount = HashMap.size transactions
let maxTransactionsPerConnection = 10
when (transactionCount >= maxTransactionsPerConnection) do
error ("You've reached the transaction limit of " <> tshow maxTransactionsPerConnection <> " transactions")
ensureBelowSubscriptionsLimit :: (?state :: IORef DataSyncController) => IO ()
ensureBelowSubscriptionsLimit :: (?state :: IORef DataSyncController, ?context :: ControllerContext) => IO ()
ensureBelowSubscriptionsLimit = do
subscriptions <- get #subscriptions <$> readIORef ?state
let subscriptionsCount = HashMap.size subscriptions
let maxSubscriptionsPerConnection = 128
when (subscriptionsCount >= maxSubscriptionsPerConnection) do
error ("You've reached the subscriptions limit of " <> tshow maxSubscriptionsPerConnection <> " subscriptions")
maxTransactionsPerConnection :: _ => Int
maxTransactionsPerConnection =
case getAppConfig @DataSyncMaxTransactionsPerConnection of
DataSyncMaxTransactionsPerConnection value -> value
maxSubscriptionsPerConnection :: _ => Int
maxSubscriptionsPerConnection =
case getAppConfig @DataSyncMaxSubscriptionsPerConnection of
DataSyncMaxSubscriptionsPerConnection value -> value
sqlQueryWithRLSAndTransactionId ::
( ?modelContext :: ModelContext
, PG.ToRow parameters
8 changes: 8 additions & 0 deletions IHP/FrameworkConfig.hs
Original file line number Diff line number Diff line change
@@ -73,6 +73,9 @@ newtype AssetVersion = AssetVersion Text

newtype CustomMiddleware = CustomMiddleware Middleware

newtype DataSyncMaxSubscriptionsPerConnection = DataSyncMaxSubscriptionsPerConnection Int
newtype DataSyncMaxTransactionsPerConnection = DataSyncMaxTransactionsPerConnection Int

-- | Puts an option into the current configuration
--
-- In case an option already exists with the same type, it will not be overriden:
@@ -156,6 +159,11 @@ ihpDefaultConfig = do
option $ RLSAuthenticatedRole rlsAuthenticatedRole

initAssetVersion

dataSyncMaxSubscriptionsPerConnection <- envOrDefault "IHP_DATASYNC_MAX_SUBSCRIPTIONS_PER_CONNECTION" 128
dataSyncMaxTransactionsPerConnection <- envOrDefault "IHP_DATASYNC_MAX_TRANSACTIONS_PER_CONNECTION" 10
option $ DataSyncMaxSubscriptionsPerConnection dataSyncMaxSubscriptionsPerConnection
option $ DataSyncMaxTransactionsPerConnection dataSyncMaxTransactionsPerConnection

option $ CustomMiddleware id

0 comments on commit 02441a6

Please sign in to comment.