网站首页> 文章专栏> yalantinglibs 增加了一个新库coro_http_server
yalantinglibs 新增加了一个基于C++20 协程的http server(https://github.com/alibaba/yalantinglibs/blob/main/src/coro_http/examples/example.cpp)
让你告别异步回调模型,代码写起来更加简单。同样也是跨平台, header only的,包含头文件即可用,来看看它的用法。
coro_http_server server(1, 9001);
server.set_http_handler<GET, POST>(
"/", [](coro_http_request &req, coro_http_response &resp) {
// response in io thread.
resp.set_status_and_content(status_type::ok, "hello world");
});
server.set_http_handler<GET>(
"/coro",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
co_await coro_io::post([&] {
// coroutine in other thread.
resp.set_status_and_content(status_type::ok, "hello world in coro");
});
});
server.sync_start();
coro_http_server 提供了三个http 服务,通过set_http_handler注册的,注册了两种http 处理函数:一种是普通函数,一种是协程函数。注册为普通函数时,会在io 线程里执行该函数;注册为协程函数时,允许在其它线程或线程池中执行。上面注册协程函数的例子展示了将业务函数调度到coro_io内部的线程池中执行。co_await 时协程挂起,io 线程不会阻塞,可以继续处理新的io 事件。
coro_http_server 也支持https, 只要设置证书也密码等参数即可。
coro_http_server server(1, 9001);
server.init_ssl("server.crt", "server.key", "test");
server.set_http_handler<GET, POST>(
"/ssl", [](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ssl");
});
server.sync_start();
cinatra::coro_http_server server(1, 9001);
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/chunked",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::chunked);
chunked_result result{};
std::string content;
while (true) {
result = co_await req.get_conn()->read_chunked();
if (result.ec) {
co_return;
}
if (result.eof) {
break;
}
content.append(result.data);
}
std::cout << content << "\n";
resp.set_format_type(format_type::chunked);
resp.set_status_and_content(status_type::ok, "chunked ok");
});
while 循环不断的co_await chunked流数据,直到读完所有的数据为止,接口使用比回调模式简单很多。
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/write_chunked",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_format_type(format_type::chunked);
bool ok;
if (ok = co_await resp.get_conn()->begin_chunked(); !ok) {
co_return;
}
std::vector<std::string> vec{"hello", " world", " ok"};
for (auto &str : vec) {
if (ok = co_await resp.get_conn()->write_chunked(str); !ok) {
co_return;
}
}
ok = co_await resp.get_conn()->end_chunked();
});
类似的,循环不断的co_await write_chunked(),写完之后调用end_chunked()即可。
目前只做了简单场景的测试,后面会增加更多的场景做测试。测试返回"hello world", 在96 核机器上,96线程,960 连接,qps 为370万。后面会增加更多场景的benchmark。
欢迎使用基于协程的coro_http_server,如果觉得不错请在github上点一个star,以便让更多人看到,这也是对我们最大的支持。项目地址:https://github.com/alibaba/yalantinglibs。
最后附上两首小诗:
《折桂》
折枝犹在手,韵味或能留。
细嗅香不在,芬芳绕指柔。
《雨中赏桂》
桂雨飘飘无声下,
金黄满地香雪洒。
秋风不解落花泪,
一年只开一旬花。
地址: www.purecpp.cn
转载请注明出处!
purecpp
一个很酷的modern c++开源社区
purecpp社区自2015年创办以来,以“Newer is Better”为理念,相信新技术可以改变世界,一直致力于现代C++研究、应用和技术创新,期望通过现代C++的技术创新来提高企业生产力和效率。
社区坚持只发表原创技术文章,已经累计发表了一千多篇原创C++技术文章;
组织了十几场的C++沙龙和C++大会,有力地促进了国内外C++开发者之间的技术交流;
开源了十几个现代C++项目,被近百家公司所使用,有力地推动了现代C++在企业中的应用。
期待更多的C++爱好者能参与到社区C++社区的建设中来,一起为现代C++开源项目添砖加瓦,一起完善C++基础设施和生态圈。
微信公众号:purecpp, 社区邮箱: purecpp@163.com