GithubHelp home page GithubHelp logo

loveyacper / ananas Goto Github PK

View Code? Open in Web Editor NEW
534.0 45.0 128.0 1.33 MB

A C++11 RPC framework based on future and protobuf, with utility: timer,ssl,future/promise,log,coroutine,etc

License: MIT License

CMake 2.35% Makefile 0.05% C++ 97.61%
c-plus-plus rpc coroutine future server-side

ananas's Introduction

ananas

Build Status

A C++11 RPC framework and toolbox for server-side development.

Requirements

  • C++11 compiler (required)
  • CMake (required)
  • Linux or MAC OS (required)
  • google protobuf 3.0 or newer (optional)
  • doxygen for docs (optional)
  • redis for name service (optional, just for demo)

Features

  • Protobuf-based rpc.
  • Future & Promise, see future.
  • HttpServer & HttpClient, see http.
  • Http health service for inspect, enabled by SetHealthService().
  • Network(udp&tcp,kqueue&epoll), support ssl.
  • Pythonic generator style coroutine, see Intro.
  • Easy used and powerful timer, see test.
  • Extremely high performance multi-thread logger, see Intro.
  • etc...

API和Class文档查看

    安装doxygen之后,执行make doc,用浏览器打开当前目录中生成的ananas.html即可。     如图所示:

ananas简介

    ananas是一个基于promise模式和google protobuf的RPC框架,目前由C++11实现,golang版本在计划中。

    写这份c++代码属于个人的舒适区,所以不会是一个大而全的框架,再加上出于教学目的,它只包含RPC框架该有的部分,非常小巧,便于初学者学习.

    可以基于ananas rpc编写代码,也可以抛弃rpc,仅仅使用网络库编写应用(类似使用muduo)。

    ananas主要包括两个部分:基础部分是一个reactor模式的多线程非阻塞网络库,基于epoll或kqueue,和陈硕先生的muduo比较相似;ananas有一些部分参考了java netty的实现。如果你是javaer,看到了EventLoopGroup可能会心一笑。

    另一个部分则是在网络库之上,融合google protobuf以及ananas future库实现了一套RPC框架,只提供了基于future的异步调用接口。可能有人会问,为什么不提供同步。最大的原因是,我不认为一个非阻塞的框架应该提供同步操作(无协程情况)。尽管netty的future提供了阻塞接口,但官方文档中明确指出不提倡使用。如果思考一下one eventloop per thread模型就明白了,同步很有可能造成死锁。另一个原因是,future接口已经非常易用,并没有将逻辑打的支离破碎,而且支持when-all、when-any、when-N并发模式,十分强大。

更新:现在future提供了阻塞的Wait接口,但和netty4一样,不推荐在正式代码中使用,除非你非常清楚在做什么,否则程序出现死锁。而异步接口永远没有顾虑,还有性能上的优势.在使用阻塞的Wait接口前,请务必检查EventLoop::Self()为nullptr,否则可能造成死锁!另外请参考Netty关于同步接口死锁的文档

需要什么预备知识

  • 你需要较为了解socket API,对TCP有一定认识;
  • 对多线程有所了解即可,会正确使用互斥锁、条件变量;
  • 需要较为熟悉C++11,特别是shared_ptr, weak_ptr, bind&function, move&forward, lambda. 强烈推荐《Effective modern C++》.

  对于新手,建议先放弃rpc和future部分.

推荐的书

  • Effective modern C++

    学习C++11只需要这一本书,请耐心多读几遍。

  • Unix网络编程

    被推荐烂了,书很厚,但只需要读1/3:

    第1-7章;基础,特别是要理解TCP的双工特性.
    第14章;了解gather write/scatter read.
    第16章;这是本书最最重要的一章。
    第30章;

  • Netty in action

    各种语言的网络库千千万,netty是其中翘楚。

源码目录结构

整个ananas的核心源码在一万行以内,其中net和util约六千行,rpc部分约两千多行,只实现必不可少的部分,便于学习。

net

reactor模式的多线程非阻塞网络库,基于epoll和kqueue,one-eventloop-per-thread模型,支持TCP/UDP/Timer等功能。

future

受folly的future库启发而来,是ananas rpc的核心。极大方便了异步编程,支持链式回调,超时回调,when-all,when-any,when-N等模式。

util

基础工具库,主要有timer/delegate/defer/ThreadPool等功能,详见util说明

protobuf_rpc

基于protobuf和future的异步RPC库,可自定义协议;支持名字服务、轮转负载均衡、容错。

unittest

一个非常简单的单元测试类

coroutine

与python generator很相似的协程,使用swapcontext系统调用实现,只支持linux系统。

tests

一些测试代码

examples

一些用例代码

ananas's People

Contributors

ironsdu avatar loveyacper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ananas's Issues

烦请处理下gcc的警告

这种capture的方式在C++11标准下会报警告,麻烦看下是否可以修复。

dep/ananas/future/Future.h:489:26: warning: lambda capture initializers only available with -std=c++14 or -std=gnu++14
                          prom = std::move(pm)](typename TryWrapper<T>::Type&& t) mutable {

dep/ananas/future/Future.h:491:28: warning: lambda capture initializers only available with -std=c++14 or -std=gnu++14
                 auto cb = [func = std::move(func), t = std::move(t), prom = std::move(prom)]() mutable {

Future.h内有个小问题

static_assert(std::is_same<T, void>::value ||
std::is_copy_constructible() ||
std::is_move_constructible(),
"must be copyable or movable or void");
修改为
static_assert(std::is_same<T, void>::value ||
std::is_copy_constructible::value ||
std::is_move_constructible::value,
"must be copyable or movable or void");

参见对正在编译的函数 模板 实例化

note: 参见对正在编译的函数 模板 实例化“ananas::Future ananas::Future::_ThenImpl<F,R,T&&>(ananas::Scheduler *,F &&,ananas::internal::ResultOfWrapper<F,T &&>)”的引用
1> with
1> [
1> F=main::<lambda_0ca2531bee9689aededc86c496028799>,
1> R=ananas::internal::CallableResult<main::<lambda_0ca2531bee9689aededc86c496028799>,int>,
1> T=int
1> ]

我用msvc编译提示这个,但是能编译过去的,警告降到2级就不会提示···有问题吗这个!

可以中断then的future 链吗

ananas::MakeReadyFuture()
	.Then([] {
		throw std::runtime_error("rrrrrrrrrrrrrrrrrrr");
		return std::string{"Hello1"};
	})
	.Then([](ananas::Try<std::string>&& str) {
		try {
			std::string a = str;
			std::cout << a << std::endl;
			return 100;
		} catch (const std::runtime_error&) {
			std::cout << "xxxxxx" << std::endl;
                           // 希望发生异常以后 退出 这个回调链
			return 888;
		}
	})
	.Then([](int nn) {
		std::cout << nn << std::endl;
	});

t'r'y

exception_.~exception_ptr()

future/promise集成度似乎还不够高

网络的一些操作还是基于epoll的异步回调的,比如connector中的非阻塞连接行为, 能否具有类似
connect().then([] { onSuccess(); }) 的同步调用模式呢?
另外,同步阻塞操作的异步化都是通过线程池的方式实现的,这里能不能考虑使用协程而不是线程来避免线程上下文切换的开销呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.