Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Latest commit

 

History

History
158 lines (103 loc) · 4.75 KB

.godocdown.md

File metadata and controls

158 lines (103 loc) · 4.75 KB

Go (golang) Bindings for 0mq (zmq, zeromq)

Build Status

This package implements Go (golang) bindings for the 0mq C API.

It is licensed under the Apache License, Version 2.0.

GoZMQ does not support zero-copy.

A full list of examples is included in the zguide.

Note that this is not the same as this implementation or this implementation.

Upgrading

GoZMQ has made some public changes that will break old code. Fortunately, we've also written a tool based on go fix that will upgrade your code for you! Here's how to run it over your source (after making a backup of course):

go get github.com/alecthomas/gozmq/gozmqfix
cd $YOUR_SOURCE_DIR
gozmqfix .

Installing

GoZMQ currently supports ZMQ 2.1.x, 2.2.x and basic support for 3.x. Following are instructions on how to compile against these versions.

Install gozmq with:

go get github.com/alecthomas/gozmq

This implementation works currently against:: ZeroMQ 2.2.x

ZeroMQ 2.1.x

If you're using ZeroMQ 2.1.x, install with:

go get -tags zmq_2_1 github.com/alecthomas/gozmq

ZeroMQ 3.x

There is basic support for ZeroMQ 3.x. Install with:

go get -tags zmq_3_x github.com/alecthomas/gozmq

Troubleshooting

Go can't find ZMQ

If the go tool can't find zmq and you know it is installed, you may need to override the C compiler/linker flags.

eg. If you installed zmq into /opt/zmq you might try:

CGO_CFLAGS=-I/opt/zmq/include CGO_LDFLAGS=-L/opt/zmq/lib \
	go get github.com/alecthomas/gozmq

Mismatch in version of ZMQ

If you get errors like this with 'go get' or 'go build':

1: error: 'ZMQ_FOO' undeclared (first use in this function)

There are two possibilities:

  1. Your version of zmq is very old. In this case you will need to download and build zmq yourself.
  2. You are building gozmq against the wrong version of zmq. See the installation instructions for details on how to target the correct version.

Differences from the C API

The API implemented by this package does not attempt to expose zmq_msg_t at all. Instead, Recv() and Send() both operate on byte slices, allocating and freeing the memory automatically. Currently this requires copying to/from C malloced memory, but a future implementation may be able to avoid this to a certain extent.

All major features are supported: contexts, sockets, devices, and polls.

Example

Here are direct translations of some of the examples from this blog post.

A simple echo server:

package main

import zmq "github.com/alecthomas/gozmq"

func main() {
  context, _ := zmq.NewContext()
  socket, _ := context.NewSocket(zmq.REP)
  socket.Bind("tcp://127.0.0.1:5000")
  socket.Bind("tcp://127.0.0.1:6000")

  for {
    msg, _ := socket.Recv(0)
    println("Got", string(msg))
    socket.Send(msg, 0)
  }
}

A simple client for the above server:

package main

import "fmt"
import zmq "github.com/alecthomas/gozmq"

func main() {
  context, _ := zmq.NewContext()
  socket, _ := context.NewSocket(zmq.REQ)
  socket.Connect("tcp://127.0.0.1:5000")
  socket.Connect("tcp://127.0.0.1:6000")

  for i := 0; i < 10; i++ {
    msg := fmt.Sprintf("msg %d", i)
    socket.Send([]byte(msg), 0)
    println("Sending", msg)
    socket.Recv(0)
  }
}

Caveats

Zero-copy

GoZMQ does not support zero-copy.

GoZMQ does not attempt to expose zmq_msg_t at all. Instead, Recv() and Send() both operate on byte slices, allocating and freeing the memory automatically. Currently this requires copying to/from C malloced memory, but a future implementation may be able to avoid this to a certain extent.

Memory management

It's not entirely clear from the 0mq documentation how memory for zmq_msg_t and packet data is managed once 0mq takes ownership. After digging into the source a little, this package operates under the following (educated) assumptions:

  • References to zmq_msg_t structures are not held by the C API beyond the duration of any function call.
  • Packet data is reference counted internally by the C API. The count is incremented when a packet is queued for delivery to a destination (the inference being that for delivery to N destinations, the reference count will be incremented N times) and decremented once the packet has either been delivered or errored.

{{ .EmitUsage }}

(generated from .godocdown.md with godocdown github.com/alecthomas/gozmq > README.md)