Created
February 18, 2015 12:55
-
-
Save dudleycarr/8f9c04e63f599b222b41 to your computer and use it in GitHub Desktop.
Timing nsqjs messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nsq = require 'nsqjs' | |
TOPIC = 'test_topic' | |
# Publish 10 messages | |
writer = new nsq.Writer '127.0.0.1', 4150 | |
writer.connect() | |
writer.on 'ready', -> | |
writer.publish TOPIC, ("#{i}" for i in [0...10]) | |
# Just for the purposes of making the output more legible. | |
shortId = (id) -> | |
id[-5..] | |
reader = new nsq.Reader TOPIC, 'default_channel', | |
maxInFlight: 5 | |
nsqdTCPAddresses: ['127.0.0.1:4150'] | |
# Message timing | |
reader.on 'message', (msg) -> | |
console.time shortId msg.id | |
msg.on 'respond', (responseCode) -> | |
if responseCode in [0,1] | |
console.timeEnd shortId msg.id | |
# Normal message handler | |
reader.on 'message' , (msg) -> | |
delay = Math.floor(Math.random() * 10) + 1 | |
console.log "#{shortId msg.id} processing for #{delay}s" | |
done = -> | |
msg.finish() | |
setTimeout done, delay * 1000 | |
reader.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment