mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-22 23:11:41 +03:00
329f3f0a39
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5836 GitOrigin-RevId: 2f94cdee01702e49ff130f6a40f6be26fa69ec88
31 lines
954 B
Haskell
31 lines
954 B
Haskell
module Hasura.ShutdownLatch
|
|
( ShutdownLatch,
|
|
newShutdownLatch,
|
|
shutdownGracefully,
|
|
waitForShutdown,
|
|
shuttingDown,
|
|
)
|
|
where
|
|
|
|
import Control.Concurrent.Extended qualified as C
|
|
import Hasura.Prelude
|
|
|
|
-- | A latch for the graceful shutdown of a server process.
|
|
newtype ShutdownLatch = ShutdownLatch {unShutdownLatch :: C.MVar ()}
|
|
|
|
newShutdownLatch :: IO ShutdownLatch
|
|
newShutdownLatch = fmap ShutdownLatch C.newEmptyMVar
|
|
|
|
-- | Block the current thread, waiting on the latch.
|
|
waitForShutdown :: ShutdownLatch -> IO ()
|
|
waitForShutdown = C.readMVar . unShutdownLatch
|
|
|
|
-- | Initiate a graceful shutdown of the server associated with the provided
|
|
-- latch.
|
|
shutdownGracefully :: ShutdownLatch -> IO ()
|
|
shutdownGracefully = void . flip C.tryPutMVar () . unShutdownLatch
|
|
|
|
-- | Returns True if the latch is set for shutdown and vice-versa
|
|
shuttingDown :: ShutdownLatch -> IO Bool
|
|
shuttingDown latch = not <$> C.isEmptyMVar (unShutdownLatch latch)
|