Skip to content

Instantly share code, notes, and snippets.

@grantr
grantr / curvecp_handshake.rb
Last active July 19, 2018 15:16
CurveCP handshake protocol in Ruby
# A demonstration of the CurveCP handshake protocol. This protocol has many
# favorable security properties described at http://curvecp.org.
#
# In addition to its security advantages, it has the following favorable properties:
# * Needs only 2 messages (1 from client, 1 from server) before application
# messages can be exchanged (3 before the server can send application messages)
# * Does not require the server to keep protocol state between handshake messages.
#
# An overview of the protocol:
#
@grantr
grantr / ssl.thor
Created July 10, 2012 17:05
ssl cert generation with thor and openssl (ruby 1.9)
# Step 1: gem install thor
# Step 2: thor ssl:generate_ca && thor ssl:generate_cert
require 'thor'
require 'securerandom'
class SSL < Thor
desc "generate_ca", "Generate a Root CA"
method_option :name, type: :string, desc: "Common name"
@grantr
grantr / keybase.md
Last active April 13, 2016 19:48
keybase.md

Keybase proof

I hereby claim:

  • I am grantr on github.
  • I am grantr (https://keybase.io/grantr) on keybase.
  • I have a public key ASAJ-N6ihtij2BAlmB2DPy4dvJGOh2A4xz7MDMjXzL5DZwo

To claim this, I am signing this object:

@grantr
grantr / spec_helper.rb
Last active December 11, 2015 07:38
kill new actors in specs
RSpec.configure do |config|
# terminate actors created during tests
config.before(:each) do
@running_actors = Celluloid::Actor.all
end
config.after(:each) do
new_actors = Celluloid::Actor.all - @running_actors
new_actors.each { |a| a.terminate if a.alive? }
end
@grantr
grantr / gist:4526886
Created January 13, 2013 23:46
zmq router sockets double connect
>> require 'ffi-rzmq'
=> true
>> ctx = ZMQ::Context.new
=> #<ZMQ::Context:0x00000001ae9d30 @io_threads=1, @max_sockets=1024, @context=#<FFI::Pointer address=0x00000001c46690>>
>> r1 = ctx.socket(ZMQ::ROUTER)
=> #<ZMQ::Socket:0x00000001b05170 @receiver_klass=ZMQ::Message, @socket=#<FFI::Pointer address=0x00000001e44e00>, @name="ROUTER", @int_cache=nil, @longlong_cache=nil, @more_parts_array=[], @option_lookup=[nil, nil, nil, nil, 1, 2, 2, 2, 0, 0, nil, 0, 0, 1, 0, 0, 0, 0, 0, 0, nil, 0, 1, 0, 0, nil, nil, 0, 0, nil, nil, 0, nil, 0, 0, 0, 0, 0, 0]>
>> r2 = ctx.socket(ZMQ::ROUTER)
=> #<ZMQ::Socket:0x00000001b074e8 @receiver_klass=ZMQ::Message, @socket=#<FFI::Pointer address=0x00000001cbbe20>, @name="ROUTER", @int_cache=nil, @longlong_cache=nil, @more_parts_array=[], @option_lookup=[nil, nil, nil, nil, 1, 2, 2, 2, 0, 0, nil, 0, 0, 1, 0, 0, 0, 0, 0, 0, nil, 0, 1, 0, 0, nil, nil, 0, 0, nil, nil, 0, nil, 0, 0, 0, 0, 0, 0]>
>> r1.setsockopt(ZMQ::IDENTITY, "r1")
=> 0
@grantr
grantr / client.irb
Created January 11, 2013 22:12
zensu dynamic configuration and remote broadcast
>> require 'zensu'
=> true
>> Zensu::Client::App.run!; nil
=> nil
>> Zensu.config.servers = ["tcp://127.0.0.1:58000"]
=> ["tcp://127.0.0.1:58000"]
>> D, [2013-01-11T14:08:30.692941 #7480] DEBUG -- default: got connect_delayed socket event: event [2], addr [tcp://127.0.0.1:58000], fd [115], field2 [115]
D, [2013-01-11T14:08:30.694223 #7480] DEBUG -- default: got connected socket event: event [1], addr [tcp://127.0.0.1:58000], fd [19], field2 [19]
I, [2013-01-11T14:08:32.152452 #7480] INFO -- default: 76711953-fa99-4586-8520-000000050000 up
D, [2013-01-11T14:09:37.599686 #7480] DEBUG -- default: handled broadcast: zensu.broadcast.topic hello clients
>> require 'celluloid/zmq'
=> true
>> socket = Celluloid::ZMQ::DealerSocket.new
=> #<Celluloid::ZMQ::DealerSocket:0x00000001d7d470>
>> m = Celluloid::ZMQ::SocketMonitor.new(socket, "socket.monitor")
=> #<Celluloid::ZMQ::SocketMonitor:0x00000001bbf9d0 @socket=#<Celluloid::ZMQ::DealerSocket:0x00000001d7d470>, @topic="socket.monitor", @flags=1023, @listener=#<Celluloid::ZMQ::PairSocket:0x00000001bbf980>>
>> m.terminate
NoMethodError: undefined method `terminate' for #<Celluloid::ZMQ::SocketMonitor:0x00000001bbf9d0>
from (irb):4
from /home/grantr/.rbenv/versions/1.9.3-p362/bin/irb:12:in `<main>'
@grantr
grantr / monitor.rb
Last active December 10, 2015 21:38
ffi-rzmq socket monitor test
require 'ffi-rzmq'
class Event
class Layout < FFI::Struct
layout :event, :int,
:data, ZMQ::LibZMQ::EventData
end
def initialize(msg)
@data = Layout.new(msg.data)
@grantr
grantr / ring_manager.rb
Created December 14, 2012 01:13
Celluloid ring pool example
require 'celluloid'
module Celluloid
class RingManager
include Celluloid
trap_exit :crash_handler
def initialize(worker_class, options = {})
@size = options[:size] || [Celluloid.cores, 2].max
raise ArgumentError, "minimum ring size is 2" if @size < 2
@grantr
grantr / benchmark.out
Created November 21, 2012 20:56
Celluloid::IncidentLogger benchmarks
MRI 1.9.3-p125
==== Ruby standard logger ====
Calculating -------------------------------------
standard 3361 i/100ms
-------------------------------------------------
standard 35759.4 (±5.5%) i/s - 181494 in 5.090712s
==== below level ====
Calculating -------------------------------------
below level 47480 i/100ms