Skip to content

Instantly share code, notes, and snippets.

@mreiferson
mreiferson / gist:3925081
Last active October 11, 2015 21:57
Example Synchronous Python Reader
import nsq
def task1(message):
print message
return True
def task2(message):
print message
return True
@mreiferson
mreiferson / gist:3925092
Last active October 11, 2015 21:57
Example Async Python Client
import nsq
buf = []
def process_message(message):
global buf
message.enable_async()
buf.append(message)
if len(buf) >= 3:
for msg in buf:
@mreiferson
mreiferson / gist:4039222
Created November 8, 2012 14:48
Example NSQ reader in Go - simplified nsq_to_http
const (
ModeAll = iota
ModeRoundRobin
)
type Publisher interface {
Publish(string, []byte) error
}
type PublishHandler struct {
@mreiferson
mreiferson / gist:5293124
Created April 2, 2013 15:31
sigh GOPATH
function j {
local project=$1
cd `find ~/dev/src -type d -mindepth 3 -maxdepth 3 -name $project`
}
@mreiferson
mreiferson / gist:5548351
Created May 9, 2013 15:54
example response from nsqlookupd
{
"status_code": 200,
"status_txt": "OK",
"data": {
"channels": ["archive", "science", "metrics"],
"producers": [
{
"broadcast_address": "clicksapi01.routable.domain.net",
"hostname": "clicksapi01.domain.net",
"tcp_port": 4150,
@mreiferson
mreiferson / gist:5548362
Created May 9, 2013 15:56
example IDENTIFY command JSON payload
{
"short_id": "app01",
"long_id": "app01.bitly.net",
"heartbeat_interval": 30000,
"feature_negotiation": true
}
@mreiferson
mreiferson / gist:5548366
Created May 9, 2013 15:57
example IDENTIFY response JSON payload
{
"max_rdy_count": 2500,
"version": "0.2.20-alpha"
}
def send_ready(reader, conn, count):
if (reader.total_ready_count + count) > reader.max_in_flight:
return
conn.send_ready(count)
conn.rdy_count = count
reader.total_ready_count += count
@mreiferson
mreiferson / is_starved.py
Last active December 17, 2015 04:09
is_starved
def is_starved(conns):
for c in conns:
# the constant 0.85 is designed to *anticipate* starvation rather than wait for it
if c.in_flight > 0 and c.in_flight >= (c.last_ready * 0.85):
return True
return False
import functools
import sys
import logging
def exit_on_exception(method):
@functools.wraps(method)
def wrapped(*args, **kwargs):
try:
return method(*args, **kwargs)