network-wait
Lightweight library for waiting on networked services to become available.
https://github.com/mbg/network-wait#readme
| LTS Haskell 24.16: | 0.2.0.0 |
| Stackage Nightly 2025-10-24: | 0.4.0.0 |
| Latest on Hackage: | 0.4.0.0 |
network-wait-0.4.0.0@sha256:d4b6fe36b8930cc9513626e5077af2a26e001030e77e3b562c6ca968d1b47e06,3268Module documentation for 0.4.0.0
- Network
network-wait
A lightweight Haskell library for waiting on networked services to become available. This is useful if you are e.g. building a web application which relies on a database server to be available, but which may not be immediately available on application startup.
Full Haddock documentation for all modules
Example
All functions provided by this library work with retry policies from Control.Retry which provide good control over the retry behaviour. To wait for a PostgreSQL server to become available on the same machine:
import Control.Retry
import Network.Wait
main :: IO ()
main = do
waitTcp retryPolicyDefault "localhost" "5432"
putStrLn "Yay, the server is available!"
The haddock documentation for this library contains more examples.
Example: PostgreSQL
The network-wait package can be compiled with the network-wait:postgres flag (e.g. stack build --flag network-wait:postgres) which enables support for checking the readiness of PostgreSQL servers specifically. Unlike the functions in the Network.Wait module, which only check that connections can be established, the functions in Network.Wait.PostgreSQL also check that a PostgreSQL server is ready to accept commands. To wait for a PostgreSQL server to become available and accept commands:
import Control.Retry (retryPolicyDefault)
import Database.PostgreSQL.Simple (defaultConnectInfo)
import Network.Wait.PostgreSQL (waitPostgreSQL)
main :: IO ()
main = do
waitPostgreSQL retryPolicyDefault defaultConnectInfo
putStrLn "Yay, the PostgreSQL server is ready to accept commands!"
Internally, this uses postgresql-simple to connect to the specified server (defaultConnectInfo in the example above) and send a SELECT 1; query. If the query is answered correctly, we consider the server to be in a state ready to accept commands.
Alternatively, a connection string may be used instead of a ConnectInfo value:
import Data.ByteString (ByteString)
import Control.Retry (retryPolicyDefault)
import Database.PostgreSQL.Simple (defaultConnectInfo)
import Network.Wait.PostgreSQL (waitPostgreSQL)
connStr :: ByteString
connStr = "host=localhost port=5432"
main :: IO ()
main = do
waitPostgreSQL retryPolicyDefault connStr
putStrLn "Yay, the PostgreSQL server is ready to accept commands!"
The Network.Wait.PostgreSQL module is gated behind the network-wait:postgres flag so that the PostgreSQL-specific dependencies are only required when PostgresSQL support is required by users of this library.
Example: Redis
The network-wait package can be compiled with the network-wait:redis flag (e.g. stack build --flag network-wait:redis) which enables support for checking the readiness of Redis servers specifically. Unlike the functions in the Network.Wait module, which only check that connections can be established, the functions in Network.Wait.Redis also check that a Redis server is ready to accept commands. To wait for a Redis server to become available and accept commands:
import Control.Retry (retryPolicyDefault)
import Database.Redis (defaultConnectInfo)
import Network.Wait.Redis (waitRedis)
main :: IO ()
main = do
waitRedis retryPolicyDefault defaultConnectInfo
putStrLn "Yay, the Redis server is ready to accept commands!"
Internally, this uses hedis to connect to the specified server (defaultConnectInfo in the example above) and send a ping. If the ping is answered, we consider the server to be in a state ready to accept commands.
The Network.Wait.Redis module is gated behind the network-wait:redis flag so that the Redis-specific dependencies are only required when Redis support is required by users of this library.
See also
- wait-for is a popular shell script with the same objectives as this library.
- The port-utils package has some functions for waiting on TCP servers to become available, with a fixed timeout.
Changes
Changelog for network-wait
0.4.0
- Fixed an issue where under some circumstances an attempt to connect to a socket could become stuck for a long time or possibly forever. A timeout is now applied to all connection attempts made by functions in the
Network.Waitmodule to ensure that the checks terminate in a reasonable amount of time. Contributed by @thomasjm in #20. - Added a new
recoveringWithStatusfunction which provides theRetryStatusto the action.
0.3.0
- Functions in the
Network.Wait.PostgreSQLmodule are now overloaded to accept different types of connection information. In addition to the previously supportedConnectInfotype, the function now also accept connection strings in the form ofByteStringvalues.
0.2.0
- Add
Network.Wait.Redismodule with functions to wait for Redis servers to become ready to accept connections. This module and its dependency onhedisare not enabled by default. Thenetwork-wait:redisflag must be enabled for this package’s Redis support. - Generalise the
recoveringWithfunction to allow it to return the result of a computation. - The functions for TCP/Sockets now return the
Socketif they are successful. - The functions for PostgreSQL now return the
Connectionif they are successful.
0.1.2
- Compatibility with GHC 8.2 and Stack LTS 11
0.1.1
- Add
Network.Wait.PostgreSQLmodule with functions to wait for PostgreSQL servers to become ready to accept connections. This module and its dependency onpostgresql-simpleare not enabled by default. Thenetwork-wait:postgresflag must be enabled for this package’s PostgreSQL support.
0.1.0
- First release