title: Chunk data with Conduit
body: <p>Here is an example of a <code>conduit</code> combinator that supposed to <code>yield</code> downstream when a complete message is received from upstream:</p>
  
  <pre><code>import qualified Data.ByteString as BS
  import Data.Conduit
  import Data.Conduit.Combinators
  import Data.Conduit.Network
  
  message :: Monad m =&gt; ConduitT BS.ByteString BS.ByteString m ()
  message = loop
    where
      loop = await &gt;&gt;= maybe (return ()) go
      go x = if (BS.isSuffixOf "|" x)
          then yield (BS.init x) &gt;&gt; loop
          else leftover x
  </code></pre>
  
  <p>Server code itself looks like following:</p>
  
  <pre><code>main :: IO ()
  main = do
    runTCPServer (serverSettings 5000 "!4") $ \ appData -&gt; runConduit $
      (appSource appData)
      .| message
      .| (appSink appData)
  </code></pre>
  
  <p>For some reason <code>telnet 127.0.0.1 5000</code> disconnects after sending any message:</p>
  
  <pre><code>telnet 127.0.0.1 5000
  Trying 127.0.0.1...
  Connected to 127.0.0.1.
  Escape character is '^]'.
  123|
  Connection closed by foreign host.
  </code></pre>
  
  <p>Please advice, what am I doing wrong here?</p>
  
  <p><strong>Update</strong></p>
  
  <p>More importantly what I try doing here is wait for completion signal <code>|</code> and then <code>yield</code> the complete message downstream. Here is the evolution of <code>message</code> combinator:</p>
  
  <pre><code>message :: Monad m =&gt; ConduitT BS.ByteString BS.ByteString m ()
  message = do
    minput &lt;- await
    case minput of
      Nothing    -&gt; return ()
      Just input -&gt; do
        case BS.breakSubstring "|" input of
          ("", "")  -&gt; return ()
          ("", "|") -&gt; return ()
          ("", xs)  -&gt; leftover $ BS.tail xs
          (x, "")   -&gt; leftover x -- problem is in this leftover
          (x, xs)   -&gt; do
            yield x
            leftover $ BS.tail xs
        message
  </code></pre>
  
  <p>The idea I had is that if there is nothing coming from the upstream combinator will have to wait until there will be something, such that it can send a complete message downstream. But it seams that <code>conduit</code> starts spinning on CPU a lot on that <code>leftover</code> call in the above <code>message</code> combinator.</p>