@@ -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