[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blocking receive #74

Open
luca-moser opened this issue Feb 4, 2020 · 1 comment
Open

Blocking receive #74

luca-moser opened this issue Feb 4, 2020 · 1 comment

Comments

@luca-moser
Copy link
luca-moser commented Feb 4, 2020

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
}
@Inphi
Copy link
Contributor
Inphi commented Feb 19, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants