Skip to content

Instantly share code, notes, and snippets.

View senderista's full-sized avatar

Tobin Baker senderista

View GitHub Profile
@senderista
senderista / http_stream.py
Created June 29, 2011 22:12
http chunked encoding iterator
class HTTPStream(object):
"""Given a URL which returns chunk-encoded results,
constructs an iterator over those results (assuming
individual records are newline-separated)."""
def __init__(self, url, username="", password=""):
self.prev_linepart = ""
self.http_client = HTTPClient()
self.http_client.fetch(
url=url,
auth_username=username,
@senderista
senderista / mock_stream.py
Created June 29, 2011 22:18
Mock class for HTTPStream testing
class MockStream(object):
"""Given a URL which returns chunk-encoded results,
constructs an iterator over those results (assuming
individual records are newline-separated)."""
def __init__(self, url, username="", password=""):
self.prev_linepart = ""
_handle_data("abc\ndef\nghi")
_handle_data("jkl\nmno\npqr")
_handle_data("stu\nvwx\nyz")
@senderista
senderista / spotlight.sh
Last active November 17, 2017 02:38
Some ack-style Spotlight search functions
BOLD=`echo -e '\033[1m'`
BLACK=`echo -e '\033[30m'`
RED=`echo -e '\033[31m'`
GREEN=`echo -e '\033[32m'`
YELLOW=`echo -e '\033[33m'`
BLUE=`echo -e '\033[34m'`
MAGENTA=`echo -e '\033[35m'`
CYAN=`echo -e '\033[36m'`
WHITE=`echo -e '\033[37m'`
BG_BLACK=`echo -e '\033[40m'`
#!/usr/bin/env python
import sys
from time import sleep
from twitter import *
t = Twitter(
auth=OAuth(
token='XXXXXX',
token_secret='XXXXXX',
@senderista
senderista / kinesis_tail.py
Last active September 17, 2015 16:45
View incoming records in a Kinesis stream in real time
#!/usr/bin/env python
import sys
import boto.kinesis
region, stream = sys.argv[1:3]
kinesis = boto.kinesis.connect_to_region(region)
response = kinesis.describe_stream(stream)
if response['StreamDescription']['StreamStatus'] == 'ACTIVE':
shard_id = response['StreamDescription']['Shards'][0]['ShardId']
@senderista
senderista / nginx.conf
Last active August 26, 2015 23:13
OpenResty rate limiter example
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
lua_shared_dict token_bucket 10m;
init_by_lua '
ngx.shared.token_bucket:set("tokens_avail", 0)
@senderista
senderista / ps_queue_sim.js
Created August 27, 2015 05:20
A simple processor-sharing queue simulator in node.js
var PoissonProcess = require('poisson-process');
var sleep = require('sleep');
var http = require('http');
var port = process.env.PORT || 1337;
var TIMESLICE_MILLIS = 1;
var MEAN_SLEEP_MILLIS = 500;
function sleep_for_timeslice(total_ms, ms_left, req_timestamp, res) {
if (ms_left > 0) {
sleep.usleep(TIMESLICE_MILLIS*1000);
@senderista
senderista / nginx.conf
Created August 27, 2015 05:24
Token bucket with upstream stats
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
lua_shared_dict token_bucket 10M;
lua_shared_dict stats 5M;
init_by_lua '
@senderista
senderista / delete_delete_markers.py
Created September 3, 2015 21:00
Delete all delete markers in a versioned S3 bucket, to "undelete" all deleted objects that still have a version before the delete marker
#!/usr/bin/env python
import sys
import boto
bucket_name = sys.argv[1]
s3 = boto.connect_s3()
bucket = s3.get_bucket(bucket_name)
for v in bucket.list_versions():
@senderista
senderista / disable_cron.sh
Created September 15, 2015 22:22
Comment out the first crontab entry for a user on a list of servers
for s in `cat servers.txt`; do
echo $s
ssh username@$s <<-EOF
sudo -u nobody bash -c "crontab -l | { echo -n '#'; cat; } | crontab -"
EOF
done