Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Perham mperham

View GitHub Profile
View gist:d35154a06946cee8a64b
$ ruby -v
ruby 2.0.0p598 (2014-11-13) [x86_64-linux]
$ irb
irb(main):001:0> require 'json'
=> true
irb(main):002:0> quit
$ bundle exec irb
rirb(main):001:0> require 'json'
LoadError: cannot load such file -- json
View example.rb
class MyCode
def process
do_something if leader?
end
def leader?
Sidekiq::Senate.leader?
end
end
@mperham
mperham / repro.md
Created Jul 22, 2015
Sidekiq / Rails 5 interlockup
View repro.md
git clone git://github.com/mperham/sidekiq
cd sidekiq && git checkout rails5
cd myapp && bundle
bundle exec sidekiq
# in another terminal tab
bundle exec rails s

Now hit http://localhost:3000/work to create a random set of jobs for sidekiq to perform.

@mperham
mperham / gist:5f492a2233ed44d1bb2b
Last active Apr 22, 2016
Golang high-level crypto APIs
View gist:5f492a2233ed44d1bb2b

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()
View gist:7d763bdc42465caf17c7

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 Aug 29, 2015
Ruby 2.1.2 memory bloat/leak
View gc opts
> 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"}
View gist:11262832
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 Aug 29, 2015
Sidekiq performance 2014
View bench.rb
require 'sidekiq'
class LazyWorker
include Sidekiq::Worker
def perform
# nothing
end
end
Sidekiq.configure_client do |config|
@mperham
mperham / gist:9864177
Created Mar 29, 2014
mime-types - such garbage
View gist:9864177
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 Feb 10, 2014
Rubby numeric handling
View gist:8922782
> 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'