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
9module Streamly.Internal.Data.Stream.Exception
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
36-- >>> import qualified Streamly.Internal.Data.Stream as Stream
38------------------------------------------------------------------------------
40------------------------------------------------------------------------------
42-- | Run the action @m b@ before the stream yields its first element.
44-- Same as the following but more efficient due to fusion:
46-- >>> before action xs = Stream.nilM action <> xs
47-- >>> before action xs = Stream.concatMap (const xs) (Stream.fromEffect action)
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:
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'.
61-- Same as the following, but with stream fusion:
63-- >>> after_ action xs = xs <> Stream.nilM action
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.
74-- The semantics of the action @IO b@ are similar to the semantics of cleanup
75-- action in 'bracketIO'.
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.
86-- /Inhibits stream fusion/
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:
95-- * action @m b@ won't run if the stream is garbage collected
96-- after partial evaluation.
97-- * has slightly better performance than 'finallyIO'.
99-- /Inhibits stream fusion/
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
111-- The semantics of running the action @IO b@ are similar to the cleanup action
112-- semantics described in 'bracketIO'.
114-- >>> finallyIO release = Stream.bracketIO (return ()) (const release)
116-- /See also 'finally_'/
118-- /Inhibits stream fusion/
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:
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'.
132-- /Inhibits stream fusion/
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.
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.
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.
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.
159-- /See also: 'bracket_'/
161-- /Inhibits stream fusion/
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.
179{-# INLINE bracket3IO #-}
180bracket3IO :: (MonadIO m, MonadCatch m)
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.
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.
198-- /Inhibits stream fusion/
202{-# INLINE ghandle #-}
203ghandle :: (MonadCatch m, Exception e)
204 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
207 . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
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.
213-- /Inhibits stream fusion/
217handle :: (MonadCatch m, Exception e)
218 => (e -> Stream m a) -> Stream m a -> Stream m a
220 fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
223-- | @retry@ takes 3 arguments
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.
228-- 2. A handler @han@ that decides how to handle an exception when the exception
231-- 3. The stream itself that we want to run this mechanism on.
233-- When evaluating a stream if an exception occurs,
235-- 1. The stream evaluation aborts
237-- 2. The exception is looked up in @m@
239-- a. If the exception exists and the mapped value is > 0 then,
241-- i. The value is decreased by 1.
243-- ii. The stream is resumed from where the exception was called, retrying
246-- b. If the exception exists and the mapped value is == 0 then the stream
249-- c. If the exception does not exist then we handle the exception using
255retry :: (MonadCatch m, Exception e, Ord e)
257 -- ^ map from exception to retry count
259 -- ^ default handler for those exceptions that are not in the map
262retry emap handler inp =
263 fromStreamD $ D.retry emap (toStreamD . handler) $ toStreamD inp
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list