mongols-0.9.2 釋出,C++ 伺服器基礎設施

類別: IT

mongols-0.9.2已經發布。

主要更新

  • 新增zstd庫

  • 為http server優化會話和快取管理

介紹:

mongols是一個主要針對c++伺服器端開發的基礎設施庫。主要特性如下:

  • tcp sever (epoll)

  • tcp multi-threading server (epoll+multi-threading)

  • http server (epoll)

  • http multi-threading server (epoll+multi-threading)

  • websocket server (epoll+multi-threading)

  • utils

依賴:

  • linux

  • gcc(c11),g++(c++11)

  • pcre-devel

  • zlib-devel

  • openssl-devel

sudo yum install pcre-devel zlib-devel openssl-devel

或者:

sudo apt-get install libpcre3-dev zlib1g-dev libssl-dev

安裝:

make clean && make -j2 && sudo make install && sudo ldconfig

使用:

pkg-config --libs --cflags mongols

例子:

#include <mongols/tcp_server.hpp>#include <mongols/tcp_threading_server.hpp>#include <mongols/http_server.hpp>#include <mongols/ws_server.hpp>#include <mongols/util.hpp>#include <iostream>/*//websocket serverint main(int,char**){	int port=9090;	const char* host="127.0.0.1";	mongols::ws_server server(host,port);	auto f=[](const std::string& input            , bool& keepalive            , bool& send_to_other            , std::pair<size_t, size_t>& g_u_id            , mongols::tcp_server::filter_handler_function& send_to_other_filter){			keepalive = KEEPALIVE_CONNECTION;			send_to_other=true;			return input;	};	server.run(f);	server.run();}*//*//tcp server or tcp multi-threading serverint main(int,char**){	auto f=[](const std::string& input                , bool& send_to_other                , std::pair<size_t, size_t>& g_u_id                , mongols::tcp_server::filter_handler_function& send_to_other_filter){					send_to_other=true;					return std::make_pair(input,KEEPALIVE_CONNECTION);				};	int port=9090;	const char* host="127.0.0.1";		mongols::tcp_threading_server	//mongols::tcp_server	server(host,port);	server.run(f);}*///http server or multi-threading serverint main(int,char**){	auto f=[](const mongols::request&){		return true;	};	auto g=[](const mongols::request& req,mongols::response& res){		/*std::unordered_map<std::string, std::string>::const_iterator i;		if((i=req.session.find("test"))!=req.session.end()){			long test=std::stol(i->second)+1;			res.content=std::to_string(test);			res.session["test"]=res.content;		}else{			res.content=std::to_string(0);;			res.session["test"]=res.content;		}*/		res.content=std::move("hello,world");		res.status=200;	};	int port=9090;	const char* host="127.0.0.1";	mongols::http_server 	//server(host,port,5000,1024,4);	server(host,port);	server.set_enable_session(false);	server.set_enable_cache(false);	server.run(f,g);}

壓力測試:

2核4G,centos7

ab -c1000 -n100000 http://127.0.0.1:9090/

RPS: 10000+

ab -kc1000 -n100000 -H'Connection: keep-alive'   http://127.0.0.1:9090/

RPS: 50000+

在ubuntu 18.04上RPS更高,keep-alive,高達80000-120000。

  • github: https://github.com/webcpp/mongols

  • websocket demo: http://fusheng.hi-nginx.com/

  • demo source: https://github.com/webcpp/fusheng

mongols-0.9.2 釋出,C++ 伺服器基礎設施原文請看這裡