Skip to content

Instantly share code, notes, and snippets.

Avatar

nicholas a. evans nevans

View GitHub Profile
@nevans
nevans / verify_and_decrypt_session_cookie.rb
Last active Mar 31, 2020
Rails 5.2.4: verifying and decrypting session cookies, to be used from rails console or in a script with the rails application loaded
View verify_and_decrypt_session_cookie.rb
@nevans
nevans / http_connection_bm.rb
Last active Aug 29, 2015
HTTP with or without TCP_NODELAY (client or server)
View http_connection_bm.rb
require "benchmark"
require "typhoeus"
require "excon"
require "net/http/persistent"
reps = ENV.fetch("HTTP_BM_REPS", 1000).to_i
url = ENV.fetch("HTTP_BM_URL", "http://localhost:5984/")
nodelay = ENV.fetch("HTTP_BM_NODELAY", "true") == "true"
Excon.defaults[:tcp_nodelay] = nodelay
@nevans
nevans / gist:9374041
Last active Oct 7, 2019
simple ruby console histogram generator
View gist:9374041
# Pass in an enumeration of data and
# (optionally) a block to extract the grouping aspect of the data.
#
# Optional: sort_by lambda (operates on group key and count)
#
def puts_hist(data, sort_by:nil, &blk)
data = data.map(&blk) if blk
counts = data.each_with_object(Hash.new(0)) {|k,h| h[k]+=1}
max = counts.values.max
width = Pry::Terminal.size!.last
@nevans
nevans / README.md
Last active Aug 28, 2020
Improving speed on slow CouchDB reduce functions
View README.md

A common pattern in my CouchDB view reductions is to "merge" together the objects generated by the map function while preserving the original form, and only query that view with group=true. It's easiest to write the view reductions naively, so they continue merging the data all the way up to the top-level reduction (group=false).

But because CouchDB stores its b+trees with the reduction for each b+tree node stored in that node, moderately sized objects can result in a lot of extra writes to disk, and moderately complicated functions can cause a lot of wasted CPU time in the indexer running merge javascript that is never queried. Re-balancing the b+tree compounds this problem. This can cause the initial creation of large indexes to slow down tremendously. If the index becomes terribly fragmented, this will also affect query speed.

One solution: once the reduction is beyond the keys at the group level I care about, stop running the merge code and return the simplest data that works (e.g. null or

@nevans
nevans / celluloid-retrying_supervisor_proxy.rb
Created Sep 11, 2012
celluloid supervisor that attempts to run the called method no matter what
View celluloid-retrying_supervisor_proxy.rb
#!/usr/bin/env ruby
# encoding: UTF-8
require "celluloid"
class BustedActor
include Celluloid
include Celluloid::Logger
def works_great; 42 end
def broken; raise "hell" end
end
View rails_class_level_memoization.md

The following code came up during a code review:

def self.account_for(email)
  @account_for ||= {}
  return @account_for[email] if @account_for.include?(email)
  @account_for[email] = Account.find(email)
end
@nevans
nevans / bm.rb
Created Mar 21, 2012
Why is EM.epoll slowing down my connections in other threads?
View bm.rb
#!/usr/bin/env ruby
# copied from https://gist.github.com/939696, and
# edited to add redis (which is where I was experiencing the issue)
require 'rubygems'
require 'net/http'
require 'hiredis'
require "redis/connection/hiredis"
require 'redis'
@nevans
nevans / foo_task.rb
Created Mar 8, 2012
good or bad ruby on rails OO?
View foo_task.rb
# I have a new feature that primarily deals with a single class. It's a
# relatively small and self contained feature; I don't expect that other
# features will ever develop dependencies on it, but it will be highly
# dependant on the single class that it deals with. For various reasons, it
# isn't appropriate to make an Observer class. I'd like to keep all of the
# code pertaining to this feature highly cohesive (localized, in one file if
# possible). But it does make some specific demands of the class that it's
# coupled with.
module FooTask
@nevans
nevans / imap_astring.treetop
Created Dec 12, 2011
tree top and IMAP astrings
View imap_astring.treetop
# See http://tools.ietf.org/html/rfc3501#section-9
# INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1 - Formal Syntax
module IMAP
grammar Astring
# astring = 1*ASTRING-CHAR / string
rule astring
ASTRING_CHAR+ / string
end
@nevans
nevans / sugared_resque_job.rb
Created Nov 29, 2011
Maybe a good way to make resque job definition just a tad simpler.
View sugared_resque_job.rb
module SugaredResqueJob
def self.new(queue, resque=Resque, &perform)
Module.new do
extend self
define_method :queue do queue end
define_method :enqueue do |*args| resque.enqueue(self, *args) end
define_method :perform do |*args| perform.call( *args) end
end
end
end
You can’t perform that action at this time.