Reverse proxy and load balancing of mongols

mongols, a C + + server infrastructure library, has recently been updated to provide reverse proxy and load balancing capabilities.

The following is a comparison between two nodejs backend (8888 and 8889) represented by mongols and nginx compression test:

The pressure test shows that mongols has completely exceeded nginx.

Here is the code for the pressure test.

First, nodejs:

var http = require('http');
var port = 8888;//8889

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});

    response.end('Hello World\n');

console.log('Server running at'+port+'/');

Then mongols:

#include <unistd.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <sys/prctl.h>

#include <mongols/util.hpp>
#include <mongols/tcp_proxy_server.hpp>

#include <cstring>
#include <iostream>
#include <functional>

int main(int, char**) {
    //    daemon(1, 0);
    auto f = [](const mongols::tcp_server::client_t & client) {
        return true;
    int port = 9090;
    const char* host = "";

    mongols::tcp_proxy_server server(host, port, 5000, 8192, 0/*2*/);


    //see example/nodejs
    server.set_backend_server(host, 8888);
    server.set_backend_server(host, 8889);


    std::function<void(pthread_mutex_t*, size_t*) > ff = [&](pthread_mutex_t* mtx, size_t * data) {;

    std::function<bool(int) > g = [&](int status) {
        std::cout << strsignal(WTERMSIG(status)) << std::endl;
        return false;

    mongols::multi_process main_process;, g);


For the same backend and the same number of working processes, the concurrency performance of mongols is better than that of nginx no matter whether the cache is enabled or not compared with the proxy pass scheme of nginx.


mongols also provides easy to develop connection level security. For example, f above can be rewritten as:

 auto f = [](const mongols::tcp_server::client_t & client) {
            return false;
            return false;
            return false;
        return true;

This functional takes the class client? T as an argument. The developer can obtain the system unique identifier sid of the connection, the connection establishment time t, the number of times that the connection has sent data count, the ip of the connection, and the total number of connections that the server keeps online.

Now, f means that if the total number of connections of the server exceeds 100000, or the frequency of sending data by a single connection exceeds 50 times per second, or the current connection ip is x.x.x.x, the current connection will be closed.

When the connection is closed, an empty string is returned for the tcp agent and a 403 error is returned for the http agent. Developers can set the default return value through the set default content method.




Tags: C++ Nginx github

Posted on Wed, 04 Dec 2019 03:16:17 -0800 by Sanoz0r