on acc37cf help sign in
270 lines 9.0 kB history raw
1-- |
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
7-- Portability : GHC
9module Streamly.Internal.Data.Stream.IsStream.Exception
10 (
11 before
12 , after_
13 , after
14 , bracket_
15 , bracket
16 , bracket'
17 , onException
18 , finally_
19 , finally
20 , ghandle
21 , handle
22 , retry
23 )
24where
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
35-- $setup
36-- >>> :m
37-- >>> import qualified Streamly.Prelude as Stream
38-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream (nilM)
40------------------------------------------------------------------------------
41-- Exceptions
42------------------------------------------------------------------------------
44-- | Run the action @m b@ before the stream yields its first element.
45--
46-- Same as the following but more efficient due to fusion:
47--
48-- >>> before action xs = Stream.nilM action <> xs
49-- >>> before action xs = Stream.concatMap (const xs) (Stream.fromEffect action)
50--
51-- @since 0.7.0
52{-# INLINE before #-}
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:
57--
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'.
62--
63-- Same as the following, but with stream fusion:
64--
65-- > after_ action xs = xs <> 'nilM' action
66--
67-- /Pre-release/
68--
69{-# INLINE after_ #-}
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.
75--
76-- The semantics of the action @m b@ are similar to the semantics of cleanup
77-- action in 'bracket'.
78--
79-- /See also 'after_'/
80--
81-- @since 0.7.0
82--
83{-# INLINE after #-}
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.
90--
91-- /Inhibits stream fusion/
92--
93-- @since 0.7.0
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:
99--
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'.
104--
105-- /Inhibits stream fusion/
106--
107-- /Pre-release/
108--
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
115-- evaluation.
116--
117-- The semantics of running the action @m b@ are similar to the cleanup action
118-- semantics described in 'bracket'.
119--
120-- @
121-- finally release = bracket (return ()) (const release)
122-- @
123--
124-- /See also 'finally_'/
125--
126-- /Inhibits stream fusion/
127--
128-- @since 0.7.0
129--
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:
135--
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'.
141--
142-- /Inhibits stream fusion/
143--
144-- /Pre-release/
145--
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.
155--
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.
160--
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.
164--
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.
168--
169-- /See also: 'bracket_'/
170--
171-- /Inhibits stream fusion/
172--
173-- @since 0.7.0
174--
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.
183--
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.
189--
190-- /Pre-release/
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.
201--
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.
204--
205-- /Inhibits stream fusion/
206--
207-- /Pre-release/
208--
209{-# INLINE ghandle #-}
210ghandle :: (IsStream t, MonadCatch m, Exception e)
211 => (e -> t m a -> t m a) -> t m a -> t m a
212ghandle handler =
213 fromStreamD
214 . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
215 . toStreamD
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.
219--
220-- /Inhibits stream fusion/
221--
222-- @since 0.7.0
223{-# INLINE handle #-}
224handle :: (IsStream t, MonadCatch m, Exception e)
225 => (e -> t m a) -> t m a -> t m a
226handle handler xs =
227 fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
230-- | @retry@ takes 3 arguments
231--
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.
234--
235-- 2. A handler @han@ that decides how to handle an exception when the exception
236-- cannot be retried.
237--
238-- 3. The stream itself that we want to run this mechanism on.
239--
240-- When evaluating a stream if an exception occurs,
241--
242-- 1. The stream evaluation aborts
243--
244-- 2. The exception is looked up in @m@
245--
246-- a. If the exception exists and the mapped value is > 0 then,
247--
248-- i. The value is decreased by 1.
249--
250-- ii. The stream is resumed from where the exception was called, retrying
251-- the action.
252--
253-- b. If the exception exists and the mapped value is == 0 then the stream
254-- evaluation stops.
255--
256-- c. If the exception does not exist then we handle the exception using
257-- @han@.
258--
259-- /Internal/
260--
261{-# INLINE retry #-}
262retry :: (IsStream t, MonadCatch m, Exception e, Ord e)
263 => Map e Int
264 -- ^ map from exception to retry count
265 -> (e -> t m a)
266 -- ^ default handler for those exceptions that are not in the map
267 -> t m a
268 -> t m a
269retry emap handler inp =
270 fromStreamD $ D.retry emap (toStreamD . handler) $ toStreamD inp