• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    C
  • License
    MIT License
  • Created about 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby wrapper for POSIX IPC message queues.

posix-mqueue Build Status

Minimal wrapper around the POSIX message queue. The POSIX message queue offers:

  • Persistence. Push messages while nothing is listening.
  • Simplicity. Nothing to set up. Built into Linux.
  • IPC. Blazingly fast communication between processes on the same machine.
  • Blocking and non-blocking. Listeners block until a message arrives on the queue. No polling. Sending messages doesn't block.

Note that this requires no third party message broker. The messages are handled by the kernel of your computer. Not all kernels have support for POSIX message queues, a notably example is Darwin (OS X). Darwin implements the older System V IPC API. See my SysV MQ wrapper.

Usage

In your Gemfile:

gem 'posix-mqueue'

Important notes

  1. This will not work on OS X, but on Linux and probably BSD (not tested).
  2. send and receive block. timedsend and timedreceive do not.
  3. The default message size is 4096 bytes.
  4. Linux's default queue size is 10 bytes.

Read on for details.

Example

require 'posix/mqueue'

# On Linux the queue name must be prefixed with a slash. Note it is not a file
# created at `/whatever`. It's just the name of the queue.
# Set maximum default Linux options. See next section to push those limits.
# Default options are msgsize: 4096 and maxmsg: 10
m = POSIX::Mqueue.new("/whatever", msgsize: 8192, maxmsg: 10)
m.send "hello"
m.receive
# => "hello"

fork { POSIX::Mqueue.new("/whatever").send("world") }

# Blocks until the forked process pushes to the queue
m.receive
# => "world"

# Queue is now full by default Linux settings, see below on how to increase it.
10.times { m.send rand(100).to_s }

# #size returns the size of the queue
m.size
# => 10

# #send will block until something is popped off the now full queue.
# timesend takes timeout arguments (first one is seconds, second is
# nanoseconds). Pass 0 for for both to not block, this is default.

assert_raises POSIX::Mqueue::QueueFull do
  m.timedsend "I will fail"
end

# Empty the queue again
10.times { m.receive }

# Like timedsend, timedreceive takes timeout arguments and will raise
# POSIX::Mqueue::Queueempty when it would otherwise block.
assert_raises POSIX::Mqueue::QueueEmpty do
  m.timedreceive
end

# Deletes the queue and any messages remaining.
# None in this case. If not unlinked, the queue will persist till reboot.
m.unlink

mqueue

Most important information from the manpages, with a little added information about the behavior of posix-mqueue.

/proc interfaces

Linux has some default limits you can easily change.

  1. /proc/sys/fs/mqueue/msg_max. Contains the maximum number of messages in a single queue. Defaults to 10. You should increase that number. #send will eventually block if the queue is full. #timedsend will throw QueueFull.
  2. /proc/sys/fs/mqueue/msgsize_max. Maximum size of a single message. Defaults to 8192 bytes. posix-mqueue defaults to 4096 bytes. Overwrite this by passing {msgsize: 8192} as the second argument when initializing.
  3. /proc/sys/fs/mqueue/queues_max. Maximum number of queues on the system. Defaults to 256.

Virtual filesystem

The message queue is created as a virtual file system. That means you can mount it:

# sudo mkdir /dev/queue
# sudo mount -t mqueue none /dev/queue

Add a queue and a few tasks, count the characters (19):

$ irb
> require 'posix/mqueue'
=> true
> m = POSIX::Mqueue.new("/queue")
=> #<POSIX::Mqueue:0xb8c9fe88>
> m.send "narwhal"
=> true
> m.send "walrus"
=> true
> m.send "ponies"
=> true

Inspect the mounted filesystem:

$ ls /dev/queue/
important  mails  queue
$ cat /dev/queue/queue
QSIZE:19         NOTIFY:0     SIGNO:0     NOTIFY_PID:0

Here QSIZE is the bytes of data in the queue. The other flags are about notifications which posix-mqueue does not support currently, read about them in mq_overview(7).

More Repositories

1

logrus

Structured, pluggable logging for Go.
Go
24,587
star
2

napkin-math

Techniques and numbers for estimating system's performance from first-principles
Rust
2,754
star
3

zk

Zettelkasten on the command-line 📚 🔍
Ruby
542
star
4

airrecord

Ruby wrapper for Airtable, your personal database
Ruby
295
star
5

dotfiles

Personal UNIX toolbox
Shell
188
star
6

anki-airtable

Sync Anki with Airtable!
Python
170
star
7

localjob

Simple, self-contained background queue built on top of SysV message queues.
Ruby
145
star
8

sysvmq

Ruby wrapper for SysV IPC message queues.
C
50
star
9

initcwnd

Script to analyze the initial congestion window of any https server
Ruby
47
star
10

contestrus

An open source algorithmic contest platform
Ruby
31
star
11

Mongui

A simple GUI (web interface) for Mongo using Sinatra.
Ruby
24
star
12

progressrus

Monitor the progress of remote, long-running jobs.
Ruby
23
star
13

cachedis

Cachedis caches expensive (database) queries in Redis
Ruby
21
star
14

flying-cat

A modular, Lua-based operating system project.
C
15
star
15

tivitybalancer

Chrome extension that sends you to a random productive site from a list
JavaScript
14
star
16

airtable-rs

Rust wrapper for the Airtable API
Rust
12
star
17

vim-execrus

Framework for context dependent execution of commands in Vim
Vim Script
10
star
18

informatics

Solutions to various Informatics tasks from different judges.
Roff
8
star
19

can-has-lolcat

Fetches a random lolcat, and returns the appropriate output format.
Ruby
8
star
20

toxiproxy-rails-example

Example Rails application that uses Toxiproxy for resiliency testing.
Ruby
7
star
21

Flimpl

Simple and lightweight PHP MVC framework.
PHP
4
star
22

SteakMachine

Javascript state machine for those who like their states medium rare.
JavaScript
3
star
23

skypelogs

Ruby wrapper for your Skype logs.
Ruby
3
star
24

everdown

Sync Markdown files with Evernote
Ruby
3
star
25

instapaper-rs

Rust Instapaper API wrapper.
Rust
3
star
26

tomb

Tomb mirror from launchpad.net/tomb
Go
3
star
27

godis

Go
3
star
28

kafka

Apache Kafka Ruby client built as a wrapper for librdkafka
C
2
star
29

CCPL

Different tasks, solved in various languages.
Java
2
star
30

zooconf

Manage templated configuration files from Zookeeper
2
star
31

applog

Mirror og tideland.biz' applog for Go
Go
2
star
32

h

programming language
Ruby
2
star
33

Ruby-Introduciton-course

Slides for Ruby introduction course
Ruby
2
star
34

truffle-grater

http://trufflegrater.com
CSS
1
star
35

Lurker

Irc bot for Lurking project.
Ruby
1
star
36

Mongoground

A little something I threw together in Sinatra and Mongo for testing/show off purposes.
JavaScript
1
star
37

babushka-deps

Ruby
1
star
38

Shouldnt-you-be-doing-something-Awesome

Shouldn't you be doing something Awesome?
Ruby
1
star