Git Product home page Git Product logo

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.
  • 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

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.