ranch分析学习(一)
Ranch 是一个tcp处理的程序框架。官方的解释 Ranch is a socket acceptor pool for TCP protocols. 主要目的是提供一个方便,易用,高效,稳定的tcp处理基础程序。前面我也用它作为基础写了个简易的聊天的程序。cowboy底层通信处理也是ranch处理,聊聊数十个个文件做为基础的http服务器。今天我们就来看看它到底有什么魔力。废话不多,接下的几天我将分析它,说说我的学习心得。如果有什么地方说的不对还请大家指正。
源码下载地址: https://github.com/extend/ranch
使用例子:https://github.com/extend/ranch/tree/master/examples
下载shell : git clone https://github.com/extend/ranch
首先我们看一下整个目录树 整个程序总共13个文件。整个程序都是采用otp程序的方式组织的。
├── ranch_acceptor.erl
├── ranch_acceptors_sup.erl
├── ranch_app.erl
├── ranch.app.src
├── ranch_conns_sup.erl
├── ranch.erl
├── ranch_listener_sup.erl
├── ranch_protocol.erl
├── ranch_server.erl
├── ranch_ssl.erl
├── ranch_sup.erl
├── ranch_tcp.erl
└── ranch_transport.erl
1.首先我们来看看 ranch.app.src
{application, ranch, [
{description, "Socket acceptor pool for TCP protocols."},
{vsn, "0.10.0"},
{modules, []},
{registered, [ranch_sup, ranch_server]},
{applications, [
kernel,
stdlib
]},
{mod, {ranch_app, []}},
{env, [{profile,true}]}
]}.
这个文件 主要是 Erlang OTP设计原则 ,其余的我都不多叙述。这里主要看看对应的环境配置选项 {env, [{profile,true}]} 这个选项是ranch 对进程的效率分析,在优化提高效率时非常有用。在生产环境中不要配置,否者会影响程序的执行效率。 这个文件是应用程序启动所需,在发布打包后 通过 application:start(ranch). 启动application:stop(ranch).关闭应用程序。
2.ranch_app.erl 应用程序启动的入口文件
-module(ranch_app).
-behaviour(application). -export([start/]).
-export([stop/]).
-export([profile_output/]). start(_, _) ->
_ = consider_profiling(),
ranch_sup:start_link(). stop(_) ->
ok. -spec profile_output() -> ok.
profile_output() ->
eprof:stop_profiling(),
eprof:log("procs.profile"),
eprof:analyze(procs),
eprof:log("total.profile"),
eprof:analyze(total). consider_profiling() ->
case application:get_env(profile) of
{ok, true} ->
{ok, _Pid} = eprof:start(),
eprof:start_profiling([self()]);
_ ->
not_profiling
end.
这个文件是 行为模式 application 的具体实现, start(_, _) 应用启动的入口。stop(_)关闭应用程序对外回调处理函数,主要让我们在关闭前做保存数据,清理程序数据。使用。在这里主要留意两个方法 第一个方法consider_profiling()读取配置文件决定是否启动应用程序性能分析工具。前面提到的 {env, [{profile,true}]} 也就是是否启动性能分析的配置。 第二个方法 profile_output().打印输出具体的性能分析。
下面我们来具体的看看上面所说的性能分析,编译启动程序 在shell中调用ranch_app:profile_output(). 结果如下
> application:start(ranch).
ok
> toolbar:start().
<0.42.>
> ranch_app:profile_output(). ****** Process <0.37.> -- 31.48 % of profiled time ***
FUNCTION CALLS % TIME [uS / CALLS]
-------- ----- --- ---- [----------]
gen:where/ 0.00 [ 0.00]
gen:timeout/ 0.00 [ 0.00]
code:call/ 0.00 [ 0.00]
proc_lib:start_link/ 0.00 [ 0.00]
proc_lib:sync_wait/ 0.00 [ 0.00]
proc_lib:get_ancestors/ 0.00 [ 0.00]
supervisor:start_link/ 0.00 [ 0.00]
lists:member/ 0.00 [ 0.00]
error_handler:undefined_function/ 1.96 [ 1.00]
gen:start/ 1.96 [ 1.00]
调用的函数,调用的次数,百分比,时间,调用一次需要的时间。等等都一目了然,清楚明白。这对程序提高效率是非常有帮助的。
3 接下来我们分析学习 ranch_sup.erl
-module(ranch_sup).
-behaviour(supervisor). -export([start_link/]).
-export([init/]). -spec start_link() -> {ok, pid()}.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) ->
ranch_server = ets:new(ranch_server, [
ordered_set, public, named_table]),
Procs = [
{ranch_server, {ranch_server, start_link, []},
permanent, , worker, [ranch_server]}
],
{ok, {{one_for_one, , }, Procs}}.
这个文件是干什么用的呢?如果把ranch_app看成董事长,那程序ranch_sup就相当公司的总经理,它管着下面的人怎么工作,工作的强度,如果出错了怎么办。都归它管理。它就是监督者。supervisor:start_link({local, ?MODULE}, ?MODULE, []). 注册一个名为ranch_sup的本地进程。注册进程类型有 local,global,via三种类型依次为本地,全局,第三个暂时不清楚具体的功能功效,只知道要多导出三个回调函数待我弄明白否补上。今天就暂时说到这,剩下的明天继续。。
ranch分析学习(一)的更多相关文章
- ranch分析学习(四)
经过的前面的梳理,整个ranch框架的结构,大致有了一个清晰的脉络,即使我说的不是很清楚大家也基本能阅读懂源码.下面我继续分析剩下的的几个文件. 7.ranch_transport.erl 这个文件是 ...
- ranch分析学习(三)
接着上一篇继续研究 上一篇结尾的时候,我们谈到了连接,监听两个监督树,今天我们就来看看这两个监督树和他们的工作者都是干什么的,怎么实现的.文件编号接上篇. 6. ranch_acceptors_sup ...
- ranch分析学习(二)
紧接上篇,今天我们来分析监督树的工作者,打工仔执行任务的人.废话不多少我们直接进入正题. 3.ranch_server.erl 整个文件的功能主要是存储tcp对应参数的的信息.信息的存储方式采用的 ...
- jQuery源码逐行分析学习02(第一部分:jQuery的一些变量和函数)
第一次尝试使用Office Word,方便程度大大超过网页在线编辑,不过初次使用,一些内容不甚熟悉,望各位大神见谅~ 在上次的文章中,把整个jQuery的结构进行了梳理,得到了整个jQuery的简化结 ...
- ranch 源码分析(完)
接上 ranch 源码分析(三) 在上一次,根据ranch源码把大概流程理了一遍,下面我们将一些细节解释一下. ranch只是一个服务的框架,它提供了传输层协议代码(ranch_tcp 和ranch_ ...
- Objective-C Programming The Big Nerd Ranch Guide 笔记 19-37
Properties are either atomic or nonatomic, The difference has to do with multithreading. atomic is t ...
- ranch 源码分析(一)
以前写了一个ranch的处理流程,http://www.cnblogs.com/tudou008/p/5197314.html ,就只有一张图,不是很清晰,现在有空做个源码分析. ranch的源码(版 ...
- ranch 源码分析(三)
接上ranch 源码分析(二) 上次讲到了ranch_conns_sup和ranch_acceptors_sup这2个ranch的核心模块,我们接着分析 首先查看ranch_conns_sup.erl ...
- ranch 源码分析(二)
接上ranch 源码分析(一) 上次讲到了ranch.erl的start_listener函数,下面我们详细分析下这个函数 -module(ranch). %...... 省略若干行 -spec st ...
随机推荐
- Why not inherit from List<T>?
问题: When planning out my programs, I often start with a chain of thought like so: A football team is ...
- mybatis的一级缓存和二级缓存(1)
1.mybatis一级缓存,sqlSesion级别的缓存,一级缓存默认一直开启的,sqlSession级别的一个Map,把查询的数据放到一个Map中,以后需要相同的数据,直接从Map中去取 与数据库一 ...
- Linux服务器上ftp的搭建和使用
知识点: 1. FTP的简介.工作原理 2.在Linux上搭建FTP服务器 参考: 阿里云文档:https://help.aliyun.com/knowledge_detail/60152.html ...
- 01_zookeeper简介(刷新)
1. 分布式系统及其问题 zookeeper是帮助我们构建分布式系统的一个软件(协调员的角色)首先,我们要明白分布式系统以及它的问题,之后才能理解为什么有zookeeper 1.1 分布式系统 分布式 ...
- Maven 一段时间知识小结
二种打包命令生成后的jar包比较 1.clean install -P dev 2.clean package -Dmaven.test.skip=true -P dev //clean packa ...
- hibernate:inverse、cascade,一对多、多对多详解
1.到底在哪用cascade="..."? cascade属性并不是多对多关系一定要用的,有了它只是让我们在插入或删除对像时更方便一些,只要在cascade的源头上插入或是删除,所 ...
- offset的坑 使用前要将对象先show
使用jquery $('#obj').offset( {top:300, left: 600}); 如果设置offset之前是隐藏的,那么你设置新的offset之后就不会是指定的位置,而且会越飞越远, ...
- 【Python】模块学习之(__call__)实现准确计算函数运行时间
背景 博主在写自动化的过程中,有遇到有的用例运行缓慢的问题,想起在上一家公司的的“自动化工厂”有一个指标:两小时内运行完所有的用例才算合格.所以想计算每一个用例的运行时间. 思路 因为使用的POM模型 ...
- [spring mvc]Hello World入门
1.新建项目 File->New->Other,选择Dynamic web project: 项目建好之后,目录结构如下: 2.WEB-INF/web.xml 中配置 dispatcher ...
- js今日小结—Ajax、前端安全、GET&POST、闭包、HTTPS
HTTPS HTTP+加密(SSL.TLS)+认证+完整性保护 = HTTPS: GET和POST的区别 get拉取数据,post传输数据 get请求能被浏览器主动缓存,post不会(除非手动) ge ...