<div dir="ltr"><div>Due to the coroutine-based nature of conduit, this kind of approach isn't possible. In short, when you're inside the `ConduitT` transformer, you cannot use `timeout` on a `ConduitT` action because you need to yield control of execution to a different coroutine. Instead, you should move the `timeout` call to _outside_ the `ConduitT` parts, e.g.:</div><div><br></div><div>    timeout foo $ runConduit $ src .| satisfy .| sink</div><div><br></div><div>It seems like in this case, that kind of timeout usage is going to be too coarse-grained. I haven't used it personally, but the `stm-conduit` package probably has something in the direction you're looking for. Alternatively, I put together an example of how this might be done using some standard Haskell libraries like stm and async here:<br></div><div><br></div><div><a href="https://gist.github.com/snoyberg/7e5dd52109b03c8bf1aa8fe1a7e522b9">https://gist.github.com/snoyberg/7e5dd52109b03c8bf1aa8fe1a7e522b9</a></div><div><br></div><div>The basic idea is to have two sibling threads: one running the original source and writing its values to a queue, and another running the full conduit pipeline with a modified source that will time out on reads from that queue.<br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 21, 2018 at 4:08 PM Luke Lau <<a href="mailto:luke_lau@icloud.com">luke_lau@icloud.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">I have a <a href="https://hackage.haskell.org/package/conduit-parse-0.2.1.0/docs/Data-Conduit-Parser.html#t:ConduitParser" target="_blank">ConduitParser</a> (a Sink with some parsing state) with a version of satisfy that can time out:<div><br></div><div><div style="line-height:15px"><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)"><span style="color:#50fa7b">satisfy</span> <span style="color:#ff79c6">::</span> <span style="color:#8be9fd;font-style:italic">MonadIO</span> m <span style="color:#ff79c6">=></span> (a <span style="color:#ff79c6">-></span> <span style="color:#8be9fd;font-style:italic">Bool</span>) <span style="color:#ff79c6">-></span> <span style="color:#8be9fd;font-style:italic">ConduitParser</span> a m a</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">satisfy pred <span style="color:#ff79c6">=</span> <span style="color:#ff79c6">do</span></div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  tId <span style="color:#ff79c6"><-</span> liftIO myThreadId</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  timeoutThread <span style="color:#ff79c6"><-</span> liftIO <span style="color:#ff79c6">$</span> forkIO <span style="color:#ff79c6">$</span> <span style="color:#ff79c6">do</span></div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">    threadDelay <span style="color:#bd93f9">1000000</span></div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">    throwTo tId <span style="color:#bd93f9">TimeoutException</span></div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  x <span style="color:#ff79c6"><-</span> await</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  liftIO <span style="color:#ff79c6">$</span> killThread timeoutThread</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  <span style="color:#ff79c6">if</span> pred x</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">    <span style="color:#ff79c6">then</span> return x</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">    <span style="color:#ff79c6">else</span> empty</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap"><br></div><div>However I would rather not deal with the risks involved with handling concurrency myself and use a system library like System.Timeout:</div><div><br></div><div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)"><span style="color:rgb(80,250,123)">satisfy</span> <span style="color:rgb(255,121,198)">::</span> <span style="color:rgb(139,233,253);font-style:italic">MonadIO</span> m <span style="color:rgb(255,121,198)">=></span> (a <span style="color:rgb(255,121,198)">-></span> <span style="color:rgb(139,233,253);font-style:italic">Bool</span>) <span style="color:rgb(255,121,198)">-></span> <span style="color:rgb(139,233,253);font-style:italic">ConduitParser</span> a m a</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">satisfy pred <span style="color:rgb(255,121,198)">=</span> <span style="color:rgb(255,121,198)">do</span></div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  x <span style="color:rgb(255,121,198)"><-</span> timeout <span style="color:rgb(189,147,249)">1000000</span> await</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">  <span style="color:rgb(255,121,198)">if</span> pred x</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">    <span style="color:rgb(255,121,198)">then</span> return x</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">    <span style="color:rgb(255,121,198)">else</span> empty</div><div style="color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap"><br></div><div><div><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px;white-space:normal">This doesn’t work though since I need to be able to both lift and unlift <span style="background-color:rgb(40,42,54);color:rgb(248,248,242);font-family:"SF Mono",Menlo,Monaco,"Courier New",monospace;font-size:11px;white-space:pre-wrap">await</span> from </span><font color="#f8f8f2" face="SF Mono, Menlo, Monaco, Courier New, monospace"><span style="font-size:11px;white-space:pre-wrap;background-color:rgb(40,42,54)">IO</span></font>, and ConduitParser lies on top of ConduitT, which is <a href="https://github.com/fpco/unliftio#limitations" target="_blank">one of the types of monads that UnliftIO cannot be an instance of</a>. Are there any better approaches to this?</div><a href="https://github.com/fpco/unliftio#limitations" target="_blank"></a><a href="https://github.com/fpco/unliftio#limitations" target="_blank"></a><a href="https://github.com/fpco/unliftio#limitations" target="_blank"></a></div></div></div></div></div>_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>