Skip to content

Instantly share code, notes, and snippets.

@steve-chavez
steve-chavez / parent_join_test_query_new.sql
Last active September 19, 2017 21:21
Tests for PostgREST new proposed query for parent embeds
-- Prior 9.6 \setrandom :rid 1 999900
\set rid random(1, 999900)
BEGIN ISOLATION LEVEL READ COMMITTED READ ONLY;
WITH pg_source AS
(SELECT "parent_query_test"."items"."id",
"parent_query_test"."items"."name",
row_to_json("parent_items_parent_items".*) AS "parent_items"
FROM "parent_query_test"."items"
@steve-chavez
steve-chavez / http_streaming.md
Created July 9, 2017 18:13 — forked from CMCDragonkai/http_streaming.md
HTTP Streaming (or Chunked vs Store & Forward)

HTTP Streaming (or Chunked vs Store & Forward)

The standard way of understanding the HTTP protocol is via the request reply pattern. Each HTTP transaction consists of a finitely bounded HTTP request and a finitely bounded HTTP response.

However it's also possible for both parts of an HTTP 1.1 transaction to stream their possibly infinitely bounded data. The advantages is that the sender can send data that is beyond the sender's memory limit, and the receiver can act on

@steve-chavez
steve-chavez / ca.md
Created June 26, 2017 22:59 — forked from soarez/ca.md
How to setup your own CA with OpenSSL

How to setup your own CA with OpenSSL

For educational reasons I've decided to create my own CA. Here is what I learned.

First things first

Lets get some context first.

@steve-chavez
steve-chavez / self-sign-with-ca.sh
Last active September 6, 2025 01:12 — forked from kyledrake/gist:d7457a46a03d7408da31
Creating a self-signed SSL certificate, and then verifying it on another Linux machine
# Procedure is for Archlinux.
# Using these guides:
# http://datacenteroverlords.com/2012/03/01/creating-your-own-ssl-certificate-authority/
# https://turboflash.wordpress.com/2009/06/23/curl-adding-installing-trusting-new-self-signed-certificate/
# https://jamielinux.com/articles/2013/08/act-as-your-own-certificate-authority/
# Generate the root (GIVE IT A PASSWORD IF YOU'RE NOT AUTOMATING SIGNING!):
openssl genrsa -aes256 -out ca.key 2048
openssl req -new -x509 -days 7300 -key ca.key -sha256 -extensions v3_ca -out ca.crt

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@steve-chavez
steve-chavez / latency.txt
Created March 28, 2017 02:17 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@steve-chavez
steve-chavez / pgtap_example.sql
Last active March 14, 2017 04:58 — forked from Su-Shee/pgtap example
basic pgtap testing
begin;
create extension pgtap;
create table users(
username text primary key,
email text not null,
firstname text,
lastname text
);
@steve-chavez
steve-chavez / ratelimit.nginxconf
Created January 12, 2017 20:01 — forked from ipmb/ratelimit.nginxconf
Nginx reverse proxy with rate limiting
upstream myapp {
server 127.0.0.1:8081;
}
limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
server {
listen 443 ssl spdy;
server_name _;
CREATE TEXT SEARCH CONFIGURATION fr ( COPY = french );
ALTER TEXT SEARCH CONFIGURATION fr ALTER MAPPING
FOR hword, hword_part, word WITH unaccent, french_stem;
CREATE TEXT SEARCH CONFIGURATION en ( COPY = english );
ALTER TEXT SEARCH CONFIGURATION en ALTER MAPPING
FOR hword, hword_part, word WITH unaccent, english_stem;
CREATE TEXT SEARCH CONFIGURATION de ( COPY = german );
ALTER TEXT SEARCH CONFIGURATION de ALTER MAPPING
#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------
# - Where to Log -
log_destination = 'csvlog' # Valid values are combinations of
# stderr, csvlog, syslog, and eventlog,
# depending on platform. csvlog
# requires logging_collector to be on.