Quantcast
Channel: How to communicate between ZeroMQ contexts in different Goroutines? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to communicate between ZeroMQ contexts in different Goroutines?

$
0
0

I'm using this as boilerplate, except that in the same program I also have some goroutines that are the workers and connect to the backend endpoint, tcp://127.0.0.1:5560.

What I would like to do is have it connect through a more efficient way, such as ipc://, inproc://, or even unix sockets. I've tried those, and it didn't work. Channels is a no-go with ZeroMQ right ?

So how do I connect different goroutines with ZeroMQ contexts, without tcp ? Is there a better alternative ?

update: The code:

// Simple message queuing broker
// Same as request-reply broker but using QUEUE device
//
// Author:  Brendan Mc.
// Requires: http://github.com/alecthomas/gozmq

package main

import (
    zmq "github.com/alecthomas/gozmq"
)

func startWorker() {
    context, _ := zmq.NewContext()
    defer context.Close()

    worker, _ := context.NewSocket(zmq.REP)
    //err := worker.Connect("ipc:///backend")  // Tried it, but nothing
    //err := worker.Connect("inproc:///backend")  // Tried it, but nothing
    err := worker.Connect("tcp://127.0.0.1:5560") // this works
    if err != nil {
        fmt.Println(err)
    }

    for {
        data, err := worker.Recv(0)
        fmt.Println(string(data))
        worker.Send([]byte("I got your data"), 0)
    }
}

func main() {
    context, _ := zmq.NewContext()
    defer context.Close()

    // Socket facing clients
    frontend, _ := context.NewSocket(zmq.ROUTER)
    defer frontend.Close()
    frontend.Bind("tcp://*:5559")

    // Socket facing services
    backend, _ := context.NewSocket(zmq.DEALER)
    defer backend.Close()
    //backend.Bind("ipc:///backend")  // Tried it, but nothing
    //backend.Bind("inproc:///backend")  // Tried it, but nothing
    backend.Bind("tcp://*:5560") // this works

    for i := 0; i < 4; i++ {
        go startWorker() // Start workers in a separate goroutine
    }

    // Start built-in device
    zmq.Device(zmq.QUEUE, frontend, backend)

    // We never get here…
}

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>