Skip to content

Instantly share code, notes, and snippets.

@meetme2meat
Created April 4, 2018 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meetme2meat/534b133fe299a47c165960a973439590 to your computer and use it in GitHub Desktop.
Save meetme2meat/534b133fe299a47c165960a973439590 to your computer and use it in GitHub Desktop.
require 'bundler'
Bundler.setup(:default)
require 'ffi-rzmq'
require 'celluloid/zmq'
require 'celluloid/current'
require 'securerandom'
Celluloid::ZMQ.init
class SockOne
include Celluloid::ZMQ
attr_reader :sock
def initialize
@sock = PullSocket.new
@sock.connect('tcp://127.0.0.1:20483')
end
def read
loop do
middleware.async.start(sock.read_multipart)
end
end
end
class SockTwo
include Celluloid::ZMQ
attr_reader :sock
def initialize
@sock = PushSocket.new
@sock.connect('tcp://127.0.0.1:20484')
end
def send_response(other_id, id, response)
start_time = Time.now
sock.send(response)
end
end
class Middleware
include Celluloid
def start(data)
Handler.new.start(data)
end
end
class Handler
def start(data)
begin
execute(data)
rescue => exception
airbrake.async.notify('some exception')
ensure
sock_two.async.send_response(['i got a reply'])
end
end
def execute(data)
data
end
end
class Airbrake
include Celluloid
def notify(data)
puts "this is notify to airbrake"
end
end
supervisor = Celluloid::SupervisionGroup.run!
$middleware = supervisor.pool(Middleware, as: :middleware, size: 10)
$sock_two = supervisor.pool(SockTwo, as: :sock_two, size: 10)
$airbrake = supervisor.pool(Airbrake, as: :airbrake, size: 1)
def airbrake
$airbrake
end
def sock_two
$sock_two
end
def middleware
$middleware
end
if $0 == __FILE__
puts "Starting.."
sock_one = SockOne.new
sock_one.read
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment