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 ...
随机推荐
- 深入理解Java枚举类型(enum)
https://blog.csdn.net/javazejian/article/details/71333103 深入理解Java类型信息(Class对象)与反射机制 深入理解Java枚举类型(en ...
- Java RMI 简单示例
一.创建远程服务 1.创建 Remote 接口,MyRemote.java import java.rmi.*; public interface MyRemote extends Remote{ p ...
- htop 比top更好用的top
安装 sudo apt-get install htop 使用 htop
- photoshop cc下载与安装
地址:https://jingyan.baidu.com/article/c275f6bacdd927e33d756729.html
- Module 的加载实现
浏览器加载 传统方法 HTML 网页中,浏览器通过<script>标签加载 JavaScript 脚本. <!-- 页面内嵌的脚本 --> <script type=&q ...
- HAproxy的安装配置及动静分离
/////////////////////////////目录//////////////////////////////////////////一.安装HAproxy二.编写HAproxy启动脚本三 ...
- duff's device
const duffDevice = (items, process) => { let iterations = Math.floor(items.length / 8); let start ...
- mysql术语
事务 概念:在关系数据库中,一个事物可以是一条sql语句,一组sql语句或整个程序. 特性:事物应该具有4个特性:原子性.一致性.隔离性.持久性.统称为ACID特性. 原子性(A)一个不可分割的工作单 ...
- Unity使用Win10语音
1. 引入头文件 using UnityEngine.Windows.Speech; 2. 设置识别词 public string[] keywords = new string[] { ...
- gem doorkeeper(4000✨) ,Go-rails视频
博客OAuth教程:https://i.cnblogs.com/EditPosts.aspx?postid=9531091 doorkeeper: (4000