Skip to content

Instantly share code, notes, and snippets.

View mkdynamic's full-sized avatar

Mark Dodwell mkdynamic

View GitHub Profile
# Okasaki style Functional Red Black Tree
# https://www.cs.tufts.edu/comp/150FP/archive/chris-okasaki/redblack99.pdf
#
# leaves and root are black
# BST
# No red node has a red child
# Every path from the root to a leaf has the same number of black nodes
module RBTree
class Leaf
@zealot128
zealot128 / rails-models-to-typescript-schema.rb
Last active July 1, 2024 16:05
Simple ruby script to generate Active Record Typescript information with enums + associations
# USAGE:
# rails runner rails-models-to-typescript-schema.rb > app/javascript/types/schema.d.ts
Rails.application.eager_load!
models = ActiveRecord::Base.descendants.reject { |i| i.abstract_class? }
belongs_to = true
has_many = true
@abkunal
abkunal / vpc_stack.py
Created January 2, 2021 15:05
CDK Stack file to create a custom VPC
from aws_cdk import core
from aws_cdk.aws_ec2 import Vpc, CfnRouteTable, RouterType, CfnRoute, CfnInternetGateway, CfnVPCGatewayAttachment, \
CfnSubnet, CfnSubnetRouteTableAssociation, CfnSecurityGroup, CfnInstance
from . import config
class VpcStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
@miguelmota
miguelmota / buildspec.yml
Last active January 20, 2023 06:05
AWS CodeBuild build spec for Elastic Container Service deployment from CodeBuild and CodePipeline
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- REPOSITORY_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME"

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.

def pmap(enum)
return to_enum(:pmap, enum) unless block_given?
enum.map { |e| Thread.new { yield e } }.map(&:value)
end
# Returns elements in order, as expected.
pmap(1..10) { |e| e } #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Returns elements in nondeterministic order on MRI >= 1.9.3.
# Works as expected on JRuby, Rubinius, and earlier versions of MRI.
@jonneale
jonneale / dynamo-backups.rb
Last active December 17, 2015 20:39
Dynamo DB backups using the Ruby AWS SDK
require 'aws-sdk'
AWS.config({:access_key_id => "your-access-key-id",
:secret_access_key => "your-secrect-access-key",
:region => "eu-west-1"}) #region is optional
log_uri = "s3n://dynamo-backups/dynamo-backup-resources/logs" #Can be anywhere you like on S3
emr = AWS::EMR.new
job = emr.job_flows.create('dynamo-db-backup', {log_uri: log_uri,
@mattwynne
mattwynne / integrated_docs.rb
Created November 21, 2012 00:27
Integrated documentation for Ruby
# For context, this was inspired by the RubyRogues podcast #79 where they talked about
# documentation in Ruby, and specifically grumbled quite a bit about the failings of RDoc.
#
# http://rubyrogues.com/079-rr-documenting-code/
#
# As someone who's spent a lot of time using an IDE for programming C# and Java, I think
# Ruby could do a lot better at putting documentation at our fingertips as we program.
#
# Maybe making the documentation part of the structure of the code would facilitate this?
#
@mkdynamic
mkdynamic / ios-media-queries.css.scss
Created October 17, 2012 07:30
SASS media query helper for iOS devices
@mixin respond-to($media) {
@if $media == iphone {
@media only screen and (device-width: 320px) and (device-height: 480px) { @content; }
}
@else if $media == iphone-portrait {
@media only screen and (device-width: 320px) and (device-height: 480px) and (orientation: portrait) { @content; }
}
@else if $media == iphone-landscape {
@media only screen and (device-width: 320px) and (device-height: 480px) and (orientation: landscape) { @content; }
}
@dkasper
dkasper / CoreTextLabel.h
Created October 2, 2012 03:26
A multiline label drawn with core text. Needed it for line spacing, can easily be modified to use any core text properties though.
//
// CoreTextLabel.h
// Frost
//
// Created by David Kasper on 10/1/12.
//
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>