All Projects → gvolpe → postgresql-resilient

gvolpe / postgresql-resilient

Licence: Apache-2.0 license
Automatic re-connection support for PostgreSQL.

Programming Languages

haskell
3896 projects
Nix
1067 projects

Projects that are alternatives of or similar to postgresql-resilient

Guardian auth
The Guardian Authentication Implementation Using Ecto/Postgresql Elixir Phoenix [ User Authentication ]
Stars: ✭ 15 (-6.25%)
Mutual labels:  postgres, postgresql-database
Zapatos
Zero-abstraction Postgres for TypeScript: a non-ORM database library
Stars: ✭ 448 (+2700%)
Mutual labels:  postgres, postgresql-database
Postgresqltuner
Simple script to analyse your PostgreSQL database configuration, and give tuning advice
Stars: ✭ 2,214 (+13737.5%)
Mutual labels:  postgres, postgresql-database
Udacity Data Engineering Projects
Few projects related to Data Engineering including Data Modeling, Infrastructure setup on cloud, Data Warehousing and Data Lake development.
Stars: ✭ 458 (+2762.5%)
Mutual labels:  postgres, postgresql-database
Niklick
Rails Versioned API solution template for hipsters! (Ruby, Ruby on Rails, REST API, GraphQL, Docker, RSpec, Devise, Postgress DB)
Stars: ✭ 39 (+143.75%)
Mutual labels:  postgres, postgresql-database
Fullstack Apollo Express Postgresql Boilerplate
💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
Stars: ✭ 1,079 (+6643.75%)
Mutual labels:  postgres, postgresql-database
Embedded Database Spring Test
A library for creating isolated embedded databases for Spring-powered integration tests.
Stars: ✭ 196 (+1125%)
Mutual labels:  postgres, postgresql-database
zenith
Neon: Serverless Postgres. We separated storage and compute to offer autoscaling, branching, and bottomless storage.
Stars: ✭ 4,239 (+26393.75%)
Mutual labels:  postgres
cloud-tileserver
Serve mapbox vectortiles via AWS stack
Stars: ✭ 48 (+200%)
Mutual labels:  postgres
pycroft
The new AG DSN management system
Stars: ✭ 16 (+0%)
Mutual labels:  postgres
aspnet-core-vuejs-template
ASP.NET Core / Vue.js SPA Template App
Stars: ✭ 75 (+368.75%)
Mutual labels:  postgresql-database
google-bigtable-postgres-fdw
Google Bigtable Postgres FDW in Rust
Stars: ✭ 37 (+131.25%)
Mutual labels:  postgres
CovidCode-Service
CovidCode service is an authorization code generation service for the CovidCode-UI.
Stars: ✭ 12 (-25%)
Mutual labels:  postgresql-database
ParseCareKit
Securely synchronize any CareKit 2.1+ based app to a Parse Server Cloud. Compatible with parse-hipaa.
Stars: ✭ 28 (+75%)
Mutual labels:  postgres
graphql-to-sql
GraphQL model to SQL
Stars: ✭ 13 (-18.75%)
Mutual labels:  postgres
integresql
IntegreSQL manages isolated PostgreSQL databases for your integration tests.
Stars: ✭ 475 (+2868.75%)
Mutual labels:  postgres
shuttletracker
🚐 Shuttle tracking for RPI.
Stars: ✭ 44 (+175%)
Mutual labels:  postgres
ChefAPI
API using FastAPI and PostgreSQL for sharing or keeping track of awesome food recipes Based on Oauth2 and JWT 💎
Stars: ✭ 16 (+0%)
Mutual labels:  postgres
postgres-deno
A PostgreSQL extension for Deno: run Typescript in PostgreSQL functions and triggers.
Stars: ✭ 87 (+443.75%)
Mutual labels:  postgres
pgsql-ast-parser
Yet another simple Postgres SQL parser
Stars: ✭ 152 (+850%)
Mutual labels:  postgres

postgresql-resilient

CI Status

Automatic reconnection support for PostgreSQL, built on top of postgresql-simple.

Motivation

The postgresql-simple package gives us the two following functions (see connection management).

connect :: ConnectInfo -> IO Connection
close :: Connection -> IO ()

Once we acquire a Connection, it would work as long as there are no connectivity issues. However, if the PostgreSQL server becomes unreachable even for a second, such Connection will no longer be valid and you will need to close it and try to connect again, which could also fail if the server is still down.

So the tiny postgresql-resilient package provides a ResilientConnection from which we can always get a healthy connection. A background process will take care of checking the connection status and reconnecting when necessary. All with configurable retries and exponential back-offs as well as closing the connection once done using it (guaranteed by bracket).

Therefore, instead of using connect, you can leverage the following function.

withResilientConnection
  :: forall a
   . ResilientSettings
  -> LogHandler
  -> P.ConnectInfo
  -> (ResilientConnection IO -> IO a)
  -> IO a

Note: it only depends on exceptions and postgresql-simple, yielding a tiny footprint.

Quick Start

import           Database.PostgreSQL.Resilient
import qualified Database.PostgreSQL.Simple    as P

withResilientConnection defaultResilientSettings logHandler connectInfo $ \pool ->
  (conn :: P.Connection) <- getConnection pool
  res <- P.query_ conn "SELECT * FROM foo"
  putStrLn $ show res

logHandler :: String -> IO ()
logHandler = putStrLn

connectInfo :: P.ConnectInfo
connectInfo = P.ConnectInfo
  { P.connectHost     = "localhost"
  , P.connectPort     = 5432
  , P.connectUser     = "postgres"
  , P.connectPassword = ""
  , P.connectDatabase = "store"
  }

defaultResilientSettings :: ResilientSettings
defaultResilientSettings = ResilientSettings
  { healthCheckEvery            = 3
  , exponentialBackoffThreshold = 10
  }

E.g. using the managed library for your resources.

import           Control.Monad.Managed

mkConnection :: Managed (ResilientConnection IO)
mkConnection = managed $ withResilientConnection
  defaultResilientSettings logHandler connectInfo

main :: IO ()
main = with mkConnection $ \pool ->
  (conn :: P.Connection) <- getConnection pool
  doSomething conn

Reconnection

Here are the logs of a simple connection example where the PostgreSQL server is shutdown on purpose and it's then brought back up a few seconds later.

$ cabal new-run postgresql-resilient-demo
Up to date
Connecting to PostgreSQL

Checking PostgreSQL connection status
[Only {fromOnly = "PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit"}]

Checking PostgreSQL connection status
[Only {fromOnly = "PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit"}]

Checking PostgreSQL connection status
Closing no longer valid PostgreSQL connection
Connecting to PostgreSQL
libpq: failed (could not connect to server: Connection refused
	Is the server running on host "localhost" (::1) and accepting
	TCP/IP connections on port 5432?
could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?
)
 > Retrying in 1 seconds.

Connecting to PostgreSQL
libpq: failed (could not connect to server: Connection refused
	Is the server running on host "localhost" (::1) and accepting
	TCP/IP connections on port 5432?
could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?
)
 > Retrying in 2 seconds.

Connecting to PostgreSQL
libpq: failed (could not connect to server: Connection refused
	Is the server running on host "localhost" (::1) and accepting
	TCP/IP connections on port 5432?
could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?
)
 > Retrying in 4 seconds.

Connecting to PostgreSQL
Checking PostgreSQL connection status
[Only {fromOnly = "PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit"}]

Checking PostgreSQL connection status
[Only {fromOnly = "PostgreSQL 13.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit"}]

^CClosing PostgreSQL connection
Shutdown PostgreSQL reconnection process

The health-check is performed every 3 seconds by default but it is configurable via the healthCheckEvery setting. The retries are exponential by ^2 seconds with a threshold of 10 seconds, also configurable via exponentialThreshold.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].