Skip to content

Instantly share code, notes, and snippets.

@jharlap
Created May 5, 2011 14:24
Show Gist options
  • Save jharlap/957127 to your computer and use it in GitHub Desktop.
Save jharlap/957127 to your computer and use it in GitHub Desktop.
riak-js interacting with file upload, causing strange stall
###
This is chopped down from a multi-kloc app, which explains why Connect is involved
and why I would even care to do a riak.get and ignore its result. The issue here is
that if RUN_TEST is 1, doing the riak.get frequently causes the file upload to stall,
while when RUN_TEST is 0 and riak.get is skipped, the file upload works reliably.
###
connect = require 'connect'
fs = require 'fs'
riak = require('riak-js').getClient()
routes = (app) ->
app.put '/:RUN_TEST/:key', (req, res) ->
handleUpload = () ->
filename = "/tmp/blahblah_#{new Date().getTime()}"
console.log "Writing to #{filename}"
fileStream = fs.createWriteStream filename
fileStream.on 'error', (err) ->
console.log "Error on filestream: #{err}"
res.writeHead 500
res.end()
req.on 'end', () ->
console.log "req ended"
fileStream.end()
res.writeHead 204
res.end()
req.pipe fileStream, end:false
req.resume()
if req.params.RUN_TEST is '1' then riak.get 'bucket', 'key', (err, data) -> handleUpload()
else handleUpload()
server = connect.createServer()
server.use '/', connect.router routes
server.listen 8123, "127.0.0.1"
console.log 'Server running at http://127.0.0.1:8123/'
console.log "Now run: curl -v -X PUT -H 'Content-Type: image/jpeg' -d @/path/to/img.jpg http://localhost:8123/1/mytestimg1"
@jharlap
Copy link
Author

jharlap commented May 5, 2011

Solution: req.pause() before doing riak.get.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment