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 Control.Monad.Trans.Control (MonadBaseControl)
30import Data.Map.Strict (Map)
31import Streamly.Internal.Data.Stream.Type (Stream, fromStreamD, toStreamD)
33import qualified Streamly.Internal.Data.Stream.StreamD as D
37-- >>> import qualified Streamly.Internal.Data.Stream as Stream
39------------------------------------------------------------------------------
41------------------------------------------------------------------------------
43-- | Run the action @m b@ before the stream yields its first element.
45-- Same as the following but more efficient due to fusion:
47-- >>> before action xs = Stream.nilM action <> xs
48-- >>> before action xs = Stream.concatMap (const xs) (Stream.fromEffect action)
52before :: Monad m => m b -> Stream m a -> Stream m a
53before action xs = fromStreamD $ D.before action $ toStreamD xs
55-- | Like 'after', with following differences:
57-- * action @m b@ won't run if the stream is garbage collected
58-- after partial evaluation.
59-- * Monad @m@ does not require any other constraints.
60-- * has slightly better performance than 'after'.
62-- Same as the following, but with stream fusion:
64-- >>> after_ action xs = xs <> Stream.nilM action
69after_ :: Monad m => m b -> Stream m a -> Stream m a
70after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
72-- | Run the action @m b@ whenever the stream @Stream m a@ stops normally, or
73-- if it is garbage collected after a partial lazy evaluation.
75-- The semantics of the action @m b@ are similar to the semantics of cleanup
76-- action in 'bracket'.
82after :: (MonadIO m, MonadBaseControl IO m)
83 => m b -> Stream m a -> Stream m a
84after action xs = fromStreamD $ D.after action $ toStreamD xs
86-- | Run the action @m b@ if the stream aborts due to an exception. The
87-- exception is not caught, simply rethrown.
89-- /Inhibits stream fusion/
92{-# INLINE onException #-}
93onException :: MonadCatch m => m b -> Stream m a -> Stream m a
94onException action xs = fromStreamD $ D.onException action $ toStreamD xs
96-- | Like 'finally' with following differences:
98-- * action @m b@ won't run if the stream is garbage collected
99-- after partial evaluation.
100-- * does not require a 'MonadAsync' constraint.
101-- * has slightly better performance than 'finally'.
103-- /Inhibits stream fusion/
107{-# INLINE finally_ #-}
108finally_ :: MonadCatch m => m b -> Stream m a -> Stream m a
109finally_ action xs = fromStreamD $ D.finally_ action $ toStreamD xs
111-- | Run the action @m b@ whenever the stream @Stream m a@ stops normally,
112-- aborts due to an exception or if it is garbage collected after a partial
115-- The semantics of running the action @m b@ are similar to the cleanup action
116-- semantics described in 'bracket'.
118-- >>> finally release = Stream.bracket (return ()) (const release)
120-- /See also 'finally_'/
122-- /Inhibits stream fusion/
125{-# INLINE finally #-}
126finally :: (MonadIO m, MonadBaseControl IO m, MonadCatch m) =>
127 m b -> Stream m a -> Stream m a
128finally action xs = fromStreamD $ D.finally action $ toStreamD xs
130-- | Like 'bracket' but with following differences:
132-- * alloc action @m b@ runs with async exceptions enabled
133-- * cleanup action @b -> m c@ won't run if the stream is garbage collected
134-- after partial evaluation.
135-- * does not require a 'MonadAsync' constraint.
136-- * has slightly better performance than 'bracket'.
138-- /Inhibits stream fusion/
142{-# INLINE bracket_ #-}
143bracket_ :: MonadCatch m
144 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
145bracket_ bef aft bet = fromStreamD $
146 D.bracket_ bef aft (toStreamD . bet)
148-- | Run the alloc action @m b@ with async exceptions disabled but keeping
149-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
150-- output @b@ as input to @b -> Stream m a@ to generate an output stream.
152-- @b@ is usually a resource under the state of monad @m@, e.g. a file
153-- handle, that requires a cleanup after use. The cleanup action @b -> m c@,
154-- runs whenever the stream ends normally, due to a sync or async exception or
155-- if it gets garbage collected after a partial lazy evaluation.
157-- 'bracket' only guarantees that the cleanup action runs, and it runs with
158-- async exceptions enabled. The action must ensure that it can successfully
159-- cleanup the resource in the face of sync or async exceptions.
161-- When the stream ends normally or on a sync exception, cleanup action runs
162-- immediately in the current thread context, whereas in other cases it runs in
163-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
165-- /See also: 'bracket_'/
167-- /Inhibits stream fusion/
170{-# INLINE bracket #-}
171bracket :: (MonadIO m, MonadBaseControl IO m, MonadCatch m)
172 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
173bracket bef aft = bracket' bef aft aft aft
175-- For a use case of this see the "streamly-process" package. It needs to kill
176-- the process in case of exception or garbage collection, but waits for the
177-- process to terminate in normal cases.
179-- | Like 'bracket' but can use separate cleanup actions depending on the mode
180-- of termination. @bracket' before onStop onGC onException action@ runs
181-- @action@ using the result of @before@. If the stream stops, @onStop@ action
182-- is executed, if the stream is abandoned @onGC@ is executed, if the stream
183-- encounters an exception @onException@ is executed.
186{-# INLINE bracket' #-}
187bracket' :: (MonadIO m, MonadBaseControl IO m, MonadCatch m)
194bracket' bef aft gc exc bet = fromStreamD $
195 D.bracket' bef aft exc gc (toStreamD . bet)
197-- | Like 'handle' but the exception handler is also provided with the stream
198-- that generated the exception as input. The exception handler can thus
199-- re-evaluate the stream to retry the action that failed. The exception
200-- handler can again call 'ghandle' on it to retry the action multiple times.
202-- This is highly experimental. In a stream of actions we can map the stream
203-- with a retry combinator to retry each action on failure.
205-- /Inhibits stream fusion/
209{-# INLINE ghandle #-}
210ghandle :: (MonadCatch m, Exception e)
211 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
214 . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
217-- | When evaluating a stream if an exception occurs, stream evaluation aborts
218-- and the specified exception handler is run with the exception as argument.
220-- /Inhibits stream fusion/
224handle :: (MonadCatch m, Exception e)
225 => (e -> Stream m a) -> Stream m a -> Stream m a
227 fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
230-- | @retry@ takes 3 arguments
232-- 1. A map @m@ whose keys are exceptions and values are the number of times to
233-- retry the action given that the exception occurs.
235-- 2. A handler @han@ that decides how to handle an exception when the exception
238-- 3. The stream itself that we want to run this mechanism on.
240-- When evaluating a stream if an exception occurs,
242-- 1. The stream evaluation aborts
244-- 2. The exception is looked up in @m@
246-- a. If the exception exists and the mapped value is > 0 then,
248-- i. The value is decreased by 1.
250-- ii. The stream is resumed from where the exception was called, retrying
253-- b. If the exception exists and the mapped value is == 0 then the stream
256-- c. If the exception does not exist then we handle the exception using
262retry :: (MonadCatch m, Exception e, Ord e)
264 -- ^ map from exception to retry count
266 -- ^ default handler for those exceptions that are not in the map
269retry emap handler inp =
270 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