Skip to content

Instantly share code, notes, and snippets.

View mperham's full-sized avatar

Mike Perham mperham

View GitHub Profile
@mperham
mperham / gist:5f492a2233ed44d1bb2b
Last active April 16, 2024 12:31
Golang high-level crypto APIs

Go has a number of low-level crypto APIs which check off marketing bullet-points (got FIPS supprt, check!) but is missing an high-level API usable by mere mortal programmers. Imagine you want to create a document, sign it and verify that document later. Now check out Go's crypto APIs and give up in frustration after an hour of Googling.

The API should encapsulate a half-dozen common operations and make them as easy as possible. Avoid choice where possible, just pick something reasonably secure in 2014 for me and use it! I'm speaking specifically of a few basic actions (yes, this API is very naive/non-idiomatic), call it crypto/easy:

  • Create me a public/private key pair and save it to the filesystem.
// create and persist a keypair to the current directory.
// this is just a one-time operation, now we have a keypair to use.
easy.CreateKeyPair()
@mperham
mperham / gist:7d763bdc42465caf17c7
Last active August 29, 2015 14:02
Rule execution

I want to build a system which uses rules. These rules will change hourly (i.e. "this rule is effective from 2pm to 6pm"). The rules must support arbitrarily complex logic on just 2-3 pre-defined variables and result in a boolean:

item.a > 10 && item.b == 0 || item.category == FOOTWEAR

These rules will be executed hundreds of times per second so I can't afford the overhead of plain old eval. I want to reload the current effective ruleset from the database hourly, precompile each rule's logic string and execute it via the quickest method possible. It might look something like this:

class Rule
@mperham
mperham / gc opts
Last active August 29, 2015 14:01
Ruby 2.1.2 memory bloat/leak
> pp ENV.select {|x| x =~ /^RUBY/ }
{"RUBY_GC_HEAP_GROWTH_FACTOR"=>"1.3",
"RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR"=>"1.3",
"RUBY_GC_HEAP_INIT_SLOTS"=>"600000",
"RUBY_GC_MALLOC_LIMIT"=>"90000000",
"RUBYOPT"=>"-rbundler/setup",
"RUBYLIB"=>
"/usr/local/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib"}
@mperham
mperham / gist:11262832
Created April 24, 2014 17:33
FCC comment
Allowing ISPs to charge for different levels of access to consumers
creates an uneven playing field for Internet-based companies. As a
consumer, I already pay an ISP monthly to provide me a level of service when
accessing the internet. I expect that access to be fair for all
companies so that I have equal access to all services on the Internet.
Allowing large companies to pay for faster "lanes" means that small
companies cannot compete fairly and hurts the normal pace of
innovation. Please keep Net Neutrality in place so the Internet
continues to be a place of innovation and not monied incumbents.
@mperham
mperham / bench.rb
Last active August 29, 2015 13:57
Sidekiq performance 2014
require 'sidekiq'
class LazyWorker
include Sidekiq::Worker
def perform
# nothing
end
end
Sidekiq.configure_client do |config|
@mperham
mperham / gist:9864177
Created March 29, 2014 22:38
mime-types - such garbage
640 ::CLASS
645 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:ARRAY
676 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/stub_specification.rb:21:STRING
722 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/x86_64-darwin13.0/openssl.bundle:0:STRING
724 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/specification.rb:1338:OBJECT
734 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/requirement.rb:92:ARRAY
734 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/requirement.rb:92:STRING
872 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/requirement.rb:53:OBJECT
876 /Users/mikep/.gem/ruby/2.1.1/gems/sprockets-2.10.0/lib/sprockets/mime.rb:1:STRING
884 /Users/mikep/.gem/ruby/2.1.1/gems/mime-types-1.25/lib/mime/types.rb:423:STRING
@mperham
mperham / gist:8922782
Created February 10, 2014 19:43
Rubby numeric handling
> require 'bigdecimal'
=> true
> Float('foo')
ArgumentError: invalid value for Float(): "foo"
from (irb):2:in `Float'
from (irb):2
from /Users/mikep/.rubies/ruby-2.0.0-p353/bin/irb:12:in `<main>'
> Integer('foo')
ArgumentError: invalid value for Integer(): "foo"
from (irb):3:in `Integer'
@mperham
mperham / gist:8463495
Created January 16, 2014 21:10
Using #join with a Sidekiq::Batch in Rails to parallelize data migration
class MoveInventoryUpdatesToS3 < ActiveRecord::Migration
def up
batch = Sidekiq::Batch.new
batch.jobs do
InventoryUpdate.where("content is not null").pluck(:id).each do |iuid|
InventoryUpdate.delay.send_to_s3(iuid)
end
end
# wait for all jobs to finish
@mperham
mperham / gist:8323180
Created January 8, 2014 19:40
Using tempfile
# Use open to ensure tempfile is cleaned up asap
Tempfile.open("somename") do |tfile|
# Stage 1 - create the tempfile with some data
tfile.write "some bytes"
tfile.close
# Stage 2 - use the tempfile
File.open(tfile.path) do |file|
assert "some bytes", file.read
@mperham
mperham / yolo.rb
Created July 12, 2013 17:15
Have 100s of models and thousands of tests? Prefer simplicity over security when upgrading to Rails4? YOLO!
class ActiveRecord::Base
class << self
def inherited_yolo(klass)
old_inherited(klass)
klass.attr_accessible(*klass.column_names.map(&:to_sym)) unless klass.abstract_class?
end
alias_method :old_inherited, :inherited
alias_method :inherited, :inherited_yolo
end
end