goczmq icon indicating copy to clipboard operation
goczmq copied to clipboard

issues with setidentity

Open Irooniam opened this issue 6 years ago • 8 comments

Hello,

I think it would be really helpful to point out in the documentation that you need to set the identity of the socket before you connect.

The following will not set the correct identity dealer, err := goczmq.NewDealer("tcp://127.0.0.1:5555") dealer.SetIdentity("magic")

However this does work: dealer := goczmq.NewSock(goczmq.Dealer) dealer.SetIdentity("magic") err = dealer.Connect("tcp://127.0.0.1:5555")

Also, if you could provide example on how to setoptions via NewDealer that would be awesome. Thanks much.

Irooniam avatar Nov 30 '18 20:11 Irooniam

Thanks for filing the issue! It's always great to hear about cases that are non obvious to other people. I appreciate the feedback!

taotetek avatar Dec 07 '18 22:12 taotetek

I met the same issue, thank you @Irooniam

myzhan avatar Feb 11 '19 06:02 myzhan

This doesn't seem correct, at least not anymore perhaps. Go errors out on building this code. Whats the recommended method?

func (c *czmqSocketClient) connect() (err error) {
        addr := fmt.Sprintf("tcp://%s:%d", c.masterHost, c.masterPort)
        dealer := goczmq.NewSock(goczmq.Dealer)
        dealer.SetIdentity(c.identity)
        err = dealer.Connect(addr)
        if err != nil {
                return err
        }

        c.dealerSocket = dealer

        log.Printf("Boomer is connected to master(%s) press Ctrl+c to quit.\n", addr)

        go c.recv()
        go c.send()

        return nil
}
dealer.SetIdentity undefined (type *goczmq.Sock has no field or method SetIdentity)

gclair avatar Jul 13 '19 06:07 gclair

Figured it out for the newer version.

        sockoptions := goczmq.SockSetIdentity(c.identity)
        dealer.SetOption(sockoptions)

gclair avatar Jul 13 '19 14:07 gclair

Some socket options needing to be set before a bind or connect on a socket are things that definitely tripped me up when I first started using ZeroMQ, even before I started writing GoCZMQ. I think it's a great suggestion to make it more visible in the documentation and can do so.

@gclair note that outside of the new socket options in my latest release your example has one other problem: ZeroMQ sockets are not thread safe. You should not spawn a receive or a send via a go routine. While it may sometimes work, it is almost guaranteed to eventually cause a problem.

taotetek avatar Jul 15 '19 10:07 taotetek

You should not spawn a receive or a send via a go routine

Oops, thanks for the heads up. Because Sock.RecvFrame is a blocking call, how can I read and write in a single goroutine?

myzhan avatar Jul 15 '19 11:07 myzhan

@myzhan take a look at the poller - https://github.com/zeromq/goczmq/blob/master/poller.go https://github.com/zeromq/goczmq/blob/master/poller_test.go

taotetek avatar Jul 19 '19 14:07 taotetek

@taotetek Thanks, if I put reading and writing in a goroutine, I can select on a time.Ticker to do polling.

myzhan avatar Jul 22 '19 09:07 myzhan