Skip to content

Instantly share code, notes, and snippets.

View lloydmeta's full-sized avatar
🏠
Working from home

Lloyd lloydmeta

🏠
Working from home
View GitHub Profile
@lloydmeta
lloydmeta / gist:4958987
Created February 15, 2013 07:34
Using OOBGC with Unicorn along with a UnicornKiller that kills workers if they use too much memory
#--- Credit: http://kzk9.net/unicorn-configuration-on-heroku
# config.ru
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
# Unicorn self-process killer
@lloydmeta
lloydmeta / gist:5493691
Created May 1, 2013 04:10
Adding a swap file for 16GB m2.xlarge as per recommendations on Redis documentation http://redis.io/topics/admin
#8GB => set count=8192
#16GB => set count=16384 <-- for m2.xlarge with 16GB memory, as per official Redis documentation, use the same amount of swap as there is memory
sudo dd if=/dev/zero of=/var/swapfile bs=1M count=1657 &&
sudo chmod 600 /var/swapfile &&
sudo mkswap /var/swapfile &&
echo /var/swapfile none swap defaults 0 0 | sudo tee -a /etc/fstab &&
sudo swapon -a
@lloydmeta
lloydmeta / gist:5530215
Created May 7, 2013 04:14
Cleaning of Redis keys based on pattern and external keys dump file. This allows for easy cleanup of Redis keys based on any kind of key pattern without having to care about escaping characters, memory issues, etc.
#encoding: utf-8
require 'rubygems'
require 'bundler/setup'
require 'redis'
class RedisKeyCleaner
attr_accessor :redis, :temp_file_path
@lloydmeta
lloydmeta / gist:5538871
Created May 8, 2013 07:43
Javascript API function for RockMongo to find slowish endpoints based on nginx access logs gathered from instances
// Contains questionable Javascript and Regex. You have been warned.
function () {
var counting_object = {
count_hash : {},
clean_endpoint : function(endpoint) {
return endpoint.replace(/posts\/[\d]+/,'posts/:id')
@lloydmeta
lloydmeta / gist:5545084
Last active December 17, 2015 03:39
Javascript API function for RockMongo to find slowish endpoints based on nginx access logs gathered from instances. Now sorts endpoints based on number of results in descending order !
// Contains questionable Javascript and Regex. You have been warned.
function () {
var counting_object = {
count_hash : {},
clean_endpoint : function(endpoint) {
return endpoint.replace(/(\/[\w]+\/[\d]+)/, function(v){return v.replace(/\/[\d]+/, "/:id")})
@lloydmeta
lloydmeta / gist:5545466
Last active December 17, 2015 03:48
Javascript API function for RockMongo to find slowish endpoints based on nginx access logs gathered from instances. Now sorts endpoints based on number of results in descending order ! Doesn't care about individual query params
// Contains questionable Javascript and Regex. You have been warned.
function () {
var counting_object = {
count_hash : {},
clean_endpoint : function(endpoint) {
return endpoint.replace(/(\/[\w]+\/[\d]+)/, function(v){return v.replace(/\/[\d]+/, "/:id")})
@lloydmeta
lloydmeta / gist:5662413
Created May 28, 2013 12:29
Returns a List of consecutive Doubles based on a start and end and a step value. Guarantees the end value is in the last double.
def listOfConsecutivePairsInRange(start: Double, end: Double, step: Double = 1) = {
val initialRange = start to end by step
def splitIntoConsecutivePairs(xs: List[Double]):List[(Double,Double)] = {
xs match {
case x::y::Nil => {
if (y != end) {
List((x, end))
} else {
List((x, y))
@lloydmeta
lloydmeta / RichRange.scala
Last active December 17, 2015 19:49
Pimp My Library: RichRange
package org.beachape.support
object RichRange {
implicit def range2RichRange(r: Range) = RichRange(r)
}
case class RichRange(range: Range) {
def listOfConsecutivePairsInSteps(step: Int) = {
val steppedRange = range by step
@lloydmeta
lloydmeta / clearout.rb
Last active December 18, 2015 02:39
Clear out nonce set that has, regrettably gotten out of hand thanks to race condition on setting expiry.
def cleanout_nonce(key)
loop do
print "."
break unless $redis.spop key
end
end
cleanout_nonce("nonce_set")
@lloydmeta
lloydmeta / string_extensions.rb
Created June 6, 2013 09:37
Methods to split a Ruby string into continuous CJK (Chinese/Japanese/Korean) and alphabetical string segments.
# encoding: UTF-8
class String
def contains_cjk?
!!(self =~ /\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}/)
end
def for_each_continous_cjk_alpha_part
split_into_cjk_alpha_phrases.each.with_index do |part, i|