OVERVIEW
Project | ichannel |
---|---|
Homepage | https://github.com/robgleeson/ichannel |
Documentation | http://rubydoc.info/github/robgleeson/ichannel/frames |
CI | |
Author | Robert Gleeson |
DESCRIPTION
ichannel is a channel for interprocess communication between ruby processes on the same machine or network. The basic premise is that you can “put” a ruby object onto the channel and on the other end(maybe in a different process, or maybe on a different machine) you can “get” the object from the channel.
The two main modes of transport are a UNIXSocket and redis. A (unbound) unix socket is local to one machine but it can act as a queue, it has no external dependencies, & it shares the same interface as its redis counterpart. A redis channel can do all of the above but also expand its reach beyond one machine by sending and receiving messages from other channels running on different machines.
A channel depends on a serializer when reading and writing from the underlying transport(i.e: redis or a unix socket) but the choice of serializer is up to you. The default is set to be Marshal but a serializer can be any object that implements dump and load. A few off the top of my head would be JSON, YAML, or MessagePack(with a wrapper, see examples).
EXAMPLES
1.
A demo of how to pass ruby objects through a channel and also between processes.
Marshal is the serializer of choice,
and a streamed UNIXSocket is mode of transport:
ruby
channel = IChannel.unix Marshal
pid = fork do
channel.put Process.pid
channel.put 'Hello!'
end
Process.wait pid
channel.get # => Fixnum
channel.get # => 'Hello!'
2.
Knowing when a channel is readable can be useful so that you can avoid a blocking read on the underlying UNIXSocket. This (bad) example demonstrates how to do that:
ruby
channel = IChannel.unix Marshal
pid = fork do
sleep 3
channel.put 42
end
until channel.readable?
# do something else
end
channel.get # => 42
Process.wait pid
3.
A demo of a channel sending messages between machines by using redis as a backend:
```ruby channel = IChannel.redis Marshal, key: “readme-example” channel.put %w(a)
In another process, on another machine, far away…
channel = IChannel.redis Marshal, key: “readme-example” channel.get # => [“a”] ```
4.
MessagePack doesn’t implement dump
or load
but a wrapper can be easily
written:
```ruby module MyMessagePack def self.dump(msg) MessagePack.pack(msg) end
def self.load(msg) MessagePack.unpack(msg) end end channel = IChannel.unix MyMessagePack ```
PLATFORM SUPPORT
supported
- CRuby (1.9+)
- Rubinius (1.9+)
- JRuby (1.9+ - some tests skipped)
JRuby is supported and passes the test suite but it has a few skips. Three skips are because jruby does not implement Kernel.fork and one looks like a possible bug in JRuby’s Marshal when trying to deserialize a channel that uses a unix socket. The other 24 tests pass on jruby, & those tests cover both unix sockets & redis.
unsupported
- Any 1.8 implementation
- MacRuby
INSTALL
If you plan on using redis you’ll need to install the ‘redis’ gem. It’s optional:
$ gem install redis
And to finish:
$ gem install ichannel
SEE ALSO
- ifuture
futures built on process forks and ichannel.
LICENSE
MIT. See LICENSE.txt.