Skip to content

Instantly share code, notes, and snippets.

View mikz's full-sized avatar

Michal Cichra mikz

  • Prague, Czech Republic
View GitHub Profile

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.

@nonsintetic
nonsintetic / ArduinoZeroTimer.ino
Last active January 11, 2025 06:52
SAMD21 Arduino Timer Example
/*
* This sketch illustrates how to set a timer on an SAMD21 based board in Arduino (Feather M0, Arduino Zero should work)
* It should generate a 1Hz square wave as it is (thanks richdrich for the suggestion)
* Some more info about Timer Counter works can be found in this article:
* http://www.lucadavidian.com/2017/08/08/arduino-m0-pro-il-sistema-di-clock/
* and in the datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/SAM_D21_DA1_Family_DataSheet_DS40001882F.pdf
*/
uint32_t sampleRate = 1000; //sample rate in milliseconds, determines how often TC5_Handler is called
# Cleans up branches like:
# if Shopify.rails_next?
# # Rails 5 login
# else
# # Rails 4 login
# end
module RuboCop
module Cop
module ShopifyRails
class RailsNextUnless < Cop
@jdneo
jdneo / timerInterrupt.ino
Created December 8, 2016 02:21
Timer Interrupt example for Adafruit Feather M0. Callback function can be written in TC3_Handler().
#define LED_PIN 13
#define CPU_HZ 48000000
#define TIMER_PRESCALER_DIV 1024
void startTimer(int frequencyHz);
void setTimerFrequency(int frequencyHz);
void TC3_Handler();
bool isLEDOn = false;
@mikz
mikz / luarocks.rb
Created August 21, 2016 12:26
LuaRocks Homebrew Formula
require 'formula'
class Luarocks < Formula
homepage 'https://rocks.moonscript.org/'
stable do
url 'https://github.com/keplerproject/luarocks/archive/v2.3.0.tar.gz'
sha256 '92c014889ec6a09c4bb492df6b7f7be784110d6abe031e16418342781ca5c5ce'
end
@mikz
mikz / jspm.rb
Created June 17, 2016 09:34
detect jspm packages licenses
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
require 'shellwords'
ENV['NODE_PATH'] = '/usr/local/lib/node_modules'
# npm install -g licenses

Minimum Viable Async with Node 6

With the release of Node 6.0.0, the surface of code that needs transpilation to use ES6 features has been reduced very dramatically.

This is what my current workflow looks like to set up a minimalistic and fast microservice using micro and async + await.

The promise

@anynoomcz
anynoomcz / Stručná historie přípravy systému EET.md
Last active April 8, 2016 23:33
Jak v roce 2015 probíhala příprava implementace systému EET

Stručná historie dodávky systému EET

Plány Ministerstva financí

Během roku 2015 probíhaly nezanedbatelné aktivity v oblasti EET tak, jak je MF na počátku roku předestřelo v tiskové zprávě.

Průzkum trhu

Dle uvedeného plánu SPCSS (Státní pokladna Centrum sdílených služeb) uspořádalo průzkum trhu, který byl podkladem pro zúžení skupiny oslovených potenciálních dodavatelů, kde základním kvalifikačním předpokladem byla schopnost zpracovávat nabídku a následně dodávat systém v režimu utajených informací.

@rezan
rezan / s3.vcl
Last active July 6, 2023 13:02
Varnish AWS S3 Gateway VCL
#
# Varnish AWS S3 Gateway VCL
#
# Allows global read (GET, HEAD) and ACL protected writes (POST, PUT, DELETE).
# When writing, pass in Content-Type and Content-MD5, both are optional.
#
# Params:
#
# %BUCKET% - S3 bucket name, S3 host may be regional
# %ACCESS_ID% - IAM access ID for bucket
@paoloantinori
paoloantinori / keycloak.sh
Created January 26, 2016 15:59
Keycloak Admin API Rest Example
#!/bin/bash
export TKN=$(curl -X POST 'http://localhost:8080/auth/realms/master/protocol/openid-connect/token' \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d 'password=admin' \
-d 'grant_type=password' \
-d 'client_id=admin-cli' | jq -r '.access_token')
curl -X GET 'http://localhost:8080/auth/admin/realms' \