[Haskell-cafe] Data.ByteString.Lazy.hPut and timeouts?

Jeremy Shaw jeremy at n-heptane.com
Wed Jun 2 22:55:01 EDT 2010


I am using Data.ByteString.Lazy.hPut to write data to a network  
socket. Is there any portable way to have hPut timeout if no data has  
been sent after some time period (say 120s?).

I know that some OSes provide a per socket timeout that I can set in  
my application code (instead of a system-wide timeout that can only be  
set by root). But that does not seem like a very portable solution.

My only other thought so far is to modify hPut to take a timeout. The  
standard definition looks like this:
hPut :: Handle -> ByteString -> IO ()
hPut h cs = foldrChunks (\c rest -> S.hPut h c >> rest) (return ()) cs
Seems like it should be easy to modify it so that it forks another  
thread to act as a watch dog. After every S.hPut it can tickle the  
timer. Since chunks are 16k, I think this should be sufficiently ok.  
If it is taking longer than 120s to send 16k, then we might want to  
consider the connection dead, even if it is making painfully slow  
progress.. Once all the chunks have been sent, we can kill the watch  
dog thread..
But, I am wondering if I am missing a better solution ? (Or an  
existing implementation of this solution?)
Thank you for your insights!
- jeremy
p.s. At this point, using lazy ByteStrings is a requirement. Though I  
believe I would still have a very similar question if I was using  
iteratees anyway.

More information about the Haskell-Cafe mailing list