erlang學習: 基於gen_server的echoserver及監控模組
第一版基礎上,將echoserver融入OTP:gen_server框架
同時引入OPT:supervisor行為包,實現對 echoserver的監控模組
當 echoserver退出後,監控模組master可以重啟之。
erlang的監控樹絕對是精華所在,解決了分散式開發的一個重要的問題域
同時引入OPT:supervisor行為包,實現對 echoserver的監控模組
當 echoserver退出後,監控模組master可以重啟之。
erlang的監控樹絕對是精華所在,解決了分散式開發的一個重要的問題域
-module(es2).
-behavior(gen_server).
-export([start_link/0, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([server_loop/2, handle_connect/3]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
stop() ->
gen_server:cast(?MODULE, stop).
init([]) ->
process_flag(trap_exit, true),
io:format("~p starting, ~p~n", [?MODULE, self()]),
{ok, ListenSocket} = gen_tcp:listen(1234, [binary, {reuseaddr, true}, {active, false}]),
register(echoserver, spawn(?MODULE, server_loop, [ListenSocket, 0])),
{ok, 0}.
terminate(_Reason, N) ->
io:format("~p stopping, ~p~n", [?MODULE, self()]),
ok.
handle_call(_Request, _From, _State) ->
{reply, 0, _State}.
handle_cast(stop, N) ->
echoserver ! {quit},
{stop, normal, N};
handle_cast(_Msg, N) ->
{noreply, N}.
handle_info(_Info, N) ->
{noreply, N}.
code_change(Old, N, Extra) ->
{ok, N}.
server_loop(ListenSocket, Count) ->
% 阻塞,等待連線
receive
{quit} ->
io:format("echoserver will stop~n"),
gen_tcp:close(ListenSocket);
{'EXIT', Pid, _Reason } ->
io:format("Child ~p exit~n", [Pid]),
server_loop(ListenSocket, Count);
_ ->
%io:format("recv ~p~n", Reason)
true
after 10 ->
case gen_tcp:accept(ListenSocket, 3000) of
{ok, Socket} ->
% 建立程式?
spawn(?MODULE, handle_connect, [Socket, [], Count]),
server_loop(ListenSocket, Count+1);
{error, timeout} ->
server_loop(ListenSocket, Count);
{error, Reason} ->
io:format("accept failed~n"),
gen_tcp:close(ListenSocket)
end
end.
handle_connect(Socket, BinaryList, Count) ->
io:format("handle_connect ~p~n", [self()]),
case gen_tcp:recv(Socket, 0) of
{ok, Binary} ->
% 繼續接收資料
case gen_tcp:send(Socket, Binary) of
ok ->
handle_connect(Socket, BinaryList, Count);
{error, Reason} ->
io:format("send failed~n"),
gen_tcp:close(Socket)
end;
{error, timeout} ->
io:format("recv timeout~n"),
gen_tcp:close(Socket);
{error, closed} ->
% 直到對端關閉
io:format("peer closed~n"),
gen_tcp:close(Socket)
end.
-module(master).
-behavior(supervisor).
-export([start/0, stop/0, start_monitor/0]).
-export([init/1]).
%% 從shell執行的時候呼叫此函式
start() ->
%% 建立master程式
register(master, spawn(?MODULE, start_monitor, [])).
stop() ->
master ! {quit}.
start_monitor() ->
io:format("master started: ~p~n", [self()]),
%% 建立監控子程式,註冊為monitor,並執行 init/1
{ok, Pid} = supervisor:start_link({local, monitor}, ?MODULE, []),
%unlink(Pid),
wait().
%% monitor程式執行init/1,啟動所有被監控的子程式,並監控這些程式
init(FileName) ->
io:format("enter init, ~s, ~p~n", [FileName, self()]),
%% 執行 es2:start_link
ChildSpec = {es, {es2, start_link, []}, permanent, 2000, worker, [es2]},
%% 監控規範:被監控程式退出後,重啟之,重啟次數100,重啟間隔10ms
{ok, {{one_for_one, 100, 10}, [ChildSpec]}}.
%% master程式迴圈等待退出訊息
wait() ->
receive
{quit} ->
io:format("recv quit msg~n");
%%master程式退出,從而導致monitor及所有monitor的子程式都退出
{'EXIT', Pid, Reason} ->
io:format("Child ~p exit~n", [Pid]),
wait()
end.
相關文章
- 基於OkHttp的Http監控HTTP
- python基礎學習16—-模組Python
- prometheus 監控學習Prometheus
- CurriculumNet:基於大規模網路影像的弱監督學習演算法演算法
- CurriculumNet:基於大規模網路影象的弱監督學習演算法演算法
- retrying模組的學習
- [Python模組學習] glob模組Python
- 模組學習之hashlib模組
- 模組學習之logging模組
- 基於 Prometheus 的監控系統實踐Prometheus
- 基於 prometheus 的微服務指標監控Prometheus微服務指標
- SpringCloud使用Prometheus監控(基於Eureka)SpringGCCloudPrometheus
- Python開發常用的庫及模組!Python學習教程Python
- 學習Python的urllib模組Python
- itertools 模組學習
- 基於自編碼器的表徵學習:如何攻克半監督和無監督學習?
- 基於多模態對抗學習的無監督時間序列異常檢測
- 基於websocket的celery任務狀態監控Web
- 基於 Prometheus 的監控神器,簡單靈活!Prometheus
- APM效能監控軟體的監控型別服務及監控流程型別
- Python案例學習——模組和包、爬蟲的基礎概念Python爬蟲
- Node fs模組學習
- Python學習——logging模組Python
- Python學習——shelve模組Python
- Python學習——xml模組PythonXML
- Python學習——configparser模組Python
- Python學習——hashlib模組Python
- Python學習之模組Python
- Linux核心模組學習Linux
- nginx學習之模組Nginx
- 基於Serverless雲函式站點監控的方法Server函式
- 基於 ZooKeeper 實現爬蟲叢集的監控爬蟲
- 基於 IntersectionObserver 實現一個元件的曝光監控Server元件
- Java生產環境效能監控與調優—基於JDK命令列工具的監控JavaJDK命令列
- 基於 Zabbix 系統監控 Windows、Linux、VMwareWindowsLinux
- SpringCloud使用Sofa-lookout監控(基於Eureka)SpringGCCloud
- Erlang那些事兒第2回之我是模組(module),一檔案一模組
- Python的shutil zipfile tarfile模組學習Python
- 基於 HTML5 的 WebGL 3D 隧道監控HTMLWeb3D