on 106939a help sign in
263 lines 8.7 kB history raw
1-- |
2-- Module : Streamly.Internal.Data.Stream.Exception
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
7-- Portability : GHC
9module Streamly.Internal.Data.Stream.Exception
10 (
11 before
12 , after_
13 , afterIO
14 , bracket_
15 , bracketIO
16 , bracket3IO
17 , onException
18 , finally_
19 , finallyIO
20 , ghandle
21 , handle
22 , retry
23 )
24where
26import Control.Exception (Exception)
27import Control.Monad.Catch (MonadCatch)
28import Control.Monad.IO.Class (MonadIO)
29import Data.Map.Strict (Map)
30import Streamly.Internal.Data.Stream.Type (Stream, fromStreamD, toStreamD)
32import qualified Streamly.Internal.Data.Stream.StreamD as D
34-- $setup
35-- >>> :m
36-- >>> import qualified Streamly.Internal.Data.Stream as Stream
38------------------------------------------------------------------------------
39-- Exceptions
40------------------------------------------------------------------------------
42-- | Run the action @m b@ before the stream yields its first element.
43--
44-- Same as the following but more efficient due to fusion:
45--
46-- >>> before action xs = Stream.nilM action <> xs
47-- >>> before action xs = Stream.concatMap (const xs) (Stream.fromEffect action)
48--
49-- @since 0.9.0
50{-# INLINE before #-}
51before :: Monad m => m b -> Stream m a -> Stream m a
52before action xs = fromStreamD $ D.before action $ toStreamD xs
54-- | Like 'after', with following differences:
55--
56-- * action @m b@ won't run if the stream is garbage collected
57-- after partial evaluation.
58-- * Monad @m@ does not require any other constraints.
59-- * has slightly better performance than 'after'.
60--
61-- Same as the following, but with stream fusion:
62--
63-- >>> after_ action xs = xs <> Stream.nilM action
64--
65-- /Pre-release/
66--
67{-# INLINE after_ #-}
68after_ :: Monad m => m b -> Stream m a -> Stream m a
69after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
71-- | Run the action @IO b@ whenever the stream stream stops normally, or
72-- if it is garbage collected after a partial lazy evaluation.
73--
74-- The semantics of the action @IO b@ are similar to the semantics of cleanup
75-- action in 'bracketIO'.
76--
77-- /See also 'after_'/
78--
79{-# INLINE afterIO #-}
80afterIO :: MonadIO m => IO b -> Stream m a -> Stream m a
81afterIO action xs = fromStreamD $ D.after action $ toStreamD xs
83-- | Run the action @m b@ if the stream aborts due to an exception. The
84-- exception is not caught, simply rethrown.
85--
86-- /Inhibits stream fusion/
87--
88-- @since 0.9.0
89{-# INLINE onException #-}
90onException :: MonadCatch m => m b -> Stream m a -> Stream m a
91onException action xs = fromStreamD $ D.onException action $ toStreamD xs
93-- | Like 'finally' with following differences:
94--
95-- * action @m b@ won't run if the stream is garbage collected
96-- after partial evaluation.
97-- * has slightly better performance than 'finallyIO'.
98--
99-- /Inhibits stream fusion/
100--
101-- /Pre-release/
102--
103{-# INLINE finally_ #-}
104finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
105finally_ action xs = fromStreamD $ D.finally_ action $ toStreamD xs
107-- | Run the action @IO b@ whenever the stream stream stops normally, aborts
108-- due to an exception or if it is garbage collected after a partial lazy
109-- evaluation.
110--
111-- The semantics of running the action @IO b@ are similar to the cleanup action
112-- semantics described in 'bracketIO'.
113--
114-- >>> finallyIO release = Stream.bracketIO (return ()) (const release)
115--
116-- /See also 'finally_'/
117--
118-- /Inhibits stream fusion/
119--
120{-# INLINE finallyIO #-}
121finallyIO :: (MonadIO m, MonadCatch m) =>
122 IO b -> Stream m a -> Stream m a
123finallyIO action xs = fromStreamD $ D.finally action $ toStreamD xs
125-- | Like 'bracket' but with following differences:
126--
127-- * alloc action @m b@ runs with async exceptions enabled
128-- * cleanup action @b -> m c@ won't run if the stream is garbage collected
129-- after partial evaluation.
130-- * has slightly better performance than 'bracketIO'.
131--
132-- /Inhibits stream fusion/
133--
134-- /Pre-release/
135--
136{-# INLINE bracket_ #-}
137bracket_ :: MonadCatch m
138 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
139bracket_ bef aft bet = fromStreamD $
140 D.bracket_ bef aft (toStreamD . bet)
142-- | Run the alloc action @IO b@ with async exceptions disabled but keeping
143-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
144-- output @b@ as input to @b -> Stream m a@ to generate an output stream.
145--
146-- @b@ is usually a resource under the IO monad, e.g. a file handle, that
147-- requires a cleanup after use. The cleanup action @b -> IO c@, runs whenever
148-- the stream ends normally, due to a sync or async exception or if it gets
149-- garbage collected after a partial lazy evaluation.
150--
151-- 'bracketIO' only guarantees that the cleanup action runs, and it runs with
152-- async exceptions enabled. The action must ensure that it can successfully
153-- cleanup the resource in the face of sync or async exceptions.
154--
155-- When the stream ends normally or on a sync exception, cleanup action runs
156-- immediately in the current thread context, whereas in other cases it runs in
157-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
158--
159-- /See also: 'bracket_'/
160--
161-- /Inhibits stream fusion/
162--
163{-# INLINE bracketIO #-}
164bracketIO :: (MonadIO m, MonadCatch m)
165 => IO b -> (b -> IO c) -> (b -> Stream m a) -> Stream m a
166bracketIO bef aft = bracket3IO bef aft aft aft
168-- For a use case of this see the "streamly-process" package. It needs to kill
169-- the process in case of exception or garbage collection, but waits for the
170-- process to terminate in normal cases.
172-- | Like 'bracketIO' but can use separate cleanup actions depending on the mode
173-- of termination. @bracket3IO before onStop onGC onException action@ runs
174-- @action@ using the result of @before@. If the stream stops, @onStop@ action
175-- is executed, if the stream is abandoned @onGC@ is executed, if the stream
176-- encounters an exception @onException@ is executed.
177--
178-- /Pre-release/
179{-# INLINE bracket3IO #-}
180bracket3IO :: (MonadIO m, MonadCatch m)
181 => IO b
182 -> (b -> IO c)
183 -> (b -> IO d)
184 -> (b -> IO e)
185 -> (b -> Stream m a)
186 -> Stream m a
187bracket3IO bef aft gc exc bet = fromStreamD $
188 D.bracket' bef aft exc gc (toStreamD . bet)
190-- | Like 'handle' but the exception handler is also provided with the stream
191-- that generated the exception as input. The exception handler can thus
192-- re-evaluate the stream to retry the action that failed. The exception
193-- handler can again call 'ghandle' on it to retry the action multiple times.
194--
195-- This is highly experimental. In a stream of actions we can map the stream
196-- with a retry combinator to retry each action on failure.
197--
198-- /Inhibits stream fusion/
199--
200-- /Pre-release/
201--
202{-# INLINE ghandle #-}
203ghandle :: (MonadCatch m, Exception e)
204 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
205ghandle handler =
206 fromStreamD
207 . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
208 . toStreamD
210-- | When evaluating a stream if an exception occurs, stream evaluation aborts
211-- and the specified exception handler is run with the exception as argument.
212--
213-- /Inhibits stream fusion/
214--
215-- @since 0.9.0
216{-# INLINE handle #-}
217handle :: (MonadCatch m, Exception e)
218 => (e -> Stream m a) -> Stream m a -> Stream m a
219handle handler xs =
220 fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
223-- | @retry@ takes 3 arguments
224--
225-- 1. A map @m@ whose keys are exceptions and values are the number of times to
226-- retry the action given that the exception occurs.
227--
228-- 2. A handler @han@ that decides how to handle an exception when the exception
229-- cannot be retried.
230--
231-- 3. The stream itself that we want to run this mechanism on.
232--
233-- When evaluating a stream if an exception occurs,
234--
235-- 1. The stream evaluation aborts
236--
237-- 2. The exception is looked up in @m@
238--
239-- a. If the exception exists and the mapped value is > 0 then,
240--
241-- i. The value is decreased by 1.
242--
243-- ii. The stream is resumed from where the exception was called, retrying
244-- the action.
245--
246-- b. If the exception exists and the mapped value is == 0 then the stream
247-- evaluation stops.
248--
249-- c. If the exception does not exist then we handle the exception using
250-- @han@.
251--
252-- /Internal/
253--
254{-# INLINE retry #-}
255retry :: (MonadCatch m, Exception e, Ord e)
256 => Map e Int
257 -- ^ map from exception to retry count
258 -> (e -> Stream m a)
259 -- ^ default handler for those exceptions that are not in the map
260 -> Stream m a
261 -> Stream m a
262retry emap handler inp =
263 fromStreamD $ D.retry emap (toStreamD . handler) $ toStreamD inp