Skip to content

Instantly share code, notes, and snippets.

@Imater
Created September 2, 2014 03:55
Show Gist options
  • Save Imater/3fec74f4e4bd3b8706ac to your computer and use it in GitHub Desktop.
Save Imater/3fec74f4e4bd3b8706ac to your computer and use it in GitHub Desktop.
Receive from RabbitMQ queue
fib = (n) ->
# Do it the ridiculous, but not most ridiculous, way. For better,
# see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
a = 0
b = 1
i = 0
while i < n
c = a + b
a = b
b = c
i++
a
amqp = require("amqplib")
amqp.connect("amqp://localhost").then((conn) ->
process.once "SIGINT", ->
conn.close()
return
conn.createChannel().then (ch) ->
reply = (msg) ->
n = parseInt(msg.content.toString())
console.log " [.] fib(%d)", n
response = fib(n)
ch.sendToQueue msg.properties.replyTo, new Buffer(response.toString()),
correlationId: msg.properties.correlationId
ch.ack msg
return
q = "rpc_queue"
ok = ch.assertQueue(q,
durable: false
)
ok = ok.then(->
ch.prefetch 1
ch.consume q, reply
)
return ok.then(->
console.log " [x] Awaiting RPC requests"
return
)
return
).then null, console.warn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment