Skip to content

Instantly share code, notes, and snippets.

@paxan
paxan / RedirectSpy.java
Last active December 17, 2015 09:38
A demo of shortened URL expansion using Apache HttpComponents (http://hc.apache.org/)
import com.google.common.base.Joiner;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolException;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.protocol.HttpContext;
@paxan
paxan / Node Under Pressure.md
Last active December 21, 2015 12:09
Putting a simple one-dyno Node.js app under pressure on Heroku

What the app does

It receives certain JSON events as HTTP POSTs, parses them, and writes received JSON with along with some extra metadata to stdout using console.log(). POST-ed data does not usually exceed ~800 bytes per request.

It runs with node 0.10.15 engine on Heroku with just one web dyno. Also it uses Express 3.3.4 web service library.

Under pressure

I've subjected the app to some traffic by means of blitz.io:

@paxan
paxan / fibosayer.py
Last active December 26, 2015 17:49
Says numbers of Fibonacci sequence starting from the 50th one and goes on forever.
#!/usr/bin/env python
from __future__ import print_function
import random
import time
from subprocess import check_call
from shlex import split
voices = tuple('Agnes Kathy Princess Vicki Victoria Bruce Fred Junior Ralph'.split()) + \
@paxan
paxan / with-open-seq.clj
Last active August 29, 2015 13:56
with-open-seq
(require '[clojure.core.async :refer [chan close! go >! <!!]])
(defmacro with-open-seq
"Like with-open, but only closes resources when
the sequence generated by the body is exhausted."
[bindings & body]
`(let [c# (chan)]
(go
(try
(with-open ~bindings
@paxan
paxan / librato.go
Created December 5, 2014 23:55
HACK: Librato measurement sender
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"regexp"
@paxan
paxan / demo
Last active August 29, 2015 14:19
try-let
```
> (try-let [x (/ 5 1) y (/ 5 2)] [x y])
[5 5/2]
> (try-let [x (/ 5 1) y (/ 5 0)] [x y])
#<ArithmeticException java.lang.ArithmeticException: Divide by zero>
```
@paxan
paxan / runtime-metrics.clj
Created May 23, 2015 17:40
boot.clj Heroku runtime metrics
{:time #inst "2015-05-23T16:45:35.294Z", :from "run.6221", :text "source=run.6221 sample#memory_total=7.62MB sample#memory_rss=0.66MB sample#memory_cache=6.96MB sample#memory_swap=0.00MB sample#memory_pgpgin=2512pages sample#memory_pgpgout=561pages"}
{:time #inst "2015-05-23T16:45:56.995Z", :from "run.6221", :text "source=run.6221 sample#memory_total=259.27MB sample#memory_rss=220.50MB sample#memory_cache=38.77MB sample#memory_swap=0.00MB sample#memory_pgpgin=91599pages sample#memory_pgpgout=25225pages"}
{:time #inst "2015-05-23T16:46:19.082Z", :from "run.6221", :text "source=run.6221 sample#load_avg_1m=1.36"}
{:time #inst "2015-05-23T16:46:19.082Z", :from "run.6221", :text "source=run.6221 sample#memory_total=627.97MB sample#memory_rss=481.32MB sample#memory_cache=7.44MB sample#memory_swap=139.21MB sample#memory_pgpgin=208632pages sample#memory_pgpgout=83508pages"}
{:time #inst "2015-05-23T16:46:19.083Z", :from "run.6221", :text "Process running mem=627M(122.7%)"}
{:time #inst "2015-05-23T16:46:19.083Z", :fr
@paxan
paxan / dynamic-jar-loading
Created June 12, 2015 23:38
Example of loading a local jar file dynamically using boot-clj
#!/usr/bin/env boot
;; -*- mode: clojure -*-
(set-env! :dependencies '[[org.apache.hadoop/hadoop-common "2.4.0"]
[org.apache.pig/pig "0.12.0"
:exclusions [commons-logging
commons-net
net.java.dev.jets3t/jets3t
org.slf4j/slf4j-api
tomcat/jasper-compiler
@paxan
paxan / tornado_force_https.py
Last active August 15, 2023 01:24
A hack to make Tornado web server redirect http requests to https for Heroku or similar reverse-proxied deployments
from __future__ import absolute_import, print_function
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
def create_server(*args, **kwargs):
'''
@paxan
paxan / kmstool.py
Last active January 26, 2016 00:48
A tool for storing secrets on disk securely using AWS KMS
from __future__ import absolute_import, print_function
import boto3
import errno
import json
import os
import re
import shlex
import sys
import tempfile