Use zeromq with caution

Source: Internet
Author: User

1. Don't talk about zeromq. You can go to the official guide for details.

2. note: 1) when using the C ++ interface, you must set zmq in your own class or in the Apache module :: the context_t object is defined before the zmq: socket_t object. This ensures the order of destruction.
2) When Sub-pub is used, if sub does not call setsockopt to set a filter item (null is used to accept all), no messages will be received, and all messages will be blocked by default.
3) if it is used by the backend service cluster and zmq requires bind port, it is best to filter it in iptables instead of Connecting External links; otherwise, the CPU will be idling; CPU idling will be discussed at the end
4) when the client connects to the server, zmq in each client uses one socket for each connection. Although one socket can call connect multiple times, that is, connecting multiple servers. But remember, it is best to use a socket to call connect only once.
5) use the zmq program. After running for a period of time (may be a few days), if you encounter an exception and quit, and you use the lognohup running by nohup. if zeromq connection timeout is displayed in the out file, you can check row 203 in the zeromq/src/tcp_socket.cpp file as an error occurred when writing.
int zmq::tcp_socket_t::write (const void *data, int size){    ssize_t nbytes = send (s, data, size, 0);    //  Several errors are OK. When speculative write is being done we may not    //  be able to write a single byte to the socket. Also, SIGSTOP issued    //  by a debugging tool can result in EINTR error.    if (nbytes == -1 && (errno == EAGAIN || errno == EWOULDBLOCK ||          errno == EINTR))        return 0;    //  Signalise peer failure.    if (nbytes == -1 && (errno == ECONNRESET || errno == EPIPE))        return -1;    errno_assert (nbytes != -1);    return (size_t) nbytes;}
This is because errno_assert (nbytes! =-1); but the real cause is that when nbytes =-1, errno = etimedout does not add the timeout judgment in the previous line, in addition, you can refer to the process on win in this file. Therefore, if you encounter a timeout error
= Add etimeout to the end of errno = epipe and re-compile
6) CPU idling. First, you can use the top program name to check whether the CPU usage of your program is 100%, which is basically fixed. At this time, you need to doubt whether your program is faulty, then, use the process Number Of The lsof-P program to check whether there is a file descriptor with can't.
Identify protocol. Check whether an Internet IP address is connected to the listening port of your zmq instance. Then, you can go to zeromq/src/epoll. log the loop, rm_fd, and add_fd functions in CPP to check whether the file descriptor of epollerr | epollhup is consistent with that of the can't identify protocol viewed by lsof-P. In fact, as long as we achieve the first five points, the CPU idling will basically not happen.
PS: Finally, we hope that you will be able to think about why such a problem occurs and how to solve it will be of great help to your own improvement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.