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 應用程序啟動的入口文件
1 -module(ranch_app). 2 -behaviour(application). 3 4 -export([start/2]). 5 -export([stop/1]). 6 -export([profile_output/0]). 7 8 start(_, _) -> 9 _ = consider_profiling(), 10 ranch_sup:start_link(). 11 12 stop(_) -> 13 ok. 14 15 -spec profile_output() -> ok. 16 profile_output() -> 17 eprof:stop_profiling(), 18 eprof:log("procs.profile"), 19 eprof:analyze(procs), 20 eprof:log("total.profile"), 21 eprof:analyze(total). 22 23 consider_profiling() -> 24 case application:get_env(profile) of 25 {ok, true} -> 26 {ok, _Pid} = eprof:start(), 27 eprof:start_profiling([self()]); 28 _ -> 29 not_profiling 30 end.
這個文件是 行為模式 application 的具體實現, start(_, _) 應用啟動的入口。stop(_)關閉應用程序對外回調處理函數,主要讓我們在關閉前做保存數據,清理程序數據。使用。在這里主要留意兩個方法 第一個方法consider_profiling()讀取配置文件決定是否啟動應用程序性能分析工具。前面提到的 {env, [{profile,true}]} 也就是是否啟動性能分析的配置。 第二個方法 profile_output().打印輸出具體的性能分析。
下面我們來具體的看看上面所說的性能分析,編譯啟動程序 在shell中調用ranch_app:profile_output(). 結果如下
1> application:start(ranch). ok 2> toolbar:start(). <0.42.0> 3> ranch_app:profile_output(). ****** Process <0.37.0> -- 31.48 % of profiled time *** FUNCTION CALLS % TIME [uS / CALLS] -------- ----- --- ---- [----------] gen:where/1 1 0.00 0 [ 0.00] gen:timeout/1 1 0.00 0 [ 0.00] code:call/1 1 0.00 0 [ 0.00] proc_lib:start_link/5 1 0.00 0 [ 0.00] proc_lib:sync_wait/2 1 0.00 0 [ 0.00] proc_lib:get_ancestors/0 1 0.00 0 [ 0.00] supervisor:start_link/3 1 0.00 0 [ 0.00] lists:member/2 2 0.00 0 [ 0.00] error_handler:undefined_function/3 1 1.96 1 [ 1.00] gen:start/6 1 1.96 1 [ 1.00]
調用的函數,調用的次數,百分比,時間,調用一次需要的時間。等等都一目了然,清楚明白。這對程序提高效率是非常有幫助的。
3 接下來我們分析學習 ranch_sup.erl
1 -module(ranch_sup). 2 -behaviour(supervisor). 3 4 -export([start_link/0]). 5 -export([init/1]). 6 7 -spec start_link() -> {ok, pid()}. 8 start_link() -> 9 supervisor:start_link({local, ?MODULE}, ?MODULE, []). 10 11 init([]) -> 12 ranch_server = ets:new(ranch_server, [ 13 ordered_set, public, named_table]), 14 Procs = [ 15 {ranch_server, {ranch_server, start_link, []}, 16 permanent, 5000, worker, [ranch_server]} 17 ], 18 {ok, {{one_for_one, 10, 10}, Procs}}.
這個文件是干什么用的呢?如果把ranch_app看成董事長,那程序ranch_sup就相當公司的總經理,它管着下面的人怎么工作,工作的強度,如果出錯了怎么辦。都歸它管理。它就是監督者。supervisor:start_link({local, ?MODULE}, ?MODULE, []). 注冊一個名為ranch_sup的本地進程。注冊進程類型有 local,global,via三種類型依次為本地,全局,第三個暫時不清楚具體的功能功效,只知道要多導出三個回調函數待我弄明白否補上。今天就暫時說到這,剩下的明天繼續。。