on master help sign in
Merge pull request #543 from yesodweb/reaperDocs

Add example code for Control.Reaper to Haddocks

commite7630c8627ec17523abb0e78b7edf6595c3db212 authorMichael Snoyman <michael@snoyman.com> dateMon 25 Apr 2016, 09:55 (10 years ago) parentcf2ff488f04eca
1 file changed+86−11
auto-update/Control/Reaper.hs+86−11
auto-update/Control/Reaper.hs+86−11
@@ -3,8 +3,18 @@
33
44-- | This module provides the ability to create reapers: dedicated cleanup
55-- threads. These threads will automatically spawn and die based on the
6−-- presence of a workload to process on.
6+-- presence of a workload to process on. Example uses include:
7+--
8+-- * Killing long-running jobs
9+-- * Closing unused connections in a connection pool
10+-- * Pruning a cache of old items (see example below)
11+--
12+-- For real-world usage, search the <https://github.com/yesodweb/wai WAI family of packages>
13+-- for imports of "Control.Reaper".
714module Control.Reaper (
15+ -- * Example: Regularly cleaning a cache
16+ -- $example1
17+
818 -- * Settings
919 ReaperSettings
1020 , defaultReaperSettings
@@ -33,7 +43,7 @@ import Data.IORef (IORef, newIORef, readIORef, writeIORef)
3343-- be a list of @item@s. This is encouraged by 'defaultReaperSettings' and
3444-- 'mkListAction'.
3545--
36−-- Since 0.1.1
46+-- @since 0.1.1
3747data ReaperSettings workload item = ReaperSettings
3848 { reaperAction :: workload -> IO (workload -> workload)
3949 -- ^ The action to perform on a workload. The result of this is a
@@ -46,38 +56,38 @@ data ReaperSettings workload item = ReaperSettings
4656 -- temporary workload. This is incredibly useless; you should
4757 -- definitely override this default.
4858 --
49− -- Since 0.1.1
59+ -- @since 0.1.1
5060 , reaperDelay :: {-# UNPACK #-} !Int
5161 -- ^ Number of microseconds to delay between calls of 'reaperAction'.
5262 --
5363 -- Default: 30 seconds.
5464 --
55− -- Since 0.1.1
65+ -- @since 0.1.1
5666 , reaperCons :: item -> workload -> workload
5767 -- ^ Add an item onto a workload.
5868 --
5969 -- Default: list consing.
6070 --
61− -- Since 0.1.1
71+ -- @since 0.1.1
6272 , reaperNull :: workload -> Bool
6373 -- ^ Check if a workload is empty, in which case the worker thread
6474 -- will shut down.
6575 --
6676 -- Default: 'null'.
6777 --
68− -- Since 0.1.1
78+ -- @since 0.1.1
6979 , reaperEmpty :: workload
7080 -- ^ An empty workload.
7181 --
7282 -- Default: empty list.
7383 --
74− -- Since 0.1.1
84+ -- @since 0.1.1
7585 }
7686
7787-- | Default @ReaperSettings@ value, biased towards having a list of work
7888-- items.
7989--
80−-- Since 0.1.1
90+-- @since 0.1.1
8191defaultReaperSettings :: ReaperSettings [item] item
8292defaultReaperSettings = ReaperSettings
8393 { reaperAction = \wl -> return (wl ++)
@@ -104,11 +114,11 @@ data Reaper workload item = Reaper {
104114data State workload = NoReaper -- ^ No reaper thread
105115 | Workload workload -- ^ The current jobs
106116
107−-- | Create a reaper addition function. This funciton can be used to add
117+-- | Create a reaper addition function. This function can be used to add
108118-- new items to the workload. Spawning of reaper threads will be handled
109119-- for you automatically.
110120--
111−-- Since 0.1.1
121+-- @since 0.1.1
112122mkReaper :: ReaperSettings workload item -> IO (Reaper workload item)
113123mkReaper settings@ReaperSettings{..} = do
114124 stateRef <- newIORef NoReaper
@@ -187,7 +197,7 @@ reaper settings@ReaperSettings{..} stateRef tidRef = do
187197-- return either a new work item, or @Nothing@ if the work item is
188198-- expired.
189199--
190−-- Since 0.1.1
200+-- @since 0.1.1
191201mkListAction :: (item -> IO (Maybe item'))
192202 -> [item]
193203 -> IO ([item'] -> [item'])
@@ -202,3 +212,68 @@ mkListAction f =
202212 Nothing -> front
203213 Just y -> front . (y:)
204214 go front' xs
215+
216+-- $example1
217+-- In this example code, we use a 'Data.Map.Strict.Map' to cache fibonacci numbers, and a 'Reaper' to prune the cache.
218+--
219+-- The @main@ function first creates a 'Reaper', with fields to initialize the
220+-- cache ('reaperEmpty'), add items to it ('reaperCons'), and prune it ('reaperAction').
221+-- The reaper will run every two seconds ('reaperDelay'), but will stop running while
222+-- 'reaperNull' is true.
223+--
224+-- @main@ then loops infinitely ('Control.Monad.forever'). Each second it calculates the fibonacci number
225+-- for a value between 30 and 34, first trying the cache ('reaperRead' and 'Data.Map.Strict.lookup'),
226+-- then falling back to manually calculating it (@fib@)
227+-- and updating the cache with the result ('reaperAdd')
228+--
229+-- @clean@ simply removes items cached for more than 10 seconds.
230+-- This function is where you would perform IO-related cleanup,
231+-- like killing threads or closing connections, if that was the purpose of your reaper.
232+--
233+-- @
234+-- module Main where
235+--
236+-- import "Data.Time" (UTCTime, getCurrentTime, diffUTCTime)
237+-- import "Control.Reaper"
238+-- import "Control.Concurrent" (threadDelay)
239+-- import "Data.Map.Strict" (Map)
240+-- import qualified "Data.Map.Strict" as Map
241+-- import "Control.Monad" (forever)
242+-- import "System.Random" (getStdRandom, randomR)
243+--
244+-- fib :: 'Int' -> 'Int'
245+-- fib 0 = 0
246+-- fib 1 = 1
247+-- fib n = fib (n-1) + fib (n-2)
248+--
249+-- type Cache = 'Data.Map.Strict.Map' 'Int' ('Int', 'Data.Time.Clock.UTCTime')
250+--
251+-- main :: IO ()
252+-- main = do
253+-- reaper <- 'mkReaper' 'defaultReaperSettings'
254+-- { 'reaperEmpty' = Map.'Data.Map.Strict.empty'
255+-- , 'reaperCons' = \\(k, v, time) workload -> Map.'Data.Map.Strict.insert' k (v, time) workload
256+-- , 'reaperAction' = clean
257+-- , 'reaperDelay' = 1000000 * 2 -- Clean every 2 seconds
258+-- , 'reaperNull' = Map.'Data.Map.Strict.null'
259+-- }
260+-- forever $ do
261+-- fibArg <- 'System.Random.getStdRandom' ('System.Random.randomR' (30,34))
262+-- cache <- 'reaperRead' reaper
263+-- let cachedResult = Map.'Data.Map.Strict.lookup' fibArg cache
264+-- case cachedResult of
265+-- 'Just' (fibResult, _createdAt) -> 'putStrLn' $ "Found in cache: `fib " ++ 'show' fibArg ++ "` " ++ 'show' fibResult
266+-- 'Nothing' -> do
267+-- let fibResult = fib fibArg
268+-- 'putStrLn' $ "Calculating `fib " ++ 'show' fibArg ++ "` " ++ 'show' fibResult
269+-- time <- 'Data.Time.Clock.getCurrentTime'
270+-- ('reaperAdd' reaper) (fibArg, fibResult, time)
271+-- 'threadDelay' 1000000 -- 1 second
272+--
273+-- -- Remove items > 10 seconds old
274+-- clean :: Cache -> IO (Cache -> Cache)
275+-- clean oldMap = do
276+-- currentTime <- 'Data.Time.Clock.getCurrentTime'
277+-- let pruned = Map.'Data.Map.Strict.filter' (\\(_, createdAt) -> currentTime \`diffUTCTime\` createdAt < 10.0) oldMap
278+-- return (\\newData -> Map.'Data.Map.Strict.union' pruned newData)
279+-- @