Skip to content

Instantly share code, notes, and snippets.

View mozillazg's full-sized avatar
💭
I may be slow to respond.

Huang Huang mozillazg

💭
I may be slow to respond.
View GitHub Profile
@csrutil
csrutil / ikev2
Last active November 4, 2017 00:26
https://github.com/gaomd/docker-ikev2-vpn-server
docker run --privileged -d --name ikev2-vpn-server --restart=always -p 500:500/udp -p 4500:4500/udp gaomd/ikev2-vpn-server:0.3.0
docker run --privileged -i -t --rm --volumes-from ikev2-vpn-server -e "HOST=domain_name_or_ip" gaomd/ikev2-vpn-server:0.3.0 generate-mobileconfig > ikev2-vpn.mobileconfig
@edofic
edofic / indirect.py
Created May 15, 2017 06:42
Indirect parametrization in pytest
import pytest
@pytest.fixture()
def user(request):
user = 'mock user'
if hasattr(request, 'param'):
user += ': {}'.format(request.param)
return user

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.

@guyskk
guyskk / classtree.py
Last active March 22, 2017 12:58
Print class tree, include all sub classes alive in Python VM
# coding:utf-8
from inspect import getclasstree
def classtree(cls, indent=0, fillchar='-'):
"""
Print class tree
Args:
cls: base class
#!/bin/python
# This simple Python script runs simulations for different retry algorithms
# The results are printed as CSV data to the console
# See this article on my blog for details:
# https://blog.miguelgrinberg.com/post/how-to-retry-with-class
from random import random, choice
@efrecon
efrecon / run.tpl
Last active July 16, 2024 10:49
`docker inspect` template to regenerate the `docker run` command that created a container
docker run \
--name {{printf "%q" .Name}} \
{{- with .HostConfig}}
{{- if .Privileged}}
--privileged \
{{- end}}
{{- if .AutoRemove}}
--rm \
{{- end}}
{{- if .Runtime}}
@tianchaijz
tianchaijz / cjk_auto_formating.py
Created March 25, 2016 01:27
CJK Auto Formating
#!/usr/bin/env python
# encoding: utf-8
from __future__ import (unicode_literals, print_function)
import os
import re
import sys
import codecs
@thisismitch
thisismitch / le-renew-webroot
Last active April 26, 2024 04:13
Let's Encrypt Auto-Renewal using the Webroot Plugin (Nginx)
#!/bin/bash
web_service='nginx'
config_file="/usr/local/etc/le-renew-webroot.ini"
le_path='/opt/letsencrypt'
exp_limit=30;
if [ ! -f $config_file ]; then
echo "[ERROR] config file does not exist: $config_file"
#include <unistd.h>
#include <stdio.h>
#define LOAD 16384
int main() {
int i;
char s[256];
for (i=0; i<LOAD; i++) {
@ipmb
ipmb / Dockerfile
Created May 24, 2015 21:23
Example wheel upload to simple PyPI on S3
FROM ubuntu:14.04
ENV BUCKET your-s3-bucket
RUN apt-get update && apt-get install -y wget \
build-essential python-dev \
libssl-dev libffi-dev \
libpcre3-dev \
libmemcached-dev \
libpq-dev \