Skip to content

Commit fe64d42

Browse files
authored
smp server: split postgres support to a separate executable, to not require postgres library in the main binary (#1482)
* smp server: split postgres support to a separate executable, to not require postgres library in the main binary * comments * enable server_postgres flag by default, add CPP option to test * refactor * change default for server_postgres to False * diff
1 parent 1b5a9f3 commit fe64d42

File tree

16 files changed

+425
-298
lines changed

16 files changed

+425
-298
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858

5959
- name: Build binaries
6060
shell: bash
61-
run: docker exec -t -e apps="$apps" builder sh -c 'cabal build --enable-tests && mkdir /out && for i in $apps; do bin=$(find /project/dist-newstyle -name "$i" -type f -executable); strip "$bin"; chmod +x "$bin"; mv "$bin" /out/; done'
61+
run: docker exec -t -e apps="$apps" builder sh -c 'cabal build --enable-tests -fserver_postgres && mkdir /out && for i in $apps; do bin=$(find /project/dist-newstyle -name "$i" -type f -executable); strip "$bin"; chmod +x "$bin"; mv "$bin" /out/; done'
6262

6363
- name: Copy binaries from container and prepare them
6464
if: startsWith(github.ref, 'refs/tags/v')

simplexmq.cabal

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ flag client_postgres
7272
manual: True
7373
default: False
7474

75+
flag server_postgres
76+
description: Build server with support of PostgreSQL.
77+
manual: True
78+
default: False
79+
7580
library
7681
exposed-modules:
7782
Simplex.FileTransfer.Agent
@@ -101,6 +106,7 @@ library
101106
Simplex.Messaging.Agent.Store.Interface
102107
Simplex.Messaging.Agent.Store.Migrations
103108
Simplex.Messaging.Agent.Store.Migrations.App
109+
Simplex.Messaging.Agent.Store.Postgres.Options
104110
Simplex.Messaging.Agent.Store.Shared
105111
Simplex.Messaging.Agent.TRcvQueues
106112
Simplex.Messaging.Client
@@ -124,6 +130,7 @@ library
124130
Simplex.Messaging.Parsers
125131
Simplex.Messaging.Protocol
126132
Simplex.Messaging.Server.Expiration
133+
Simplex.Messaging.Server.QueueStore.Postgres.Config
127134
Simplex.Messaging.Server.QueueStore.QueueInfo
128135
Simplex.Messaging.ServiceScheme
129136
Simplex.Messaging.Session
@@ -206,11 +213,6 @@ library
206213
Simplex.FileTransfer.Server.Stats
207214
Simplex.FileTransfer.Server.Store
208215
Simplex.FileTransfer.Server.StoreLog
209-
Simplex.Messaging.Agent.Store.Postgres
210-
Simplex.Messaging.Agent.Store.Postgres.Common
211-
Simplex.Messaging.Agent.Store.Postgres.DB
212-
Simplex.Messaging.Agent.Store.Postgres.Migrations
213-
Simplex.Messaging.Agent.Store.Postgres.Util
214216
Simplex.Messaging.Notifications.Server
215217
Simplex.Messaging.Notifications.Server.Control
216218
Simplex.Messaging.Notifications.Server.Env
@@ -226,6 +228,7 @@ library
226228
Simplex.Messaging.Server.Env.STM
227229
Simplex.Messaging.Server.Information
228230
Simplex.Messaging.Server.Main
231+
Simplex.Messaging.Server.Main.Init
229232
Simplex.Messaging.Server.MsgStore
230233
Simplex.Messaging.Server.MsgStore.Journal
231234
Simplex.Messaging.Server.MsgStore.Journal.SharedLock
@@ -235,14 +238,24 @@ library
235238
Simplex.Messaging.Server.Prometheus
236239
Simplex.Messaging.Server.QueueStore
237240
Simplex.Messaging.Server.QueueStore.STM
238-
Simplex.Messaging.Server.QueueStore.Postgres
239-
Simplex.Messaging.Server.QueueStore.Postgres.Migrations
240241
Simplex.Messaging.Server.QueueStore.Types
241242
Simplex.Messaging.Server.Stats
242243
Simplex.Messaging.Server.StoreLog
243244
Simplex.Messaging.Server.StoreLog.ReadWrite
244245
Simplex.Messaging.Server.StoreLog.Types
245246
Simplex.Messaging.Transport.WebSockets
247+
if flag(client_postgres) || flag(server_postgres)
248+
exposed-modules:
249+
Simplex.Messaging.Agent.Store.Postgres
250+
Simplex.Messaging.Agent.Store.Postgres.Common
251+
Simplex.Messaging.Agent.Store.Postgres.DB
252+
Simplex.Messaging.Agent.Store.Postgres.Migrations
253+
Simplex.Messaging.Agent.Store.Postgres.Util
254+
255+
if flag(server_postgres)
256+
exposed-modules:
257+
Simplex.Messaging.Server.QueueStore.Postgres
258+
Simplex.Messaging.Server.QueueStore.Postgres.Migrations
246259
other-modules:
247260
Paths_simplexmq
248261
hs-source-dirs:
@@ -307,21 +320,23 @@ library
307320
case-insensitive ==1.2.*
308321
, hashable ==1.4.*
309322
, ini ==0.4.1
310-
, postgresql-simple ==0.7.*
311323
, optparse-applicative >=0.15 && <0.17
312324
, process ==1.6.*
313-
, raw-strings-qq ==1.1.*
314325
, temporary ==1.3.*
315326
, websockets ==0.12.*
316-
if flag(client_postgres) || !flag(client_library)
327+
if flag(client_postgres) || flag(server_postgres)
317328
build-depends:
318329
postgresql-libpq >=0.10.0.0
330+
, postgresql-simple ==0.7.*
331+
, raw-strings-qq ==1.1.*
319332
if flag(client_postgres)
320333
cpp-options: -DdbPostgres
321334
else
322335
build-depends:
323336
direct-sqlcipher ==2.3.*
324337
, sqlcipher-simple ==0.4.*
338+
if flag(server_postgres)
339+
cpp-options: -DdbServerPostgres
325340
if impl(ghc >= 9.6.2)
326341
build-depends:
327342
bytestring ==0.11.*
@@ -439,7 +454,6 @@ test-suite simplexmq-test
439454
CoreTests.UtilTests
440455
CoreTests.VersionRangeTests
441456
FileDescriptionTests
442-
Fixtures
443457
NtfClient
444458
NtfServerTests
445459
RemoteControl
@@ -455,7 +469,10 @@ test-suite simplexmq-test
455469
Static
456470
Static.Embedded
457471
Paths_simplexmq
458-
if !flag(client_postgres)
472+
if flag(client_postgres)
473+
other-modules:
474+
Fixtures
475+
else
459476
other-modules:
460477
AgentTests.SchemaDump
461478
AgentTests.SQLiteTests
@@ -477,7 +494,6 @@ test-suite simplexmq-test
477494
, crypton-x509
478495
, crypton-x509-store
479496
, crypton-x509-validation
480-
, deepseq ==1.4.*
481497
, directory
482498
, file-embed
483499
, filepath
@@ -491,11 +507,8 @@ test-suite simplexmq-test
491507
, ini
492508
, iso8601-time
493509
, main-tester ==0.2.*
494-
, memory
495510
, mtl
496511
, network
497-
, postgresql-simple ==0.7.*
498-
, process
499512
, QuickCheck ==2.14.*
500513
, random
501514
, silently ==1.2.*
@@ -516,10 +529,15 @@ test-suite simplexmq-test
516529
, yaml
517530
default-language: Haskell2010
518531
if flag(client_postgres)
519-
build-depends:
520-
postgresql-libpq >=0.10.0.0
521-
, raw-strings-qq ==1.1.*
522532
cpp-options: -DdbPostgres
523533
else
524534
build-depends:
525-
sqlcipher-simple
535+
deepseq ==1.4.*
536+
, memory
537+
, process
538+
, sqlcipher-simple
539+
if flag(client_postgres) || flag(server_postgres)
540+
build-depends:
541+
postgresql-simple ==0.7.*
542+
if flag(server_postgres)
543+
cpp-options: -DdbServerPostgres

src/Simplex/Messaging/Agent/Store/Postgres/Common.hs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Control.Concurrent.STM
2020
import Control.Exception (bracket)
2121
import Data.ByteString (ByteString)
2222
import qualified Database.PostgreSQL.Simple as PSQL
23-
import Numeric.Natural
23+
import Simplex.Messaging.Agent.Store.Postgres.Options
2424

2525
-- TODO [postgres] use log_min_duration_statement instead of custom slow queries (SQLite's Connection type)
2626
data DBStore = DBStore
@@ -35,14 +35,6 @@ data DBStore = DBStore
3535
dbNew :: Bool
3636
}
3737

38-
data DBOpts = DBOpts
39-
{ connstr :: ByteString,
40-
schema :: ByteString,
41-
poolSize :: Natural,
42-
createSchema :: Bool
43-
}
44-
deriving (Show)
45-
4638
withConnectionPriority :: DBStore -> Bool -> (PSQL.Connection -> IO a) -> IO a
4739
withConnectionPriority DBStore {dbPool, dbSem} _priority =
4840
bracket
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Simplex.Messaging.Agent.Store.Postgres.Options where
2+
3+
import Data.ByteString (ByteString)
4+
import Numeric.Natural
5+
6+
data DBOpts = DBOpts
7+
{ connstr :: ByteString,
8+
schema :: ByteString,
9+
poolSize :: Natural,
10+
createSchema :: Bool
11+
}
12+
deriving (Show)

src/Simplex/Messaging/Server/CLI.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import qualified Data.X509.File as XF
2727
import Data.X509.Validation (Fingerprint (..))
2828
import Network.Socket (HostName, ServiceName)
2929
import Options.Applicative
30-
import Simplex.Messaging.Agent.Store.Postgres.Common (DBOpts (..))
30+
import Simplex.Messaging.Agent.Store.Postgres.Options (DBOpts (..))
3131
import Simplex.Messaging.Encoding.String
3232
import Simplex.Messaging.Protocol (ProtoServerWithAuth (..), ProtocolServer (..), ProtocolTypeI)
3333
import Simplex.Messaging.Server.Env.STM (AServerStoreCfg (..), ServerStoreCfg (..), StorePaths (..))
34-
import Simplex.Messaging.Server.QueueStore.Postgres (PostgresStoreCfg (..))
34+
import Simplex.Messaging.Server.QueueStore.Postgres.Config (PostgresStoreCfg (..))
3535
import Simplex.Messaging.Transport (ATransport (..), TLS, Transport (..))
3636
import Simplex.Messaging.Transport.Server (AddHTTP, loadFileFingerprint)
3737
import Simplex.Messaging.Transport.WebSockets (WS)

src/Simplex/Messaging/Server/Env/STM.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import Simplex.Messaging.Server.MsgStore.STM
5656
import Simplex.Messaging.Server.MsgStore.Types
5757
import Simplex.Messaging.Server.NtfStore
5858
import Simplex.Messaging.Server.QueueStore
59-
import Simplex.Messaging.Server.QueueStore.Postgres (PostgresStoreCfg (..))
59+
import Simplex.Messaging.Server.QueueStore.Postgres.Config
6060
import Simplex.Messaging.Server.QueueStore.STM (STMQueueStore, setStoreLog)
6161
import Simplex.Messaging.Server.QueueStore.Types
6262
import Simplex.Messaging.Server.Stats
@@ -324,7 +324,7 @@ newProhibitedSub = do
324324
return Sub {subThread = ProhibitSub, delivered}
325325

326326
newEnv :: ServerConfig -> IO Env
327-
newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smpAgentCfg, information, messageExpiration, idleQueueInterval, msgQueueQuota, maxJournalMsgCount, maxJournalStateLines, startOptions} = do
327+
newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smpAgentCfg, information, messageExpiration, idleQueueInterval, msgQueueQuota, maxJournalMsgCount, maxJournalStateLines} = do
328328
serverActive <- newTVarIO True
329329
server <- newServer
330330
msgStore <- case serverStoreCfg of
@@ -339,12 +339,16 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smp
339339
ms <- newMsgStore cfg
340340
loadStoreLog (mkQueue ms) storeLogFile $ stmQueueStore ms
341341
pure $ AMS qt mt ms
342+
#if defined(dbServerPostgres)
342343
ASSCfg qt mt SSCDatabaseJournal {storeCfg, storeMsgsPath'} -> do
343-
let StartOptions {confirmMigrations} = startOptions
344+
let StartOptions {confirmMigrations} = startOptions config
344345
qsCfg = PQStoreCfg (storeCfg {confirmMigrations} :: PostgresStoreCfg)
345346
cfg = mkJournalStoreConfig qsCfg storeMsgsPath' msgQueueQuota maxJournalMsgCount maxJournalStateLines idleQueueInterval
346347
ms <- newMsgStore cfg
347348
pure $ AMS qt mt ms
349+
#else
350+
ASSCfg _ _ SSCDatabaseJournal {} -> noPostgresExit
351+
#endif
348352
ntfStore <- NtfStore <$> TM.emptyIO
349353
random <- C.newRandom
350354
tlsServerCreds <- getCredentials "SMP" smpCredentials
@@ -404,6 +408,12 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smp
404408
_ -> SPMQueues
405409
_ -> SPMMessages
406410

411+
noPostgresExit :: IO a
412+
noPostgresExit = do
413+
putStrLn "Error: server binary is compiled without support for PostgreSQL database."
414+
putStrLn "Please download `smp-server-postgres` or re-compile with `cabal build -fserver_postgres`."
415+
exitFailure
416+
407417
mkJournalStoreConfig :: QStoreCfg s -> FilePath -> Int -> Int -> Int -> Int64 -> JournalStoreConfig s
408418
mkJournalStoreConfig queueStoreCfg storePath msgQueueQuota maxJournalMsgCount maxJournalStateLines idleQueueInterval =
409419
JournalStoreConfig

0 commit comments

Comments
 (0)