2-- Module : Streamly.Internal.Data.Stream.IsStream.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.IsStream.Exception
26import Control.Exception (Exception)
27import Control.Monad.Catch (MonadCatch)
28import Data.Map.Strict (Map)
29import Streamly.Internal.Control.Concurrent (MonadRunInIO, MonadAsync)
30import Streamly.Internal.Data.Stream.IsStream.Type
31 (IsStream(..), fromStreamD, toStreamD)
33import qualified Streamly.Internal.Data.Stream.StreamD as D
37-- >>> import qualified Streamly.Prelude as Stream
38-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (nilM)
40------------------------------------------------------------------------------
42------------------------------------------------------------------------------
44-- | Run the action @m b@ before the stream yields its first element.
46-- Same as the following but more efficient due to fusion:
48-- >>> before action xs = Stream.nilM action <> xs
49-- >>> before action xs = Stream.concatMap (const xs) (Stream.fromEffect action)
53before :: (IsStream t, Monad m) => m b -> t m a -> t m a
54before action xs = fromStreamD $ D.before action $ toStreamD xs
56-- | Like 'after', with following differences:
58-- * action @m b@ won't run if the stream is garbage collected
59-- after partial evaluation.
60-- * Monad @m@ does not require any other constraints.
61-- * has slightly better performance than 'after'.
63-- Same as the following, but with stream fusion:
65-- > after_ action xs = xs <> 'nilM' action
70after_ :: (IsStream t, Monad m) => m b -> t m a -> t m a
71after_ action xs = fromStreamD $ D.after_ action $ toStreamD xs
73-- | Run the action @m b@ whenever the stream @t m a@ stops normally, or if it
74-- is garbage collected after a partial lazy evaluation.
76-- The semantics of the action @m b@ are similar to the semantics of cleanup
77-- action in 'bracket'.
84after :: (IsStream t, MonadRunInIO m)
85 => m b -> t m a -> t m a
86after action xs = fromStreamD $ D.after action $ toStreamD xs
88-- | Run the action @m b@ if the stream aborts due to an exception. The
89-- exception is not caught, simply rethrown.
91-- /Inhibits stream fusion/
94{-# INLINE onException #-}
95onException :: (IsStream t, MonadCatch m) => m b -> t m a -> t m a
96onException action xs = fromStreamD $ D.onException action $ toStreamD xs
98-- | Like 'finally' with following differences:
100-- * action @m b@ won't run if the stream is garbage collected
101-- after partial evaluation.
102-- * does not require a 'MonadAsync' constraint.
103-- * has slightly better performance than 'finally'.
105-- /Inhibits stream fusion/
109{-# INLINE finally_ #-}
110finally_ :: (IsStream t, MonadCatch m) => m b -> t m a -> t m a
111finally_ action xs = fromStreamD $ D.finally_ action $ toStreamD xs
113-- | Run the action @m b@ whenever the stream @t m a@ stops normally, aborts
114-- due to an exception or if it is garbage collected after a partial lazy
117-- The semantics of running the action @m b@ are similar to the cleanup action
118-- semantics described in 'bracket'.
121-- finally release = bracket (return ()) (const release)
124-- /See also 'finally_'/
126-- /Inhibits stream fusion/
130{-# INLINE finally #-}
131finally :: (IsStream t, MonadAsync m, MonadCatch m) => m b -> t m a -> t m a
132finally action xs = fromStreamD $ D.finally action $ toStreamD xs
134-- | Like 'bracket' but with following differences:
136-- * alloc action @m b@ runs with async exceptions enabled
137-- * cleanup action @b -> m c@ won't run if the stream is garbage collected
138-- after partial evaluation.
139-- * does not require a 'MonadAsync' constraint.
140-- * has slightly better performance than 'bracket'.
142-- /Inhibits stream fusion/
146{-# INLINE bracket_ #-}
147bracket_ :: (IsStream t, MonadCatch m)
148 => m b -> (b -> m c) -> (b -> t m a) -> t m a
149bracket_ bef aft bet = fromStreamD $
150 D.bracket_ bef aft (toStreamD . bet)
152-- | Run the alloc action @m b@ with async exceptions disabled but keeping
153-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
154-- output @b@ as input to @b -> t m a@ to generate an output stream.
156-- @b@ is usually a resource under the state of monad @m@, e.g. a file
157-- handle, that requires a cleanup after use. The cleanup action @b -> m c@,
158-- runs whenever the stream ends normally, due to a sync or async exception or
159-- if it gets garbage collected after a partial lazy evaluation.
161-- 'bracket' only guarantees that the cleanup action runs, and it runs with
162-- async exceptions enabled. The action must ensure that it can successfully
163-- cleanup the resource in the face of sync or async exceptions.
165-- When the stream ends normally or on a sync exception, cleanup action runs
166-- immediately in the current thread context, whereas in other cases it runs in
167-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
169-- /See also: 'bracket_'/
171-- /Inhibits stream fusion/
175{-# INLINE bracket #-}
176bracket :: (IsStream t, MonadAsync m, MonadCatch m)
177 => m b -> (b -> m c) -> (b -> t m a) -> t m a
178bracket bef aft = bracket' bef aft aft aft
180-- For a use case of this see the "streamly-process" package. It needs to kill
181-- the process in case of exception or garbage collection, but waits for the
182-- process to terminate in normal cases.
184-- | Like 'bracket' but can use separate cleanup actions depending on the mode
185-- of termination. @bracket' before onStop onGC onException action@ runs
186-- @action@ using the result of @before@. If the stream stops, @onStop@ action
187-- is executed, if the stream is abandoned @onGC@ is executed, if the stream
188-- encounters an exception @onException@ is executed.
191{-# INLINE bracket' #-}
192bracket' :: (IsStream t, MonadAsync m, MonadCatch m)
193 => m b -> (b -> m c) -> (b -> m d) -> (b -> m e) -> (b -> t m a) -> t m a
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 :: (IsStream t, MonadCatch m, Exception e)
211 => (e -> t m a -> t m a) -> t m a -> t 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 :: (IsStream t, MonadCatch m, Exception e)
225 => (e -> t m a) -> t m a -> t 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 :: (IsStream t, 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