Skip to content

Instantly share code, notes, and snippets.

View sirupsen's full-sized avatar
🐡

Simon Eskildsen sirupsen

🐡
View GitHub Profile
@sirupsen
sirupsen / gist:96326faaf94fc3051d0b
Created January 13, 2015 16:49
Example for blog post on resiliency
def test_section_a_resilient_to_data_store_b_being_down
Toxiproxy[:data_store_b].down do
get '/section_a'
assert_response :success
end
end
@sirupsen
sirupsen / gist:3b3762c74355f42ac7bc
Created January 13, 2015 16:49
Example for blog post
def load_customer
if customer_id = session[:customer_id]
@customer = Customer.find_by_id(customer_id)
end
end
@sirupsen
sirupsen / gist:44f6c7293f25cf69382d
Created January 13, 2015 16:49
Example for blog post
def test_storefront_resilient_to_sessions_down
Toxiproxy[:sessions_data_store].down do
get '/'
assert_equal 'Customer sign in is currently unavailable', flash[:notice]
assert_response :success
end
end
def load_customer
if customer_id = session[:customer_id]
@customer = Customer.find_by_id(session[:customer_id])
end
# in reality e.g. a redis exception thrown from the driver
# could be raised from circuit breaker or semian as well (see later)
rescue Sessions::DataStoreUnavailable
flash[:notice] = 'Customer sign in is currently unavailable'
@customer = nil
end
@sirupsen
sirupsen / setup.sh
Last active August 29, 2015 14:28
Script to set up development environment for ngx-lua
WORKDIR="/home/vagrant/src"
clone_repo() {
local name=$1; shift
local repo=$1; shift
if [[ ! -d "$WORKDIR/$name" ]]; then
@sirupsen
sirupsen / book.rb
Last active March 5, 2024 20:42
Script to import books from Instapaper to Airtable. Will not work out of the box.
class Book < Airrecord::Table
class Endorser < Airrecord::Table
self.base_key = ""
self.table_name = "Endorser"
end
self.base_key = ""
self.table_name = "Books"
has_many :endorsements, class: 'Book::Endorser', column: 'Endorsements'
@sirupsen
sirupsen / ping_less.rb
Created March 7, 2019 12:33
Ping less patch for MySQL.
# frozen_string_literal: true
# By default, ActiveRecord will issue a PING command to the database to check
# if it is active at various points. This overhead is unnecessary; we instead
# attempt to issue queries without checking the connection, then if the query
# returns an error and the connection was closed, try to reconnect.
# This also allows for reconnection during a single UoW, improving resiliency
# under transient connection failure (e.g. ProxySQL restarts).
#
# To avoid amplifying load when a database is intermittently down, the attempt
use std::{fs::OpenOptions, io::Result};
use std::io::{Read, Write, Seek, SeekFrom};
use std::slice;
use std::time::Instant;
const BUF_SIZE: usize = 1024 * 32;
const FILE_SIZE: usize = 1024 * 1024 * 512;
const QUEUE_DEPTH: usize = 32;
fn main() -> Result<()> {
use std::io::{Read, Seek, SeekFrom, Write};
use std::slice;
use std::time::Instant;
use std::{fs::OpenOptions, io::Result};
use std::ptr;
use std::mem::forget;
const BUF_SIZE: usize = 1024 * 32;
const FILE_SIZE: usize = 1024 * 1024 * 512;
const QUEUE_DEPTH: usize = 32;
require "sqlite3"
require 'set'
require 'byebug'
# Will be rebuilt at any time. Nice and incremental.
db = SQLite3::Database.new "index.db"
# Keep prefix indexes for "mos*" searches.
#
# TODO: It doesn't seem like SQLITE FTS5 supports synonyms well. That's ok, but
# we're going to want that. We can download this database from Princeton, write