sysv_mq

package module
v0.0.0-...-01e8b99 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 4 Imported by: 3

README

Go wrapper for SysV Message Queues

Build Status

sysv_mq is a Go wrapper for SysV Message Queues. It's important you read the manual for SysV Message Queues, msgrcv(2) and msgsnd(2) before using this library. sysv_mq is a very light wrapper, and will not hide any errors from you.

Documentation for the public API can be viewed at Godoc.

sysv_mq is tested on Linux and OS X. To run the tests run make test. This makes sure that any messages queues currently on your system are deleted before running the tests.

Example

Example which sends a message to the queue with key: 0xDEADBEEF (or creates it if it doesn't exist).

package main

import (
	"fmt"
	"github.com/Shopify/sysv_mq"
)

func main() {
	mq, err := sysv_mq.NewMessageQueue(&sysv_mq.QueueConfig{
		Key:     0xDEADBEEF,               // SysV IPC key
		MaxSize: 1024,                     // Max size of a message
		Mode:    sysv_mq.IPC_CREAT | 0600, // Creates if it doesn't exist, 0600 permissions
	})
	if err != nil {
		fmt.Println(err)
	}

	// Send a message to the queue, with message type 1, without flags.
	err = mq.SendString("Hello World", 1, 0)
	if err != nil {
		fmt.Println(err)
	}

	// Receive a message from the queue, 0 gives you the top message regardless of
	// message type passed to send().
	response, mtype, err := mq.ReceiveString(0)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("[%d] %s", mtype, response)
	// Output:
	// [1] Hello World
}

Caveats

  • The underlying SysV API works with binary data. You can send any kind of byte slice using SendBytes() and ReceiveBytes(). SendString() and ReceiveString() are a simple wrapper around the byteslice functions for convenience, and will use UTF-8 encoding.
  • The send and receive methods are blocking by default. You can pass the sysv_mq.IPC_NOWAIT flag to make them return immediately.
  • SendBytes(), ReceiveBytes() and NewMessageQueue() all do syscalls, and these could be interrupted by a signal (very common if you do a blocking ReceiveBytes()). The error will be EAGAIN in that case. It's not wrapped here, because EAGAIN is also the error if the call would block or the queue is full. Consult the manual for more information.

Documentation

Index

Examples

Constants

View Source
const (
	IPC_CREAT  = C.IPC_CREAT
	IPC_EXCL   = C.IPC_EXCL
	IPC_NOWAIT = C.IPC_NOWAIT

	IPC_STAT = C.IPC_STAT
	IPC_SET  = C.IPC_SET
	IPC_RMID = C.IPC_RMID

	MemoryAllocationError   = "malloc failed to allocate memory"
	MessageBiggerThanBuffer = "message length is longer than the size of the buffer"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MessageQueue

type MessageQueue struct {
	// contains filtered or unexported fields
}

Represents the message queue

Example
mq, err := NewMessageQueue(&QueueConfig{
	Key:     0xDEADBEEF,
	MaxSize: 1024,
	Mode:    IPC_CREAT | 0600,
})
if err != nil {
	fmt.Println(err)
}

err = mq.SendBytes([]byte("Hello World"), 1, IPC_NOWAIT)
if err != nil {
	fmt.Println(err)
}

response, _, err := mq.ReceiveBytes(0, IPC_NOWAIT)
if err != nil {
	fmt.Println(err)
}
fmt.Println(string(response))
Output:

Hello World

func NewMessageQueue

func NewMessageQueue(config *QueueConfig) (*MessageQueue, error)

NewMessageQueue returns an instance of the message queue given a QueueConfig.

func (*MessageQueue) Close

func (mq *MessageQueue) Close()

Frees up the resources associated with the message queue, but it will leave the message wueue itself in place.

func (*MessageQueue) Count

func (mq *MessageQueue) Count() (uint64, error)

Number of messages currently in the queue.

func (*MessageQueue) Destroy

func (mq *MessageQueue) Destroy() error

Get statistics about the message queue.

func (*MessageQueue) ReceiveBytes

func (mq *MessageQueue) ReceiveBytes(msgType int, flags int) ([]byte, int, error)

Receive a []byte message with the type specified by the integer argument. Pass 0 to retrieve the message at the top of the queue, regardless of type.

func (*MessageQueue) ReceiveString

func (mq *MessageQueue) ReceiveString(msgType int, flags int) (string, int, error)

Receive a string message with the type specified by the integer argument. Pass 0 to retrieve the message at the top of the queue, regardless of type.

func (*MessageQueue) SendBytes

func (mq *MessageQueue) SendBytes(message []byte, msgType int, flags int) error

Sends a []byte message to the queue of the type passed as the second argument.

func (*MessageQueue) SendString

func (mq *MessageQueue) SendString(message string, msgType int, flags int) error

Sends a string message to the queue of the type passed as the second argument.

func (*MessageQueue) Size

func (mq *MessageQueue) Size() (uint64, error)

Size of the queue in bytes.

func (*MessageQueue) Stat

func (mq *MessageQueue) Stat() (*QueueStats, error)

Get statistics about the message queue.

type QueueConfig

type QueueConfig struct {
	Mode    int // The mode of the message queue, e.g. 0600
	MaxSize int // Size of the largest message to retrieve or send, allocates a buffer of this size

	Key int // SysV IPC key

	Path   string // The path to a file to obtain a SysV IPC key if Key is not set
	ProjId int    // ProjId for ftok to generate a SysV IPC key if Key is not set
}

QueueConfig is used to configure an instance of the message queue.

type QueuePermissions

type QueuePermissions struct {
	Uid  uint32 // unsigned int32, according to bits/typesizes.h
	Gid  uint32 //
	Cuid uint32 //
	Cgid uint32 //
	Mode uint16 // unsigned short, according to msgctl(2)
}

Wraps the C structure "struct ipc_perm" (see msgctl(2))

type QueueStats

type QueueStats struct {
	Perm  QueuePermissions
	Stime int64 // signed long, according to bits/types.h
	// Rtime  int64  // https://github.com/Shopify/sysv_mq/issues/10
	Ctime  int64  //
	Cbytes uint64 // unsigned long, according to msgctl(2)
	Qnum   uint64 // unsigned long, according to bits/msq.h
	Qbytes uint64 // unsigned long, according to bits/msg.h
	Lspid  int32  // signed int32, according to bits/typesizes.h
	Lrpid  int32  //
}

Wraps the C structure "struct msgid_ds" (see msgctl(2))

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL