Skip to content

Instantly share code, notes, and snippets.

@paulswail
paulswail / cicd-pipelines.ts
Last active December 12, 2022 13:53
Create CICD pipelines for serverless services using AWS CDK
// /stacks/cicd-pipelines.ts
// CDK app which creates a stack using a set of service definitions
import 'source-map-support/register';
import { App } from '@aws-cdk/cdk';
import { ServiceCicdPipelines } from '../lib/cicd/pipelines';
import { deploymentTargetAccounts } from './config';
import services from './services';
const app = new App({
@danieldraper
danieldraper / application_form.rb
Last active September 10, 2020 06:17
Service object and form object using ActiveModel and dry-rb
require "dry/monads/result"
class ApplicationForm
include Dry::Monads::Result::Mixin
include ActiveModel::Model
def self.attribute(name, options = {})
self.send(:attr_accessor, name)
_attributes << Attribute.new(name, options)
end
@jgaskins
jgaskins / app.rb
Last active August 15, 2020 02:34
API app with Roda
require 'authentication'
class MyApp < Roda
include Authentication
plugin :json
route do |r|
r.on('docs') { r.run Docs }
@jwhiting
jwhiting / warmup.rb
Created May 14, 2018 22:00
Warming up EBS volumes on RDS replicas/snapshots (ruby script)
#!/usr/bin/env ruby
require 'pg'
=begin
when creating a postgres db replica in RDS, or restoring from a snapshot, the
underlying EBS volume of the new instance must be initialized by reading every
block, otherwise the blocks will be lazy-initialized by production queries
which will be extremely latent. (i've seen normally 50ms queries take 30s in
@polaskj
polaskj / deploy.sh
Last active October 27, 2022 10:20 — forked from dfetterman/lambda_function-ECS-cloudwatch-slack.py
Lambda function that takes ECS Cloudwatch events and sends a notification to Slack
#!/bin/bash
# deploy serverless stack
serverless deploy \
--stage "dev" \
--region "us-east-1" \
--service-name "my-service-name" \
--slack-channel "#slack-alerts-example" \
--webhook-url "https://hooks.slack.com/services/12345/12345/abcdefg" \
--ecs-cluster-arn "arn:aws:ecs:us-east-1:123456:cluster/test-cluster"
@SQLadmin
SQLadmin / postgres-user-migration.txt
Last active November 21, 2021 17:23
Migrate Postgresql users to another server and RDS
-- Export the users
pg_dumpall -g > users.sql
-- Importing to another PostgresSQL
awk '/CREATE/' users.sql > migrate.sql
-- Import to RDS; It won't support super user and replication roles. But we can grant minimal superuser with rds_superuser
psql -h localserver -d postgres -t -c"select 'grant rds_superuser to '||rolname ||';' from pg_roles where rolsuper='t';" -P "footer=off" >> migrate.sql
sed -i -e's/NOSUPERUSER//g; s/SUPERUSER//g; s/NOREPLICATION//g; s//REPLICATION/g' migrate.sql
psql -h rds-endpoint -U adminuser -d postgres < migrate.sql
package xxx.security.authc.keymaker
import java.security.KeyFactory
import java.security.interfaces.ECPublicKey
import java.security.spec.X509EncodedKeySpec
import javax.security.auth.x500.X500Principal
import com.google.protobuf.ByteString
import monix.eval.Task
import org.bouncycastle.asn1.x500.X500Name
module PostgresEnum
extend ActiveSupport::Concern
class_methods do
def postgres_enum_for(column, type_name:)
enums = ActiveRecord::Base.connection.exec_query <<-SQL
SELECT pg_enum.enumlabel AS enumlabel
FROM pg_type
JOIN pg_enum
ON pg_enum.enumtypid = pg_type.oid
image: registry.gitlab.com/gitlab-examples/kubernetes-deploy
stages:
- deploy
- rollback
kubernetes deploy:
stage: deploy
environment:
name: production
script:
- sed -i "s/__CI_ENVIRONMENT_SLUG__/$CI_ENVIRONMENT_SLUG/" nginx-deployment.yaml

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.