on master help sign in
warp: withApplication: use async
commit8beae2be350159fceecfc43a61d500c62e938ec1 authorSönke Hahn <soenkehahn@gmail.com> dateWed 9 Mar 2016, 12:54 (11 years ago) parent5435169
2 files changed+13−27
warp/Network/Wai/Handler/Warp/WithApplication.hs+12−27 warp/warp.cabal+1
warp/Network/Wai/Handler/Warp/WithApplication.hs+12−27
@@ -7,6 +7,7 @@ module Network.Wai.Handler.Warp.WithApplication (
77) where
88
99import Control.Concurrent
10+import Control.Concurrent.Async
1011import Control.Exception
1112import Network.Socket
1213import Network.Wai
@@ -14,12 +15,6 @@ import Network.Wai.Handler.Warp.Run
1415import Network.Wai.Handler.Warp.Settings
1516import Network.Wai.Handler.Warp.Types
1617
17−data App
18− = App {
19− appThread :: ThreadId,
20− appKilled :: Waiter ()
21− }
22−
2318-- | Runs the given 'Application' on a free port. Passes the port to the given
2419-- operation and executes it, while the 'Application' is running. Shuts down the
2520-- server before returning.
@@ -27,27 +22,17 @@ withApplication :: IO Application -> (Port -> IO a) -> IO a
2722withApplication mkApp action = do
2823 app <- mkApp
2924 withFreePort $ \ (port, sock) -> do
30− bracket (start app sock) stop $ \ _ ->
31− action port
32− where
33− start :: Application -> Socket -> IO App
34− start app sock = do
35− started <- mkWaiter
36− killed <- mkWaiter
37− thread <- forkIO $ do
38− let settings =
39− defaultSettings{
40− settingsBeforeMainLoop = notify started ()
41− }
42− runSettingsSocket settings sock app
43− `finally` notify killed ()
44− waitFor started
45− return $ App thread killed
46−
47− stop :: App -> IO ()
48− stop runningApp = do
49− killThread $ appThread runningApp
50− waitFor $ appKilled runningApp
25+ started <- mkWaiter
26+ let settings =
27+ defaultSettings{
28+ settingsBeforeMainLoop = notify started ()
29+ }
30+ result <- race
31+ (runSettingsSocket settings sock app)
32+ (waitFor started >> action port)
33+ case result of
34+ Left () -> throwIO $ ErrorCall "Unexpected: runSettingsSocket exited"
35+ Right x -> return x
5136
5237-- | Same as 'withApplication' but with different exception handling: If the
5338-- given 'Application' throws an exception, 'testWithApplication' will re-throw
warp/warp.cabal+1
@@ -34,6 +34,7 @@ Flag warp-debug
3434Library
3535 Build-Depends: base >= 3 && < 5
3636 , array
37+ , async
3738 , auto-update >= 0.1.3 && < 0.2
3839 , blaze-builder >= 0.4
3940 , bytestring >= 0.9.1.4