zmq4 icon indicating copy to clipboard operation
zmq4 copied to clipboard

Blocking receive

Open luca-moser opened this issue 5 years ago • 1 comments

I've setup a subscriber socket with a given topic. However, it seems that sub.Recv() is non blocking and constantly returns empty messages even when the publisher isn't sending any messages. Is this behavior wanted and if so, how can I make sub.Recv() block until actually a message is available?

Here is a code snippet showing my intend: len(msg.Frames) == 0 is constantly hit

// 2 second connect timeout
ctx, _ := context.WithTimeout(context.Background(), time.Duration(2)*time.Second)
sub := zmq4.NewSub(ctx)

if err := sub.Dial(fmt.Sprintf("tcp://%s", zmqURL)); err != nil {
	collectLog.Errorf("coul not dial: %s", err)
	return err
}

if err := sub.SetOption(zmq4.OptionSubscribe, "spent_address"); err != nil {
	collectLog.Errorf("coul not dial: %s", err)
	return err
}

collectLog.Info("connected")

for {
	msg, err := sub.Recv()
	if err != nil {
		return err
	}

	if msg.Err() != nil {
		collectLog.Error(msg.Err())
		continue
	}

	if len(msg.Frames) == 0 {
		continue
	}

   // handle message
}

luca-moser avatar Feb 04 '20 20:02 luca-moser

What version are you using? I'm unable to reproduce this with using the snippet you've provided on the latest version of zmq4. In any case, you shouldn't be creating a sub socket with a timeout context if you want to block on read. When the context is done it will unblock all IO including sub.Recv.

Inphi avatar Feb 19 '20 06:02 Inphi