Skip to content

Instantly share code, notes, and snippets.

View mperham's full-sized avatar

Mike Perham mperham

View GitHub Profile
~/src/ent/myapp (master *=)$ bundle exec derailed bundle:objects
Measuring objects created by gems in groups [:default, "production"]
Total allocated: 4246025 bytes (34726 objects)
Total retained: 437421 bytes (4179 objects)
allocated memory by gem
-----------------------------------
1348174 puma-3.6.0
749251 sidekiq/lib
538517 pro/lib
require "./src/sidekiq"
`redis-cli flushdb`
class LoadWorker
include Sidekiq::Worker
perform_types Int64
def perform(idx)
end
@mperham
mperham / locktest.rb
Created April 23, 2016 20:04
Distributed Locking with Redis and Ruby
require 'benchmark'
require 'sidekiq-ent'
require 'redis-lock'
require 'redis-semaphore'
require 'ruby_redis_lock'
# monkey patch to remove exponential backoff in pmckee11-redis-lock,
# otherwise this benchmark does not complete successfully.
class Redis
class Lock
$ 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
class MyCode
def process
do_something if leader?
end
def leader?
Sidekiq::Senate.leader?
end
end
@mperham
mperham / repro.md
Created July 22, 2015 21:45
Sidekiq / Rails 5 interlockup
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 April 22, 2016 20:22
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.