GithubHelp home page GithubHelp logo

gwright / rbczmq Goto Github PK

View Code? Open in Web Editor NEW

This project forked from methodmissing/rbczmq

1.0 1.0 0.0 9.18 MB

Ruby extension that wraps the official high level ZeroMQ C API ( http://czmq.zeromq.org/ )

Home Page: http://github.com/methodmissing/rbczmq

License: MIT License

rbczmq's Introduction

rbczmq - binding for the high level ZeroMQ C API

© 2011 Lourens Naudé (methodmissing), with API guidance from the czmq (czmq.zeromq.org/) project.

http://github.com/methodmissing/rbczmq

NOT YET PRODUCTION READY, BUT ALMOST

About ZeroMQ

In a nutshell, ZeroMQ is a hybrid networking library / concurrency framework. I quote the ØMQ Guide (zguide.zeromq.org/page:all) :

“ØMQ (ZeroMQ, 0MQ, zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry whole messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fanout, pub-sub, task distribution, and request-reply. It’s fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPL open source.”

Another ZeroMQ extension ?

This extension bundles both ZeroMQ (libzmq, www.zeromq.org/) and CZMQ (libczmq, czmq.zeromq.org/) and as such have no third party dependencies other than a Ruby distribution and a C compiler. My goals for this project were :

  • Access to a powerful messaging technology without having to install a bunch of dependencies

  • A stable and mostly version agnostic (2.x and 3.x series) API

  • Leverage and build upon a mature and maintained client (CZMQ)

  • Target Ruby distributions with a stable and comprehensive C API (MRI, Rubinius, JRuby is work in progress)

  • Support for running sockets in Threads - both green and native threads should be supported and preempt properly with edge-triggered multiplexing from libzmq.

  • Integrate with the Garbage Collector in a predictable way. CZMQ and the ZeroMQ framework is very fast and can allocate an enormous amount of objects in no time when using the Frame, Message and String wrappers. Resources such as socket connections should be cleaned up when objects are finalized as well.

  • Expose Message envelopes and Frames to developers as well to allow for higher level protocols and constructs.

  • Enforce well known best practices such as restricting socket interactions to within the thread the socket was created in etc.

Performance

ZeroMQ can have higher throughput than TCP in most cases by using a message batching technique. Please have a look through the Performance section in the ZeroMQ FAQ (www.zeromq.org/area:faq#toc2) for further implementation details.

Some notes about these benchmarks :

  • Messages go through the full network stack on localhost (TCP/IP transport)

  • The sender and receiver endpoints are Ruby processes which coerce transferred data to native String objects on both ends.

  • There’s thus a definite method dispatch cost in addition to intermittent pauses from the Garbage Collector

  • It’s still plenty fast for most soft real-time applications and we’re able to push in excess of 1 gigabits/s with the 1024 byte payloads. A language with automatic memory management cannot easily comply to hard real-time guarantees anyways.

TCP/IP loopback, 100k messages, 100 byte payloads

Lourenss-MacBook-Air:rbczmq lourens$ MSG_COUNT=100000 MSG_SIZE=100 ruby perf/pair.rb
Local pids: 2042
Remote pid: 2043
Sent 100000 messages in 0.3933s ...
[2043] Memory used before: 1128kb
[2043] Memory used after: 3012kb
[2042] Memory used before: 1120kb
====== [2042] transfer stats ======
message encoding: string
message size: 100 [B]
message count: 100000
mean throughput: 227978 [msg/s]
mean throughput: 182.383 [Mb/s]
[2042] Memory used after: 22432kb

TCP/IP loopback, 100k messages, 1024 byte payloads

Lourenss-MacBook-Air:rbczmq lourens$ MSG_COUNT=100000 MSG_SIZE=1024 ruby perf/pair.rb
Local pids: 2027
Remote pid: 2028
Sent 100000 messages in 0.641198s ...
[2028] Memory used before: 1120kb
[2028] Memory used after: 12776kb
[2027] Memory used before: 1144kb
====== [2027] transfer stats ======
message encoding: string
message size: 1024 [B]
message count: 100000
mean throughput: 160756 [msg/s]
mean throughput: 1316.919 [Mb/s]
[2027] Memory used after: 189004kb

TCP/IP loopback, 100k messages, 2048 byte payloads

Lourenss-MacBook-Air:rbczmq lourens$ MSG_COUNT=100000 MSG_SIZE=2048 ruby perf/pair.rb
Local pids: 2034
Remote pid: 2035
Sent 100000 messages in 0.94703s ...
[2035] Memory used before: 1140kb
[2035] Memory used after: 7212kb
[2034] Memory used before: 1128kb
====== [2034] transfer stats ======
message encoding: string
message size: 2048 [B]
message count: 100000
mean throughput: 123506 [msg/s]
mean throughput: 2023.528 [Mb/s]
[2034] Memory used after: 277712kb

Have a play around with the performance runner and other socket pairs as well - github.com/methodmissing/rbczmq/tree/master/perf

Usage

As a first step I’d highly recommend you read (and reread) through the zguide (zguide.zeromq.org/page:all) as understanding the supported messaging patterns and topologies is fundamental to getting the most from this binding. Here’s a few basic examples. Please refer to documentation (methodmissing.github.com/rbczmq/) and test cases (github.com/methodmissing/rbczmq/tree/master/test) for detailed usage information.

Basic send / receive, in process transport

ctx = ZMQ::Context.new
rep = ctx.socket(:PAIR)
port = rep.bind("inproc://send.receive")
req = ctx.socket(:PAIR)
req.connect("inproc://send.receive")
req.send("ping") # true
rep.recv # "ping"

ctx.destroy

Fair-queued work distribution to a set of worker threads

ctx = ZMQ::Context.new
push = ctx.bind(:PUSH, "inproc://push-pull-distribution.test")
threads = []
5.times do
  threads << Thread.new do
    pull = ctx.connect(:PULL, "inproc://push-pull-distribution.test")
    msg = pull.recv
    pull.close
    msg
  end
end

sleep 0.5 # avoid "slow joiner" syndrome
messages = %w(a b c d e f)
messages.each do |m|
  push.send m
end

threads.each{|t| t.join }
threads.all?{|t| messages.include?(t.value) } # true

ctx.destroy

Async request / reply routing

ctx = ZMQ::Context.new
router = ctx.bind(:ROUTER, "inproc://routing-flow.test")
dealer = ctx.socket(:DEALER)
dealer.identity = "xyz"
dealer.connect("inproc://routing-flow.test")

router.sendm("xyz")
router.send("request")
dealer.recv # "request"

dealer.send("reply")
router.recv # "xyz"
router.recv # "reply"

ctx.destroy

Send / receive frames

ctx = ZMQ::Context.new
rep = ctx.socket(:PAIR)
rep.bind("inproc://frames.test")
req = ctx.socket(:PAIR)
req.connect("inproc://frames.test")
ping = ZMQ::Frame("ping")
req.send_frame(ping) # true
rep.recv_frame # ZMQ::Frame("ping")
rep.send_frame(ZMQ::Frame("pong")) # true
req.recv_frame # ZMQ::Frame("pong")
rep.send_frame(ZMQ::Frame("pong")) # true
req.recv_frame_nonblock # nil
sleep 0.3
req.recv_frame_nonblock # ZMQ::Frame("pong")

ctx.destroy

Send / receive messages

ctx = ZMQ::Context.new
rep = ctx.socket(:PAIR)
rep.bind("inproc://messages.test")
req = ctx.socket(:PAIR)
req.connect("inproc://messages.test")

msg = ZMQ::Message.new
msg.push ZMQ::Frame("header")
msg.push ZMQ::Frame("body")

req.send_message(msg) # nil

recvd_msg = rep.recv_message
recvd_msg.class # ZMQ::Message
recvd_msg.pop # ZMQ::Frame("header")
recvd_msg.pop # ZMQ::Frame("body")

ctx.destroy

Resources

Requirements

  • A POSIX compliant OS, known to work well on Linux, BSD variants and Mac OS X

  • Ruby MRI 1.8, 1.9 or Rubinius (JRuby capi support forthcoming)

  • A C compiler

Installation

Rubygems installation

gem install rbczmq

Building from source

git clone git@github.com:methodmissing/rbczmq.git
rake

Running tests

rake test

TODO

  • ZMQ::Message#save && ZMQ::Message.load

  • ZMQ::Socket#handler = x. Assert handler interface here instead ???

  • Callback handler for ZMQ::Socket useful beyond just ZMQ::Loop ?

  • Optimize zloop handler callbacks (perftools)

  • OS X leaks utility - developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/leaks.1.html

  • Handle GC issue with timers in loop callbacks

  • czmq send methods aren’t non-blocking by default

  • Catch EFSM for REQ/REP pairs and raise in a format better suited for the user

  • Enforce socket timeouts

  • Look into handling signals properly

  • ZMQ.poll implementation

  • Revisit the ZMQ::Loop API

  • Restrict pair sockets to inproc transport only

  • Revisit JRuby C API support

  • Push gem out to rubygems.org

  • RDOC fail on mixed C and Ruby source files that document that same constants

  • GC guards to prevent recycling objects being sent / received.

  • uuid-dev package dependency - look into a vendored package for this

Contact, feedback and bugs

This project is still work in progress and I’m looking for guidance on API design, use cases and any outlier experiences. Please log bugs and suggestions at github.com/methodmissing/rbczmq/issues

rbczmq's People

Contributors

methodmissing avatar

Stargazers

Gary Wright avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.