Go (golang) bindings for the 0mq (zmq, zeromq) C API

Related tags

Messaging gozmq
Overview

NOTE: These gozmq bindings are in maintenance mode. Only critical bugs will be fixed. Henceforth I would suggest using @pebbe's actively maintained bindings for zmq2, zmq3 and zmq4.

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

Build Status

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

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, 3.x and 4.x. Following are instructions on how to compile against these versions.

For ZeroMQ 2.2.x install with:

go get github.com/alecthomas/gozmq

For 2.1.x install with:

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

For 3.x install with:

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

For 4.x install with:

go get -tags zmq_4_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.

Usage

const (
  // NewSocket types
  PAIR   = SocketType(C.ZMQ_PAIR)
  PUB    = SocketType(C.ZMQ_PUB)
  SUB    = SocketType(C.ZMQ_SUB)
  REQ    = SocketType(C.ZMQ_REQ)
  REP    = SocketType(C.ZMQ_REP)
  DEALER = SocketType(C.ZMQ_DEALER)
  ROUTER = SocketType(C.ZMQ_ROUTER)
  PULL   = SocketType(C.ZMQ_PULL)
  PUSH   = SocketType(C.ZMQ_PUSH)
  XPUB   = SocketType(C.ZMQ_XPUB)
  XSUB   = SocketType(C.ZMQ_XSUB)

  // Deprecated aliases
  XREQ       = DEALER
  XREP       = ROUTER
  UPSTREAM   = PULL
  DOWNSTREAM = PUSH

  // NewSocket options
  AFFINITY          = UInt64SocketOption(C.ZMQ_AFFINITY)
  IDENTITY          = StringSocketOption(C.ZMQ_IDENTITY)
  SUBSCRIBE         = StringSocketOption(C.ZMQ_SUBSCRIBE)
  UNSUBSCRIBE       = StringSocketOption(C.ZMQ_UNSUBSCRIBE)
  RATE              = Int64SocketOption(C.ZMQ_RATE)
  RECOVERY_IVL      = Int64SocketOption(C.ZMQ_RECOVERY_IVL)
  SNDBUF            = UInt64SocketOption(C.ZMQ_SNDBUF)
  RCVBUF            = UInt64SocketOption(C.ZMQ_RCVBUF)
  FD                = Int64SocketOption(C.ZMQ_FD)
  EVENTS            = UInt64SocketOption(C.ZMQ_EVENTS)
  TYPE              = UInt64SocketOption(C.ZMQ_TYPE)
  LINGER            = IntSocketOption(C.ZMQ_LINGER)
  RECONNECT_IVL     = IntSocketOption(C.ZMQ_RECONNECT_IVL)
  RECONNECT_IVL_MAX = IntSocketOption(C.ZMQ_RECONNECT_IVL_MAX)
  BACKLOG           = IntSocketOption(C.ZMQ_BACKLOG)

  // Send/recv options
  SNDMORE = SendRecvOption(C.ZMQ_SNDMORE)
)
const (
  POLLIN  = PollEvents(C.ZMQ_POLLIN)
  POLLOUT = PollEvents(C.ZMQ_POLLOUT)
  POLLERR = PollEvents(C.ZMQ_POLLERR)
)
const (
  STREAMER  = DeviceType(C.ZMQ_STREAMER)
  FORWARDER = DeviceType(C.ZMQ_FORWARDER)
  QUEUE     = DeviceType(C.ZMQ_QUEUE)
)
const (
  RCVTIMEO = IntSocketOption(C.ZMQ_RCVTIMEO)
  SNDTIMEO = IntSocketOption(C.ZMQ_SNDTIMEO)
)
const (
  RCVMORE           = UInt64SocketOption(C.ZMQ_RCVMORE)
  RECOVERY_IVL_MSEC = Int64SocketOption(C.ZMQ_RECOVERY_IVL_MSEC)
  SWAP              = Int64SocketOption(C.ZMQ_SWAP)
  MCAST_LOOP        = Int64SocketOption(C.ZMQ_MCAST_LOOP)
  HWM               = UInt64SocketOption(C.ZMQ_HWM)
  NOBLOCK           = SendRecvOption(C.ZMQ_NOBLOCK)

  // Forwards-compatible aliases:
  DONTWAIT = NOBLOCK
)
const (
  RCVMORE = IntSocketOption(C.ZMQ_RCVMORE)
  SNDHWM  = IntSocketOption(C.ZMQ_SNDHWM)
  RCVHWM  = IntSocketOption(C.ZMQ_RCVHWM)

  // TODO Not documented in the man page...
  //LAST_ENDPOINT       = UInt64SocketOption(C.ZMQ_LAST_ENDPOINT)
  FAIL_UNROUTABLE     = BoolSocketOption(C.ZMQ_FAIL_UNROUTABLE)
  TCP_KEEPALIVE       = IntSocketOption(C.ZMQ_TCP_KEEPALIVE)
  TCP_KEEPALIVE_CNT   = IntSocketOption(C.ZMQ_TCP_KEEPALIVE_CNT)
  TCP_KEEPALIVE_IDLE  = IntSocketOption(C.ZMQ_TCP_KEEPALIVE_IDLE)
  TCP_KEEPALIVE_INTVL = IntSocketOption(C.ZMQ_TCP_KEEPALIVE_INTVL)
  TCP_ACCEPT_FILTER   = StringSocketOption(C.ZMQ_TCP_ACCEPT_FILTER)

  // Message options
  MORE = MessageOption(C.ZMQ_MORE)

  // Send/recv options
  DONTWAIT = SendRecvOption(C.ZMQ_DONTWAIT)

  // Deprecated aliases
  NOBLOCK = DONTWAIT
)
var (
  // Additional ZMQ errors
  ENOTSOCK       error = zmqErrno(C.ENOTSOCK)
  EFSM           error = zmqErrno(C.EFSM)
  EINVAL         error = zmqErrno(C.EINVAL)
  ENOCOMPATPROTO error = zmqErrno(C.ENOCOMPATPROTO)
  ETERM          error = zmqErrno(C.ETERM)
  EMTHREAD       error = zmqErrno(C.EMTHREAD)
)

func Device

func Device(t DeviceType, in, out *Socket) error

run a zmq_device passing messages between in and out

func Poll

func Poll(items []PollItem, timeout time.Duration) (count int, err error)

Poll ZmqSockets and file descriptors for I/O readiness. Timeout is in time.Duration. The smallest possible timeout is time.Millisecond for ZeroMQ version 3 and above, and time.Microsecond for earlier versions.

func Proxy

func Proxy(in, out, capture *Socket) error

run a zmq_proxy with in, out and capture sockets

func Version

func Version() (int, int, int)

void zmq_version (int *major, int *minor, int *patch);

type BoolSocketOption

type BoolSocketOption int

type Context

type Context struct {
}
  • A context handles socket creation and asynchronous message delivery. * There should generally be one context per application.

func NewContext

func NewContext() (*Context, error)

Create a new context.

func (*Context) Close

func (c *Context) Close()

func (*Context) IOThreads

func (c *Context) IOThreads() (int, error)

Get a context option.

func (*Context) MaxSockets

func (c *Context) MaxSockets() (int, error)

func (*Context) NewSocket

func (c *Context) NewSocket(t SocketType) (*Socket, error)

Create a new socket. void *zmq_socket (void *context, int type);

func (*Context) SetIOThreads

func (c *Context) SetIOThreads(value int) error

Set a context option.

func (*Context) SetMaxSockets

func (c *Context) SetMaxSockets(value int) error

type DeviceType

type DeviceType int

type Int64SocketOption

type Int64SocketOption int

type IntSocketOption

type IntSocketOption int

type MessageOption

type MessageOption int

type PollEvents

type PollEvents C.short

type PollItem

type PollItem struct {
  Socket  *Socket         // socket to poll for events on
  Fd      ZmqOsSocketType // fd to poll for events on as returned from os.File.Fd()
  Events  PollEvents      // event set to poll for
  REvents PollEvents      // events that were present
}

Item to poll for read/write events on, either a *Socket or a file descriptor

type PollItems

type PollItems []PollItem

a set of items to poll for events on

type SendRecvOption

type SendRecvOption int

type Socket

type Socket struct {
}

func (*Socket) Affinity

func (s *Socket) Affinity() (uint64, error)

ZMQ_AFFINITY: Retrieve I/O thread affinity.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc7

func (*Socket) Backlog

func (s *Socket) Backlog() (int, error)

ZMQ_BACKLOG: Retrieve maximum length of the queue of outstanding connections.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc18

func (*Socket) Bind

func (s *Socket) Bind(address string) error

Bind the socket to a listening address. int zmq_bind (void *s, const char *addr);

func (*Socket) Close

func (s *Socket) Close() error

Shutdown the socket. int zmq_close (void *s);

func (*Socket) Connect

func (s *Socket) Connect(address string) error

Connect the socket to an address. int zmq_connect (void *s, const char *addr);

func (*Socket) Events

func (s *Socket) Events() (uint64, error)

ZMQ_EVENTS: Retrieve socket event state.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc20

func (*Socket) GetSockOptBool

func (s *Socket) GetSockOptBool(option BoolSocketOption) (value bool, err error)

func (*Socket) GetSockOptInt

func (s *Socket) GetSockOptInt(option IntSocketOption) (value int, err error)

Get an int option from the socket. int zmq_getsockopt (void *s, int option, void *optval, size_t *optvallen);

func (*Socket) GetSockOptInt64

func (s *Socket) GetSockOptInt64(option Int64SocketOption) (value int64, err error)

Get an int64 option from the socket. int zmq_getsockopt (void *s, int option, void *optval, size_t *optvallen);

func (*Socket) GetSockOptString

func (s *Socket) GetSockOptString(option StringSocketOption) (value string, err error)

Get a string option from the socket. int zmq_getsockopt (void *s, int option, void *optval, size_t *optvallen);

func (*Socket) GetSockOptUInt64

func (s *Socket) GetSockOptUInt64(option UInt64SocketOption) (value uint64, err error)

Get a uint64 option from the socket. int zmq_getsockopt (void *s, int option, void *optval, size_t *optvallen);

func (*Socket) HWM

func (s *Socket) HWM() (uint64, error)

ZMQ_HWM: Retrieve high water mark.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc5

func (*Socket) Identity

func (s *Socket) Identity() (string, error)

ZMQ_IDENTITY: Retrieve socket identity.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc8

func (*Socket) Linger

func (s *Socket) Linger() (time.Duration, error)

ZMQ_LINGER: Retrieve linger period for socket shutdown.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc15

func (*Socket) McastLoop

func (s *Socket) McastLoop() (bool, error)

ZMQ_MCAST_LOOP: Control multicast loop-back.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc12

func (*Socket) Rate

func (s *Socket) Rate() (int64, error)

ZMQ_RATE: Retrieve multicast data rate.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc9

func (*Socket) RcvBuf

func (s *Socket) RcvBuf() (uint64, error)

ZMQ_RCVBUF: Retrieve kernel receive buffer size.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc14

func (*Socket) RcvHWM

func (s *Socket) RcvHWM() (int, error)

ZMQ_RCVHWM: Retrieve high water mark for inbound messages.

See: http://api.zeromq.org/3.2:zmq-getsockopt#toc6

func (*Socket) RcvMore

func (s *Socket) RcvMore() (bool, error)

ZMQ_RCVMORE: More message parts to follow.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc4

func (*Socket) RcvTimeout

func (s *Socket) RcvTimeout() (time.Duration, error)

ZMQ_RCVTIMEO: Maximum time before a socket operation returns with EAGAIN.

See: http://api.zeromq.org/2.2:zmq-getsockopt#toc6

func (*Socket) ReconnectIvl

func (s *Socket) ReconnectIvl() (time.Duration, error)

ZMQ_RECONNECT_IVL: Retrieve reconnection interval.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc16

func (*Socket) ReconnectIvlMax

func (s *Socket) ReconnectIvlMax() (time.Duration, error)

ZMQ_RECONNECT_IVL_MAX: Retrieve maximum reconnection interval.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc17

func (*Socket) RecoveryIvl

func (s *Socket) RecoveryIvl() (time.Duration, error)

ZMQ_RECOVERY_IVL_MSEC: Get multicast recovery interval in milliseconds.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc11

func (*Socket) Recv

func (s *Socket) Recv(flags SendRecvOption) (data []byte, err error)

Receive a message from the socket. int zmq_recv (void *s, zmq_msg_t *msg, int flags);

func (*Socket) RecvMultipart

func (s *Socket) RecvMultipart(flags SendRecvOption) (parts [][]byte, err error)

Receive a multipart message.

func (*Socket) Send

func (s *Socket) Send(data []byte, flags SendRecvOption) error

Send a message to the socket. int zmq_send (void *s, zmq_msg_t *msg, int flags);

func (*Socket) SendMultipart

func (s *Socket) SendMultipart(parts [][]byte, flags SendRecvOption) (err error)

Send a multipart message.

func (*Socket) SetAffinity

func (s *Socket) SetAffinity(value uint64) error

ZMQ_AFFINITY: Set I/O thread affinity.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc5

func (*Socket) SetBacklog

func (s *Socket) SetBacklog(value int) error

ZMQ_BACKLOG: Set maximum length of the queue of outstanding connections.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc18

func (*Socket) SetHWM

func (s *Socket) SetHWM(value uint64) error

ZMQ_HWM: Set high water mark.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc3

func (*Socket) SetIdentity

func (s *Socket) SetIdentity(value string) error

ZMQ_IDENTITY: Set socket identity.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc6

func (*Socket) SetLinger

func (s *Socket) SetLinger(value time.Duration) error

ZMQ_LINGER: Set linger period for socket shutdown.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc15

func (*Socket) SetMcastLoop

func (s *Socket) SetMcastLoop(value bool) error

ZMQ_MCAST_LOOP: Control multicast loop-back.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc12

func (*Socket) SetRate

func (s *Socket) SetRate(value int64) error

ZMQ_RATE: Set multicast data rate.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc9

func (*Socket) SetRcvBuf

func (s *Socket) SetRcvBuf(value uint64) error

ZMQ_RCVBUF: Set kernel receive buffer size.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc14

func (*Socket) SetRcvHWM

func (s *Socket) SetRcvHWM(value int) error

ZMQ_RCVHWM: Set high water mark for inbound messages.

See: http://api.zeromq.org/3.2:zmq-setsockopt#toc4

func (*Socket) SetRcvTimeout

func (s *Socket) SetRcvTimeout(value time.Duration) error

ZMQ_RCVTIMEO: Maximum time before a recv operation returns with EAGAIN.

See: http://api.zeromq.org/2.2:zmq-setsockopt#toc9

func (*Socket) SetReconnectIvl

func (s *Socket) SetReconnectIvl(value time.Duration) error

ZMQ_RECONNECT_IVL: Set reconnection interval.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc16

func (*Socket) SetReconnectIvlMax

func (s *Socket) SetReconnectIvlMax(value time.Duration) error

ZMQ_RECONNECT_IVL_MAX: Set maximum reconnection interval.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc17

func (*Socket) SetRecoveryIvl

func (s *Socket) SetRecoveryIvl(value time.Duration) error

ZMQ_RECOVERY_IVL_MSEC: Set multicast recovery interval in milliseconds.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc11

func (*Socket) SetSndBuf

func (s *Socket) SetSndBuf(value uint64) error

ZMQ_SNDBUF: Set kernel transmit buffer size.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc13

func (*Socket) SetSndHWM

func (s *Socket) SetSndHWM(value int) error

ZMQ_SNDHWM: Set high water mark for outbound messages.

See: http://api.zeromq.org/3.2:zmq-setsockopt#toc3

func (*Socket) SetSndTimeout

func (s *Socket) SetSndTimeout(value time.Duration) error

ZMQ_SNDTIMEO: Maximum time before a send operation returns with EAGAIN.

See: http://api.zeromq.org/2.2:zmq-setsockopt#toc10

func (*Socket) SetSockOptInt

func (s *Socket) SetSockOptInt(option IntSocketOption, value int) error

Set an int option on the socket. int zmq_setsockopt (void *s, int option, const void *optval, size_t optvallen);

func (*Socket) SetSockOptInt64

func (s *Socket) SetSockOptInt64(option Int64SocketOption, value int64) error

Set an int64 option on the socket. int zmq_setsockopt (void *s, int option, const void *optval, size_t optvallen);

func (*Socket) SetSockOptString

func (s *Socket) SetSockOptString(option StringSocketOption, value string) error

Set a string option on the socket. int zmq_setsockopt (void *s, int option, const void *optval, size_t optvallen);

func (*Socket) SetSockOptStringNil

func (s *Socket) SetSockOptStringNil(option StringSocketOption) error

Set a string option on the socket to nil. int zmq_setsockopt (void *s, int option, const void *optval, size_t optvallen);

func (*Socket) SetSockOptUInt64

func (s *Socket) SetSockOptUInt64(option UInt64SocketOption, value uint64) error

Set a uint64 option on the socket. int zmq_setsockopt (void *s, int option, const void *optval, size_t optvallen);

func (*Socket) SetSubscribe

func (s *Socket) SetSubscribe(value string) error

ZMQ_SUBSCRIBE: Establish message filter.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc7

func (*Socket) SetSwap

func (s *Socket) SetSwap(value int64) error

ZMQ_SWAP: Set disk offload size.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc4

func (*Socket) SetTCPKeepalive

func (s *Socket) SetTCPKeepalive(value int) error

ZMQ_TCP_KEEPALIVE: Override SO_KEEPALIVE socket option.

See: http://api.zeromq.org/3.2:zmq-setsockopt#toc25

func (*Socket) SetTCPKeepaliveCnt

func (s *Socket) SetTCPKeepaliveCnt(value int) error

ZMQ_TCP_KEEPALIVE_CNT: Override TCP_KEEPCNT socket option.

See: http://api.zeromq.org/3.2:zmq-setsockopt#toc27

func (*Socket) SetTCPKeepaliveIdle

func (s *Socket) SetTCPKeepaliveIdle(value int) error

ZMQ_TCP_KEEPALIVE_IDLE: Override TCP_KEEPCNT(or TCP_KEEPALIVE on some OS).

See: http://api.zeromq.org/3.2:zmq-setsockopt#toc26

func (*Socket) SetTCPKeepaliveIntvl

func (s *Socket) SetTCPKeepaliveIntvl(value int) error

ZMQ_TCP_KEEPALIVE_INTVL: Override TCP_KEEPINTVL socket option.

See: http://api.zeromq.org/3.2:zmq-setsockopt#toc28

func (*Socket) SetUnsubscribe

func (s *Socket) SetUnsubscribe(value string) error

ZMQ_UNSUBSCRIBE: Remove message filter.

See: http://api.zeromq.org/2.1:zmq-setsockopt#toc8

func (*Socket) SndBuf

func (s *Socket) SndBuf() (uint64, error)

ZMQ_SNDBUF: Retrieve kernel transmit buffer size.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc13

func (*Socket) SndHWM

func (s *Socket) SndHWM() (int, error)

ZMQ_SNDHWM: Retrieves high water mark for outbound messages.

See: http://api.zeromq.org/3.2:zmq-getsockopt#toc5

func (*Socket) SndTimeout

func (s *Socket) SndTimeout() (time.Duration, error)

ZMQ_SNDTIMEO: Maximum time before a socket operation returns with EAGAIN.

See: http://api.zeromq.org/2.2:zmq-getsockopt#toc7

func (*Socket) Swap

func (s *Socket) Swap() (int64, error)

ZMQ_SWAP: Retrieve disk offload size.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc6

func (*Socket) TCPKeepalive

func (s *Socket) TCPKeepalive() (int, error)

ZMQ_TCP_KEEPALIVE: Override SO_KEEPALIVE socket option.

See: http://api.zeromq.org/3.2:zmq-getsockopt#toc26

func (*Socket) TCPKeepaliveCnt

func (s *Socket) TCPKeepaliveCnt() (int, error)

ZMQ_TCP_KEEPALIVE_CNT: Override TCP_KEEPCNT socket option.

See: http://api.zeromq.org/3.2:zmq-getsockopt#toc28

func (*Socket) TCPKeepaliveIdle

func (s *Socket) TCPKeepaliveIdle() (int, error)

ZMQ_TCP_KEEPALIVE_IDLE: Override TCP_KEEPCNT(or TCP_KEEPALIVE on some OS).

See: http://api.zeromq.org/3.2:zmq-getsockopt#toc27

func (*Socket) TCPKeepaliveIntvl

func (s *Socket) TCPKeepaliveIntvl() (int, error)

ZMQ_TCP_KEEPALIVE_INTVL: Override TCP_KEEPINTVL socket option.

See: http://api.zeromq.org/3.2:zmq-getsockopt#toc29

func (*Socket) Type

func (s *Socket) Type() (SocketType, error)

ZMQ_TYPE: Retrieve socket type.

See: http://api.zeromq.org/2.1:zmq-getsockopt#toc3

type SocketType

type SocketType int

type StringSocketOption

type StringSocketOption int

type UInt64SocketOption

type UInt64SocketOption int

type ZmqOsSocketType

type ZmqOsSocketType C.SOCKET

func (ZmqOsSocketType) ToRaw

func (self ZmqOsSocketType) ToRaw() C.SOCKET

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

Comments
  • Making gozmq compile for 0MQ 3.2

    Making gozmq compile for 0MQ 3.2

    • zmq_init -> zmq_ctx_new
    • zmq_term -> zmq_ctx_destroy
    • ZMQ_FAIL_UNROUTABLE -> ZMQ_ROUTER_MANDATORY
    • zmq_recvmsg is deprecated in favour of zmq_msg_recv
    • zmq_sendmsg is deprecated in favour of zmq_msg_send
    • Added constants from zmq.h.
      • Turned var into const where possible.
      • Added missing constants from zmq.h.
    • Added some deprecated aliases.
    opened by ajanicij 19
  • Structs to Interfaces

    Structs to Interfaces

    This adds a command "gozmqfix" that will update code when there are backwards incompatible changes in the API of the "github.com/alecthomas/gozmq" package.

    The code is copied from the Go project at version 1.0.3 with its licensing intact. Based on a little research into licensing, I've added a copyright line near the top of the LICENSE file. I am not a lawyer.

    Install with go install github.com/alecthomas/gozmq/gozmqfix. Usage is essentially the same as for go fix.

    Finally, change interfaces to structs!

    This started with some global search and replace, then cleaned up as necessary to get things building and passing tests again. Updating existing code should be as simple as:

    go install github.com/alecthomas/gozmq/gozmqfix
    gozmqfix PATH...
    

    I've tested this on the files in imatix/zguide/examples/Go and it works fine (though a couple examples are missing the time.Duration update at the moment).

    opened by jtacoma 12
  • ZMQ_FAIL_UNROUTABLE was renamed in v3.2.1-RC2

    ZMQ_FAIL_UNROUTABLE was renamed in v3.2.1-RC2

    v3.2.1-RC2 was released yesterday. 

    ZMQ_FAIL_UNROUTABLE got renamed (twice) to ZMQ_ROUTER_MANDATORY.

    without this, build fails:

    # github.com/alecthomas/gozmq
    1: error: 'ZMQ_FAIL_UNROUTABLE' undeclared (first use in this function)
    1: note: each undeclared identifier is reported only once for each function it appears in
    

    ref: https://github.com/zeromq/libzmq/pull/383 https://github.com/zeromq/libzmq/pull/436

    opened by srid 9
  • Getters and setters

    Getters and setters

    In the interest of convenience, I've added getter/setter methods that wrap the type-specific {Get,Set}SockOpt* methods.

    Following the conventions in Effective Go the setters are named with a 'Set' prefix (ex. SetLinger) and the getters are just plain (ex. Linger).

    opened by slyphon 8
  • Context Options

    Context Options

    Since libzmq 2.x only lets iothreads be specified as an argument to the context constructor while libzmq 3.x supports a general purpose API for setting context options, I took a tip from czmq and delayed actual context construction until it's required and followed the pattern already used in gozmq for socket options to support both 2.x and 3.x. The result passes tests on both 2.2.0 and 3.2.2.

    opened by jtacoma 7
  • Merging versioned files into one

    Merging versioned files into one

    Merging versioned files into one, so go get will work for all versions of ZeroMQ without use of -tags, and gozmq will function identical for all versions of ZeroMQ.

    The following stuff is (temporarily) removed:

    Zmq 2.1 & 2.2:

        RECOVERY_IVL_MSEC = Int64SocketOption(C.ZMQ_RECOVERY_IVL_MSEC)
        SWAP              = Int64SocketOption(C.ZMQ_SWAP)
        MCAST_LOOP        = Int64SocketOption(C.ZMQ_MCAST_LOOP)
        HWM               = UInt64SocketOption(C.ZMQ_HWM)
    

    Zmq 2.2:

        RCVTIMEO = IntSocketOption(C.ZMQ_RCVTIMEO)
        SNDTIMEO = IntSocketOption(C.ZMQ_SNDTIMEO)
    

    Zmq 3.x:

        SNDHWM = IntSocketOption(C.ZMQ_SNDHWM)
        RCVHWM = IntSocketOption(C.ZMQ_SNDHWM)
    
        // TODO Not documented in the man page...
        //LAST_ENDPOINT       = UInt64SocketOption(C.ZMQ_LAST_ENDPOINT)
        FAIL_UNROUTABLE     = BoolSocketOption(C.ZMQ_FAIL_UNROUTABLE)
        TCP_KEEPALIVE       = IntSocketOption(C.ZMQ_TCP_KEEPALIVE)
        TCP_KEEPALIVE_CNT   = IntSocketOption(C.ZMQ_TCP_KEEPALIVE_CNT)
        TCP_KEEPALIVE_IDLE  = IntSocketOption(C.ZMQ_TCP_KEEPALIVE_IDLE)
        TCP_KEEPALIVE_INTVL = IntSocketOption(C.ZMQ_TCP_KEEPALIVE_INTVL)
        // TODO Make this work.
        //TCP_ACCEPT_FILTER   = IntSocketOption(C.ZMQ_TCP_ACCEPT_FILTER)
    
        // Message options
        MORE = MessageOption(C.ZMQ_MORE)
    
    opened by pebbe 6
  • Use pkg-config; fix Poll() for ZeroMQ version 3

    Use pkg-config; fix Poll() for ZeroMQ version 3

    Two commits:

    Use pkg-config, instead of assuming where libs and headers are.

    Change the timeout in Poll() from uint64 to time.Duration. Make it work for both ZeroMQ versions 2 and 3. The unit for timeout changed between these two versions from microsecond to millisecond.

    opened by pebbe 5
  • Socket option getters

    Socket option getters

    This is a response to @alecthomas' comment on #15, "I haven't merged this mainly because I haven't updated it for the new tagged version builds." I'm not sure this code is ready for the master branch yet, but both go test (with libzmq-2.2.0) and go test -tags zmq_3_x (with libzmq-3.2.2) build and pass.

    opened by jtacoma 5
  • More options

    More options

    This adds some easily supported 3.x options: DelayAttachOnConnect, IPv4Only, MaxMsgSize, ROUTERMandatory, TCPAcceptFilter, and XPUBVerbose. Some were already supported but didn't have their own friendly methods yet. I made a separate commit for each option, for easier review.

    Tested on my machine with Go 1.0.3 and libzmq 2.2.0 & 3.2.2.

    Does anybody find the casing of these a bit awkward? Maybe gozmqgen is difficult for others to modify and needs more inline documentation?

    opened by jtacoma 4
  • Use C.GoBytes instead of C.memcpy

    Use C.GoBytes instead of C.memcpy

    I noticed that there was memcpy used a few times where C.GoBytes was more appropriate.

    Also remoted a few unnecessary unsafe.Pointer casts.

    Signed-off-by: Ondrej Kupka [email protected]

    opened by tchap 4
  • Add basic support for zmq_socket_monitor

    Add basic support for zmq_socket_monitor

    Hi,

    Following the small discussion with @jtacoma at #75, I was thinking that we could start implementing zmq_socket_monitor, or at least discuss how to do it.

    The API call itself is pretty simple and is already present in the pull request (no tests for now). Now the question is how to work with this whole monitor thing further. I discovered that in fact there are 2 distinct version of the zmq_event_t (i.e. 2 versions of the monitor API). The old (deprecated as of now I think) version is still in the zeromq3-x repository while the new one is in libzmq itself. Now the question is what we want to support. Can we drop zeromq3-x?

    Anyway, I think that the new API would be much easier to support, because then you can use gozmq Recv to receive events, which is not that simple with the old API since you are passing C structs around. So I guess that would require a special call for receiving events in that case...

    What do you think?

    opened by tchap 4
  • Add simple zero copy support

    Add simple zero copy support

    The simplest thing I managed to come up with is to simply remember the data still being used in a map in the Go part so that it's not GC'd. In the callback it's then enough to just delete the record in the map and we are done. Unfortunately there is some synchronization needed, creating a possible bottleneck, but who knows, maybe not. And it's locking in the callback, so I don't know what scenario would really break this down. It should be fine and enough.

    As an improvement it could be possible to simply pass the key as the integer, i.e. don't malloc space for the integer and pass the pointer, but simply cast the integer to pointer and pass that one...

    I also don't like the global state in the Go module, I will think about it a bit more, if it is possible to get rid of that. I don't think that you can possibly manage to fill the map with ID's even if you use several contexts, but it's just ugly...

    Btw tests and other stuff left out for now, we should first get this into a usable state and then take care of the rest I think.

    In fact I haven't even tried to run it, just to compile it :P

    Signed-off-by: Ondrej Kupka [email protected]

    opened by tchap 12
  • Initial support for zmq_msg_t

    Initial support for zmq_msg_t

    As mentioned in #78, current semantics of Recv are not sufficient for centain scenarios, e.g. you want to receive a pointer over PAIR and then copy the data, exactly as zmq_socket_monitor is supposed to be used. The issues is that every call to Recv automatically closes the zmq_msg_t it uses, which is not desirable here.

    This tiny initial implementation decouples Recv from zmq_msg_close call, making it possible to actually use zmq_socket_monitor and such.

    Fixes #78.

    Signed-off-by: Ondrej Kupka [email protected]

    Btw tests are intentionally left out since this can be theoretically rejected :-) I will add them later if it's the other way around...

    opened by tchap 3
Owner
Alec Thomas
Alec Thomas
A Go interface to ZeroMQ version 2

A Go interface to ZeroMQ version 2. Requires ZeroMQ version 2.1 or 2.2 For ZeroMQ version 4, see: http://github.com/pebbe/zmq4 For ZeroMQ version 3, s

Peter Kleiweg 18 May 26, 2021
A Go interface to ZeroMQ version 3

A Go interface to ZeroMQ version 3. For ZeroMQ version 4, see: http://github.com/pebbe/zmq4 For ZeroMQ version 2, see: http://github.com/pebbe/zmq2 In

Peter Kleiweg 133 Sep 24, 2022
Native Go bindings for D-Bus

dbus dbus is a simple library that implements native Go client bindings for the D-Bus message bus system. Features Complete native implementation of t

null 795 Dec 7, 2022
Package htmltopdf implements wkhtmltopdf Go bindings.

htmltopdf Package htmltopdf implements wkhtmltopdf Go bindings. It can be used to convert HTML documents to PDF files. The package does not use the wk

DigitalData s.r.o. 3 Sep 19, 2022
Golang API wrapper for MangaDex v5's MVP API.

mangodex Golang API wrapper for MangaDex v5's MVP API. Full documentation is found here. This API is still in Open Beta, so testing may not be complet

null 35 Oct 23, 2022
Mizu - API traffic viewer for Kubernetes enabling you to view all API communication between microservices

The API Traffic Viewer for Kubernetes A simple-yet-powerful API traffic viewer f

UP9 6.4k Dec 8, 2022
Golang AMQP wrapper for RabbitMQ with better API

go-rabbitmq Golang AMQP wrapper for RabbitMQ with better API Table of Contents Background Features Usage Installation Connect to RabbitMQ Declare Queu

Hadi Hidayat Hammurabi 10 Nov 22, 2022
API for sending sms through the connected modem using golang

SMS sender API API for making SMS sending from modem, by sending post request to route Request for Send SMS url method 127.0.0.1:8000/api/v1/send-mode

Muhammetberdi Jepbarov 0 Nov 5, 2021
Golang Restful API Messaging using GORM ORM (MySQL) Gorilla Mux

Golang Restful API Messaging using GORM ORM (MySQL) Gorilla Mux Getting Started Folder Structure This is my folder structure under my $GOPATH or $HOME

Septian Ramadhan 0 Dec 14, 2021
It's client library written in Golang for interacting with Linkedin Cruise Control using its HTTP API.

go-cruise-control It's client library (written in Golang) for interacting with Linkedin Cruise Control using its HTTP API. Supported Cruise Control ve

Banzai Cloud 1 Jan 10, 2022
A non-dependent, online configuration, GO-developed, API gateway

GateKeeper GateKeeper 是一个 Go 编写的不依赖分布式数据库的 API 网关,使用它可以高效进行服务代理,支持在线化热更新服务配置 以及 纯文件方式服务配置,支持主动探测方式自动剔除故障节点以及手动方式关闭下游节点流量,还可以通过自定义中间件方式灵活拓展其他功能。 特性 内容

DiDi 404 Nov 21, 2022
🔥 A fast and beautiful command line tool to build API requests.

Poodle A fast and beautiful command line tool to build API requests ?? Check out the full Demo! Poodle is an interactive command line tool to build an

Ahmed 135 Aug 23, 2022
Insta API using Go and Mongodb

Insta-API HTTP JSON API that can be used to perform basic tasks of Creating User, Creating Post, Getting User & Post details through their Unique ID.

Anmol Singh Yadav 2 May 1, 2022
App for test hypothesis about API for rabbitmq

REST API для работы с RabbitMQ Приложение для работы с брокером сообщений RabbitMQ через REST API. Основная мысль - что одиночные сообщения отправлять

Konstantin 0 Nov 12, 2021
Basic kick the tires on NATS Key-Value API (Go)

nats-kv-101 Basic kick the tires on NATS Key-Value API (Go) Usage # Get ./mybucket -s "nats://vbox1.tinghus.net" -creds "/home/todd/lab/nats-cluster1/

Todd Beets 1 Feb 15, 2022
This API functionality for creating and retrieving users from the IDT backend

IDT Messaging Api This API functionality for creating and retrieving users from the IDT backend. The Go-Kit library which provides industry standard b

nsubugak 0 Dec 6, 2021
REST API сервиса для просмотра видео роликов.

Go PostgreSQL О проекте Этот репозиторий представляет собой реализацию REST API сервиса для просмотра видео роликов. Используемы технологии Go Postgre

Sergey 0 Dec 18, 2021
Fetch gas stations prices from Tankerkönig api with Orion Context Broker persistence

tankerkoenig-fuel-machinery - Fetch gas stations from tankerkoenig api and persist them into an Orion Context Broker Scope This project aims to fetch

phoops s.r.l. 3 Feb 14, 2022
graylog-golang is a full implementation for sending messages in GELF (Graylog Extended Log Format) from Go (Golang) to Graylog

graylog-golang is a full implementation for sending messages in GELF (Graylog Extended Log Format) from Go (Golang) to Graylog

Robert Kowalski 79 Aug 12, 2022