[Haskell-cafe] Timeouts inside a ConduitParser
Luke Lau
luke_lau at icloud.com
Thu Jun 21 13:07:42 UTC 2018
I have a ConduitParser <https://hackage.haskell.org/package/conduit-parse-0.2.1.0/docs/Data-Conduit-Parser.html#t:ConduitParser> (a Sink with some parsing state) with a version of satisfy that can time out:
satisfy :: MonadIO m => (a -> Bool) -> ConduitParser a m a
satisfy pred = do
tId <- liftIO myThreadId
timeoutThread <- liftIO $ forkIO $ do
threadDelay 1000000
throwTo tId TimeoutException
x <- await
liftIO $ killThread timeoutThread
if pred x
then return x
else empty
However I would rather not deal with the risks involved with handling concurrency myself and use a system library like System.Timeout:
satisfy :: MonadIO m => (a -> Bool) -> ConduitParser a m a
satisfy pred = do
x <- timeout 1000000 await
if pred x
then return x
else empty
This doesn’t work though since I need to be able to both lift and unlift await from IO, and ConduitParser lies on top of ConduitT, which is one of the types of monads that UnliftIO cannot be an instance of <https://github.com/fpco/unliftio#limitations>. Are there any better approaches to this?
<https://github.com/fpco/unliftio#limitations> <https://github.com/fpco/unliftio#limitations> <https://github.com/fpco/unliftio#limitations>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180621/33bdbf20/attachment.html>
More information about the Haskell-Cafe
mailing list