on master help sign in
Merge pull request #380 from tvh/faster-shutdown

Shutdown faster.

commit592abdb793944cf1ad36c52ce0fefc79d9886745 authorKazu Yamamoto <kazu@iij.ad.jp> dateWed 10 Jun 2015, 14:12 (11 years ago) parentca9321c83ca195
3 files changed+14−13
warp/Network/Wai/Handler/Warp/Counter.hs+11−8 warp/Network/Wai/Handler/Warp/Run.hs+1−5 warp/warp.cabal+2
warp/Network/Wai/Handler/Warp/Counter.hs+11−8
@@ -1,24 +1,27 @@
11module Network.Wai.Handler.Warp.Counter (
22 Counter
33 , newCounter
4− , isZero
4+ , waitForZero
55 , increase
66 , decrease
77 ) where
88
9−import Network.Wai.Handler.Warp.IORef
9+import Control.Concurrent.STM
10+import Control.Monad (unless)
1011import Control.Applicative ((<$>))
1112
12−newtype Counter = Counter (IORef Int)
13+newtype Counter = Counter (TVar Int)
1314
1415newCounter :: IO Counter
15−newCounter = Counter <$> newIORef 0
16+newCounter = Counter <$> newTVarIO 0
1617
17−isZero :: Counter -> IO Bool
18−isZero (Counter ref) = (== 0) <$> readIORef ref
18+waitForZero :: Counter -> IO ()
19+waitForZero (Counter ref) = atomically $ do
20+ x <- readTVar ref
21+ unless (x == 0) retry
1922
2023increase :: Counter -> IO ()
21−increase (Counter ref) = atomicModifyIORef' ref $ \x -> (x + 1, ())
24+increase (Counter ref) = atomically $ modifyTVar' ref $ \x -> x + 1
2225
2326decrease :: Counter -> IO ()
24−decrease (Counter ref) = atomicModifyIORef' ref $ \x -> (x - 1, ())
27+decrease (Counter ref) = atomically $ modifyTVar' ref $ \x -> x - 1
warp/Network/Wai/Handler/Warp/Run.hs+1−5
@@ -467,8 +467,4 @@ setSocketCloseOnExec socket =
467467#endif
468468
469469gracefulShutdown :: Counter -> IO ()
470−gracefulShutdown counter = do
471− -- To avoid race condition, we just use threadDelay, not MVar.
472− threadDelay 10000000
473− noConnections <- isZero counter
474− unless noConnections $ gracefulShutdown counter
470+gracefulShutdown counter = waitForZero counter
warp/warp.cabal+2
@@ -46,6 +46,7 @@ Library
4646 , text
4747 , streaming-commons >= 0.1.10
4848 , vault >= 0.3
49+ , stm >= 2.3
4950 if flag(network-bytestring)
5051 Build-Depends: network >= 2.2.1.5 && < 2.2.3
5152 , network-bytestring >= 0.1.3 && < 0.1.4
@@ -143,6 +144,7 @@ Test-Suite spec
143144 , streaming-commons >= 0.1.10
144145 , async
145146 , vault
147+ , stm >= 2.3
146148 if flag(use-bytestring-builder)
147149 Build-Depends: bytestring < 0.10.2.0
148150 , bytestring-builder