Skip to content

Instantly share code, notes, and snippets.

View wyhaines's full-sized avatar
💭
Hacking

Kirk Haines wyhaines

💭
Hacking
View GitHub Profile
@wyhaines
wyhaines / gist:9b939ce08ea6f85de08c2802edb774c5
Created June 15, 2021 20:45
This is an example of some _really_ simple templating in Crystal that leverages method calls which are dispatched dynamically to get the data to fill out the template.
require "./src/send"
struct Template
getter body : String
def initialize(@body)
end
def transform(data)
@body.gsub(/(###([_\w]+)###)/) do |match|
@wyhaines
wyhaines / gist:a5238a2001fefda352f1064cba96e789
Last active April 16, 2021 04:29
Silly ways to slurp a file in Ruby
# String Subclass For Reading Files.
class FString < String
# Set a filename to read
def from(fn)
@io = IO.new(IO.sysopen(fn))
end
# Reads the file set in #from, one line at a time, yielding each line. This results in an empty FString when finished.
def feach(&blk)
self << @io.sysread(512) until self.include?($/)
@wyhaines
wyhaines / lcs.rb
Last active November 4, 2020 03:20
LCS in Ruby
require 'benchmark/ips'
class Lcs
def initialize(s1, s2)
@s1 = s1
@s2 = s2
end
def calculate
if @s1.size < @s2.size
@wyhaines
wyhaines / lcs.cr
Last active November 4, 2020 03:19
Simple LCS algorithm
require "benchmark"
class Lcs
def initialize(@s1 : String, @s2 : String); end
def calculate
if @s1.size < @s2.size
source = @s1
compare = @s2
else
@wyhaines
wyhaines / AdminDashboardOrganizationCard.js
Created August 19, 2020 15:55
useState and useEffect example to deal with async axios api calls
import React, { useState, useEffect } from 'react'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import {
Grid,
Card,
CardContent,
Button,
Divider
} from '@material-ui/core'
# frozen_string_literal: true
module Lita
module Handlers
module Wyh
# This implements a router that allows for more intelligence and sophistication
# than simple regex routing.
class EventRouter < Handler
on :unhandled_message, :event_route
@wyhaines
wyhaines / ridgepole-executor.rb
Created October 25, 2018 18:53
A ruby script to execute SQL generated with ridgepole with percona-online-schema-change for "ALTER" statements, and using mysql for the rest. It supports completely flexible/configurable specification of the pt-osc command line that is generated, using inherited config from ridgepole, or environment variables, with sensible defaults.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'erb'
require 'json'
require 'shellwords'
# This is a small utility to run ALTER TABLE statements via pt-osc while
# sending all other SQL statements to MySQL.
# rubocop: disable Metrics/ClassLength
@wyhaines
wyhaines / post-receive
Last active August 28, 2018 15:39
Stub of a post-receive hook for a really simple push based deploy based off of git remote
#!/bin/bash
ROOT="/path/to/deploy/to"
mkdir -p "${ROOT}"
BRANCH="master"
# Add to these hooks anything that you want done before or after the deployment
# such as backing up your old deployment, or restarting application containers.
BEFORE_HOOK='cd ${ROOT}'
AFTER_HOOK='cd ${ROOT}'
To recap, picture a modestly sized server. Running nginx 0.8.54 on the front end, with Passenger 3.0.5 behind it, in front of a Rails app.
Said Rails app has some pretty fast actions, so when one pokes it with a burst of, say, 100 concurrent requests, it slams through them in less than half a second.
But, when one pokes it harder, with a larger burst (200 concurrent is enough), errors start flying:
2011/03/22 06:13:33 [error] 6651#0: *33376 connect() to unix:/passenger_helper_server failed (11: Resource temporarily unavailable) while connecting to upstream....
Is this normal? It surprised me.
On an xcloud slice:
# eselect ruby list
Available Ruby profiles:
[1] ruby18 (with Rubygems)
[2] rbx110 (with Rubygems)
[3] rbx111 (with Rubygems)
[4] jruby155 (with Rubygems)
[5] jruby156 (with Rubygems) *