on master help sign in
Use auto-update in Warp
commit5108cb1344f6c40057a7676b55b0cb536ab56b50 authorMichael Snoyman <michael@snoyman.com> dateMon 4 Aug 2014, 07:08 (12 years ago) parent61081b2
2 files changed+10−21
warp/Network/Wai/Handler/Warp/Date.hs+7−20 warp/warp.cabal+3−1
warp/Network/Wai/Handler/Warp/Date.hs+7−20
@@ -8,11 +8,8 @@ module Network.Wai.Handler.Warp.Date (
88 ) where
99
1010import Control.Applicative
11−import Control.Concurrent
12−import Control.Exception
13−import Control.Monad
11+import Control.AutoUpdate (defaultUpdateSettings, updateAction, mkAutoUpdate)
1412import Data.ByteString.Char8
15−import Data.IORef
1613
1714#if WINDOWS
1815import Data.Time
@@ -26,28 +23,18 @@ import System.Posix (epochTime)
2623type GMTDate = ByteString
2724
2825-- | The type of the cache of the Date header value.
29−data DateCache = DateCache (IORef GMTDate)
26+type DateCache = IO GMTDate
3027
3128-- | Creating 'DateCache' and executing the action.
3229withDateCache :: (DateCache -> IO a) -> IO a
33−withDateCache action = bracket initialize
34− (\(t,_) -> killThread t)
35− (\(_,dc) -> action dc)
36−
37−initialize :: IO (ThreadId, DateCache)
38−initialize = do
39− dc <- DateCache <$> (getCurrentGMTDate >>= newIORef)
40− t <- forkIO $ forever $ do
41− threadDelay 1000000
42− update dc
43− return (t, dc)
30+withDateCache action = initialize >>= action
31+
32+initialize :: IO DateCache
33+initialize = mkAutoUpdate defaultUpdateSettings { updateAction = getCurrentGMTDate }
4434
4535-- | Getting 'GMTDate' based on 'DateCache'.
4636getDate :: DateCache -> IO GMTDate
47−getDate (DateCache ref) = readIORef ref
48−
49−update :: DateCache -> IO ()
50−update (DateCache ref) = getCurrentGMTDate >>= writeIORef ref
37+getDate = id
5138
5239getCurrentGMTDate :: IO GMTDate
5340#ifdef WINDOWS
warp/warp.cabal+3−1
@@ -1,5 +1,5 @@
11Name: warp
2−Version: 3.0.0.5
2+Version: 3.0.0.6
33Synopsis: A fast, light-weight web server for WAI applications.
44License: MIT
55License-file: LICENSE
@@ -40,6 +40,7 @@ Flag warp-debug
4040Library
4141 Build-Depends: base >= 3 && < 5
4242 , array
43+ , auto-update >= 0.1 && < 0.2
4344 , blaze-builder >= 0.3.3 && < 0.4
4445 , bytestring >= 0.9.1.4
4546 , case-insensitive >= 0.2
@@ -121,6 +122,7 @@ Test-Suite spec
121122 Ghc-Options: -Wall
122123 Build-Depends: base >= 4 && < 5
123124 , array
125+ , auto-update
124126 , blaze-builder >= 0.3.3 && < 0.4
125127 , bytestring >= 0.9.1.4
126128 , case-insensitive >= 0.2