1-- | Timeout requests
2module Network.Wai.Middleware.Timeout
3    ( timeout
4    , timeoutStatus
5    , timeoutAs
6    ) where
7
8import Network.HTTP.Types (Status, status503)
9import Network.Wai
10import qualified System.Timeout as Timeout
11
12-- | Time out the request after the given number of seconds
13--
14-- Timeouts respond with @'status503'@. See @'timeoutStatus'@ or @'timeoutAs'@
15-- to customize the behavior of the timed-out case.
16--
17-- @since 3.0.24.0@
18timeout :: Int -> Middleware
19timeout = timeoutStatus status503
20
21-- | Time out with the given @'Status'@
22--
23-- @since 3.0.24.0@
24timeoutStatus :: Status -> Int -> Middleware
25timeoutStatus status = timeoutAs $ responseLBS status [] ""
26
27-- | Time out with the given @'Response'@
28--
29-- @since 3.0.24.0@
30timeoutAs :: Response -> Int -> Middleware
31timeoutAs timeoutReponse seconds app req respond =
32    maybe (respond timeoutReponse) pure
33        =<< Timeout.timeout (seconds * 1000000) (app req respond)
34