From 62acbc4ad46f5b1c85b5ad0cced2170025b0d981 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 29 Mar 2022 10:21:21 +0100 Subject: [PATCH 1/7] PING error now throws error to restart SMPClient for more reliable re-connection (#342) --- src/Simplex/Messaging/Client.hs | 4 ++-- src/Simplex/Messaging/Transport/KeepAlive.hs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 151b77463..00cfc3b3b 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -114,7 +114,7 @@ smpDefaultConfig = defaultTransport = ("5223", transport @TLS), tcpTimeout = 4_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, - smpPing = 600_000_000 -- 10min + smpPing = 300_000_000 -- 5 min } data Request = Request @@ -194,7 +194,7 @@ getSMPClient smpServer cfg@SMPClientConfig {qSize, tcpTimeout, tcpKeepAlive, smp ping :: SMPClient -> IO () ping c = forever $ do threadDelay smpPing - runExceptT $ sendSMPCommand c Nothing "" PING + void . either throwIO pure =<< runExceptT (sendSMPCommand c Nothing "" PING) process :: SMPClient -> IO () process SMPClient {rcvQ, sentCommands} = forever $ do diff --git a/src/Simplex/Messaging/Transport/KeepAlive.hs b/src/Simplex/Messaging/Transport/KeepAlive.hs index aa308492d..c949394a0 100644 --- a/src/Simplex/Messaging/Transport/KeepAlive.hs +++ b/src/Simplex/Messaging/Transport/KeepAlive.hs @@ -52,7 +52,7 @@ foreign import capi "netinet/tcp.h value TCP_KEEPINTVL" _TCP_KEEPINTVL :: CInt foreign import capi "netinet/tcp.h value TCP_KEEPCNT" _TCP_KEEPCNT :: CInt -#endif +#endif setSocketKeepAlive :: Socket -> KeepAliveOpts -> IO () setSocketKeepAlive sock KeepAliveOpts {keepCnt, keepIdle, keepIntvl} = do From b6e87e4a3e4d8d6f0d4b41ec13b3787f8d1c5189 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 29 Mar 2022 11:18:49 +0100 Subject: [PATCH 2/7] increase TCP timeout to 5 sec --- src/Simplex/Messaging/Client.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Client.hs b/src/Simplex/Messaging/Client.hs index 00cfc3b3b..e3f0858e0 100644 --- a/src/Simplex/Messaging/Client.hs +++ b/src/Simplex/Messaging/Client.hs @@ -112,7 +112,7 @@ smpDefaultConfig = SMPClientConfig { qSize = 64, defaultTransport = ("5223", transport @TLS), - tcpTimeout = 4_000_000, + tcpTimeout = 5_000_000, tcpKeepAlive = Just defaultKeepAliveOpts, smpPing = 300_000_000 -- 5 min } From 33f822d72cb00a13f312fb0a908fe3732dee7961 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 31 Mar 2022 14:51:59 +0400 Subject: [PATCH 3/7] add pragmas and vacuum db (#343) --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 295e28620..f9dd27f37 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -81,10 +81,24 @@ createSQLiteStore dbFilePath poolSize migrations yesToMigrations = do let dbDir = takeDirectory dbFilePath createDirectoryIfMissing False dbDir st <- connectSQLiteStore dbFilePath poolSize + withConnection st $ \db -> DB.execute_ db "VACUUM;" + -- _printPragmas st checkThreadsafe st migrateSchema st migrations yesToMigrations pure st +_printPragmas :: SQLiteStore -> IO () +_printPragmas st@SQLiteStore {dbFilePath} = withConnection st $ \db -> do + foreign_keys <- DB.query_ db "PRAGMA foreign_keys;" :: IO [[Int]] + print $ dbFilePath <> " foreign_keys: " <> show foreign_keys + -- when run via sqlite-simple query for trusted_schema seems to return empty list + trusted_schema <- DB.query_ db "PRAGMA trusted_schema;" :: IO [[Int]] + print $ dbFilePath <> " trusted_schema: " <> show trusted_schema + secure_delete <- DB.query_ db "PRAGMA secure_delete;" :: IO [[Int]] + print $ dbFilePath <> " secure_delete: " <> show secure_delete + auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]] + print $ dbFilePath <> " auto_vacuum: " <> show auto_vacuum + checkThreadsafe :: SQLiteStore -> IO () checkThreadsafe st = withConnection st $ \db -> do compileOptions <- DB.query_ db "pragma COMPILE_OPTIONS;" :: IO [[Text]] @@ -127,7 +141,10 @@ connectSQLiteStore dbFilePath poolSize = do connectDB :: FilePath -> IO DB.Connection connectDB path = do dbConn <- DB.open path - DB.execute_ dbConn "PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL;" + DB.execute_ dbConn "PRAGMA foreign_keys = ON;" + -- DB.execute_ dbConn "PRAGMA trusted_schema = OFF;" + DB.execute_ dbConn "PRAGMA secure_delete = ON;" + DB.execute_ dbConn "PRAGMA auto_vacuum = FULL;" pure dbConn checkConstraint :: StoreError -> IO (Either StoreError a) -> IO (Either StoreError a) From a6ec93c38efcc9f5428602461b233aa00d74bc74 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 31 Mar 2022 15:26:13 +0400 Subject: [PATCH 4/7] vacuum in each connection to enable auto-vacuum (#344) --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index f9dd27f37..ff99f04b6 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -81,24 +81,10 @@ createSQLiteStore dbFilePath poolSize migrations yesToMigrations = do let dbDir = takeDirectory dbFilePath createDirectoryIfMissing False dbDir st <- connectSQLiteStore dbFilePath poolSize - withConnection st $ \db -> DB.execute_ db "VACUUM;" - -- _printPragmas st checkThreadsafe st migrateSchema st migrations yesToMigrations pure st -_printPragmas :: SQLiteStore -> IO () -_printPragmas st@SQLiteStore {dbFilePath} = withConnection st $ \db -> do - foreign_keys <- DB.query_ db "PRAGMA foreign_keys;" :: IO [[Int]] - print $ dbFilePath <> " foreign_keys: " <> show foreign_keys - -- when run via sqlite-simple query for trusted_schema seems to return empty list - trusted_schema <- DB.query_ db "PRAGMA trusted_schema;" :: IO [[Int]] - print $ dbFilePath <> " trusted_schema: " <> show trusted_schema - secure_delete <- DB.query_ db "PRAGMA secure_delete;" :: IO [[Int]] - print $ dbFilePath <> " secure_delete: " <> show secure_delete - auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]] - print $ dbFilePath <> " auto_vacuum: " <> show auto_vacuum - checkThreadsafe :: SQLiteStore -> IO () checkThreadsafe st = withConnection st $ \db -> do compileOptions <- DB.query_ db "pragma COMPILE_OPTIONS;" :: IO [[Text]] @@ -145,8 +131,22 @@ connectDB path = do -- DB.execute_ dbConn "PRAGMA trusted_schema = OFF;" DB.execute_ dbConn "PRAGMA secure_delete = ON;" DB.execute_ dbConn "PRAGMA auto_vacuum = FULL;" + DB.execute_ dbConn "VACUUM;" + -- _printPragmas dbConn path pure dbConn +_printPragmas :: DB.Connection -> FilePath -> IO () +_printPragmas db path = do + foreign_keys <- DB.query_ db "PRAGMA foreign_keys;" :: IO [[Int]] + print $ path <> " foreign_keys: " <> show foreign_keys + -- when run via sqlite-simple query for trusted_schema seems to return empty list + trusted_schema <- DB.query_ db "PRAGMA trusted_schema;" :: IO [[Int]] + print $ path <> " trusted_schema: " <> show trusted_schema + secure_delete <- DB.query_ db "PRAGMA secure_delete;" :: IO [[Int]] + print $ path <> " secure_delete: " <> show secure_delete + auto_vacuum <- DB.query_ db "PRAGMA auto_vacuum;" :: IO [[Int]] + print $ path <> " auto_vacuum: " <> show auto_vacuum + checkConstraint :: StoreError -> IO (Either StoreError a) -> IO (Either StoreError a) checkConstraint err action = action `E.catch` (pure . Left . handleSQLError err) From bb99fdaaa23a8d537e8add7fd3a22e11960cc860 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Apr 2022 12:52:13 +0400 Subject: [PATCH 5/7] catch db connection error (#345) --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index ff99f04b6..11b737893 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -78,12 +78,16 @@ data SQLiteStore = SQLiteStore createSQLiteStore :: FilePath -> Int -> [Migration] -> Bool -> IO SQLiteStore createSQLiteStore dbFilePath poolSize migrations yesToMigrations = do - let dbDir = takeDirectory dbFilePath - createDirectoryIfMissing False dbDir - st <- connectSQLiteStore dbFilePath poolSize - checkThreadsafe st - migrateSchema st migrations yesToMigrations - pure st + createStore + `E.catch` \(e :: E.SomeException) -> putStrLn ("exception: " <> show e) >> E.throwIO e + where + createStore = do + let dbDir = takeDirectory dbFilePath + createDirectoryIfMissing False dbDir + st <- connectSQLiteStore dbFilePath poolSize + checkThreadsafe st + migrateSchema st migrations yesToMigrations + pure st checkThreadsafe :: SQLiteStore -> IO () checkThreadsafe st = withConnection st $ \db -> do From c62730fe4c573aa1c42956b2439fc114e03c9878 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Apr 2022 16:39:32 +0400 Subject: [PATCH 6/7] Revert "catch db connection error" This reverts commit 1815e81c2e6973e0867ad199ca3dcc68afe9f265. --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 11b737893..ff99f04b6 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -78,16 +78,12 @@ data SQLiteStore = SQLiteStore createSQLiteStore :: FilePath -> Int -> [Migration] -> Bool -> IO SQLiteStore createSQLiteStore dbFilePath poolSize migrations yesToMigrations = do - createStore - `E.catch` \(e :: E.SomeException) -> putStrLn ("exception: " <> show e) >> E.throwIO e - where - createStore = do - let dbDir = takeDirectory dbFilePath - createDirectoryIfMissing False dbDir - st <- connectSQLiteStore dbFilePath poolSize - checkThreadsafe st - migrateSchema st migrations yesToMigrations - pure st + let dbDir = takeDirectory dbFilePath + createDirectoryIfMissing False dbDir + st <- connectSQLiteStore dbFilePath poolSize + checkThreadsafe st + migrateSchema st migrations yesToMigrations + pure st checkThreadsafe :: SQLiteStore -> IO () checkThreadsafe st = withConnection st $ \db -> do From 3ba1926b1e5ab32451a2239831d614492d40c9be Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 1 Apr 2022 17:16:18 +0400 Subject: [PATCH 7/7] remove manual vacuum (#346) --- src/Simplex/Messaging/Agent/Store/SQLite.hs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index ff99f04b6..f27552d98 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -131,7 +131,6 @@ connectDB path = do -- DB.execute_ dbConn "PRAGMA trusted_schema = OFF;" DB.execute_ dbConn "PRAGMA secure_delete = ON;" DB.execute_ dbConn "PRAGMA auto_vacuum = FULL;" - DB.execute_ dbConn "VACUUM;" -- _printPragmas dbConn path pure dbConn