on master help sign in
Refactor gbracket, Add ghandle to retry the stream on exception.
commit971da7752806050a751200c450dc0ab1d15477cd authorPranay Sashank <pranaysashank@composewell.com> dateSat 22 Aug 2020, 13:59 (6 years ago) parentc8c161c
1 file changed+20−8
src/Streamly/Internal/Data/Stream/StreamD.hs+20−8
src/Streamly/Internal/Data/Stream/StreamD.hs+20−8
@@ -292,6 +292,7 @@ module Streamly.Internal.Data.Stream.StreamD
292292 , onException
293293 , finally_
294294 , finally
295+ , ghandle
295296 , handle
296297
297298 -- * Concurrent Application
@@ -3138,7 +3139,7 @@ gbracket_
31383139 => m c -- ^ before
31393140 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
31403141 -> (c -> m d) -- ^ after, on normal stop
3141− -> (c -> e -> Stream m b) -- ^ on exception
3142+ -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
31423143 -> (c -> Stream m b) -- ^ stream generator
31433144 -> Stream m b
31443145gbracket_ bef exc aft fexc fnormal =
@@ -3159,7 +3160,8 @@ gbracket_ bef exc aft fexc fnormal =
31593160 return $ Yield x (GBracketNormal (Stream step1 s) v)
31603161 Skip s -> return $ Skip (GBracketNormal (Stream step1 s) v)
31613162 Stop -> aft v >> return Stop
3162− Left e -> return $ Skip (GBracketException (fexc v e))
3163+ Left e ->
3164+ return $ Skip (GBracketException (fexc v e (UnStream step1 st)))
31633165 step gst (GBracketException (UnStream step1 st)) = do
31643166 res <- step1 gst st
31653167 case res of
@@ -3220,7 +3222,7 @@ gbracket
32203222 => m c -- ^ before
32213223 -> (forall s. m s -> m (Either e s)) -- ^ try (exception handling)
32223224 -> (c -> m d) -- ^ after, on normal stop or GC
3223− -> (c -> e -> Stream m b) -- ^ on exception
3225+ -> (c -> e -> Stream m b -> Stream m b) -- ^ on exception
32243226 -> (c -> Stream m b) -- ^ stream generator
32253227 -> Stream m b
32263228gbracket bef exc aft fexc fnormal =
@@ -3256,7 +3258,7 @@ gbracket bef exc aft fexc fnormal =
32563258 return Stop
32573259 Left e -> do
32583260 clearIORefFinalizer ref
3259− return $ Skip (GBracketIOException (fexc v e))
3261+ return $ Skip (GBracketIOException (fexc v e (UnStream step1 st)))
32603262 step gst (GBracketIOException (UnStream step1 st)) = do
32613263 res <- step1 gst st
32623264 case res of
@@ -3332,7 +3334,7 @@ after action (Stream step state) = Stream step' Nothing
33323334onException :: MonadCatch m => m b -> Stream m a -> Stream m a
33333335onException action str =
33343336 gbracket_ (return ()) MC.try return
3335− (\_ (e :: MC.SomeException) -> nilM (action >> MC.throwM e))
3337+ (\_ (e :: MC.SomeException) _ -> nilM (action >> MC.throwM e))
33363338 (\_ -> str)
33373339
33383340{-# INLINE_NORMAL _onException #-}
@@ -3361,7 +3363,7 @@ _onException action (Stream step state) = Stream step' state
33613363bracket_ :: MonadCatch m => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
33623364bracket_ bef aft bet =
33633365 gbracket_ bef MC.try aft
3364− (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3366+ (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e)) bet
33653367
33663368-- | Run the first action before the stream starts and remember its output,
33673369-- generate a stream using the output, run the second action providing the
@@ -3373,7 +3375,7 @@ bracket :: (MonadAsync m, MonadCatch m)
33733375 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
33743376bracket bef aft bet =
33753377 gbracket bef MC.try aft
3376− (\a (e :: SomeException) -> nilM (aft a >> MC.throwM e)) bet
3378+ (\a (e :: SomeException) _ -> nilM (aft a >> MC.throwM e)) bet
33773379
33783380data BracketState s v = BracketInit | BracketRun s v
33793381
@@ -3412,13 +3414,23 @@ finally_ action xs = bracket_ (return ()) (\_ -> action) (const xs)
34123414finally :: (MonadAsync m, MonadCatch m) => m b -> Stream m a -> Stream m a
34133415finally action xs = bracket (return ()) (\_ -> action) (const xs)
34143416
3417+-- | When evaluating a stream if an exception occurs, stream
3418+-- evaluation aborts and the specified exception handler is run with
3419+-- the exception and the Stream which threw the exception as argument.
3420+--
3421+{-# INLINE_NORMAL ghandle #-}
3422+ghandle :: (MonadCatch m, Exception e)
3423+ => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
3424+ghandle f str =
3425+ gbracket_ (return ()) MC.try return (\_ -> f) (\_ -> str)
3426+
34153427-- | When evaluating a stream if an exception occurs, stream evaluation aborts
34163428-- and the specified exception handler is run with the exception as argument.
34173429{-# INLINE_NORMAL handle #-}
34183430handle :: (MonadCatch m, Exception e)
34193431 => (e -> Stream m a) -> Stream m a -> Stream m a
34203432handle f str =
3421− gbracket_ (return ()) MC.try return (\_ e -> f e) (\_ -> str)
3433+ gbracket_ (return ()) MC.try return (\_ e _ -> f e) (\_ -> str)
34223434
34233435{-# INLINE_NORMAL _handle #-}
34243436_handle :: (MonadCatch m, Exception e)