如何正确理解ZeroMQ的ZMQ_RCVHWM选项? [英] How to understand the ZMQ_RCVHWM option of ZeroMQ correctly?

查看:3485
本文介绍了如何正确理解ZeroMQ的ZMQ_RCVHWM选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个端点,分别是经销商路由器经销商已通过TCP协议连接到路由器。我将 ZMQ_SNDHWM ZMQ_RCVHWM 设置为只有一个

There are two endpoints, which are Dealer and Router. The Dealer is connected to the Router by TCP protocol. I set ZMQ_SNDHWM and ZMQ_RCVHWM to only one for all of them.

请注意经销商总是向路由器发送消息,但路由器不做任何接收操作。

Note that the Dealer always sends message to Router, but the Router does not to do any receiving operation.

当我只是开始经销商,经销商只发出一个消息。这是预期的。
但是当我在启动路由器后,经销商可以意外地发送大约4千条消息。

When I only start the Dealer, the Dealer only send out one message.That`s expected. But when I start the Router after that, the Dealer can send out about 4 thousands message unexpectedly.

为什么 ZMQ_RCVHWM 选项似乎无效?

why the ZMQ_RCVHWM option seems to be invalid?

代理商的代码:

// create ctx
void* ctx = zmq_ctx_new();
assert(nullptr != ctx);

// create in
void* in = zmq_socket(ctx, ZMQ_DEALER);
assert(in);
int sndhwm = 1;
assert(0 == zmq_setsockopt(in, ZMQ_SNDHWM, &sndhwm, sizeof(sndhwm)));
assert(0 == zmq_setsockopt(in, ZMQ_RCVHWM, &sndhwm, sizeof(sndhwm)));

int rc = zmq_connect(in, "tcp://127.0.0.1:1012");
assert(!rc);

char content[100] = {0};
int size = 0;
int64_t nCount = 0;
while(1)
{
    sprintf_s(content, "%d", ++nCount);
    size = strlen(content);
    rc = zmq_send(in, content, size, 0);
    assert(rc = size);
    printf("in = %d\n", nCount);
}

路由器的代码:

// create ctx
void* ctx = zmq_ctx_new();
void* out = zmq_socket(ctx, ZMQ_ROUTER);
int sndhwm = 1;
assert(0 == zmq_setsockopt(out, ZMQ_SNDHWM, &sndhwm, sizeof(sndhwm)));
assert(0 == zmq_setsockopt(out, ZMQ_RCVHWM, &sndhwm, sizeof(sndhwm)));

int rc = zmq_bind(out, "tcp://127.0.0.1:1012");
assert(!rc);


推荐答案

我在这里回复,因为我知道这是较新的问题,但我来自您的这个其他问题

I reply here since I understand this is the newer question, but I was derived from this other question of yours.

你描述的行为似乎是正确的,因为(强调我,引用此处):

The behavior you describe seems to be correct, since (emphasis mine, quoted from here):


对于指定的套接字正在与通信的任何单个对等体,对未完成消息的最大数量的硬限制ØMQ<<>队列在内存中。

"The high water mark is a hard limit on the maximum number of outstanding messages ØMQ shall queue in memory for any single peer that the specified socket is communicating with".

读取邮件后,它不再在内存中排队,因此接受新邮件。这同样适用于发送的邮件,这反映在您描述的行为(在第一个问题):

Once you read the message it is no longer queued in memory, hence new messages are accepted. The same applies to sent messages, and this is reflected in the behavior you described (in the first question):



  1. 当路由器未设置时,经销商只发送一条消息,然后阻塞。没错,因为ZMQ_SNDHWM是1。




  1. 但是没有新邮件被接受或有资格发送。当我设置路由器,经销商可以继续发送约4K毫克和阻塞。为什么?


现在两个对等体已连接,并且消息流畅,因为没有进行内存排队您在接收循环中不断读取套接字,发布在您第一个问题上面我提供的链接):

Now the two peers are connected and messages flow freely since no memory queuing is done (you read the socket constantly in your receive loop, posted in your first question which link I've provided above):

路由器的代码:

//...
while(true)
{
    zmq_msg_init(&msg);
    rc = zmq_recvmsg(out, &msg, 0);
    assert(rc > 0);

    printf("out = %s\n", (char*)zmq_msg_data(&msg));
    if(!zmq_msg_more(&msg))
    {
        break;
    }
}
//...






如果你强调你的系统足够的点,入站或出站消息开始排队在内存(即超过套接字发送容量-throughput-),你会看到效果 ZMQ_SNDHWM ZMQ_RCVHWM

这篇关于如何正确理解ZeroMQ的ZMQ_RCVHWM选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆