erlang小demo2_gen_server模拟游戏服务器进程管理

题目:在游戏中,有很多需要使用共享资源来处理的功能,这时候就需要单独管理进程来分配,并且按照请求有序执行资源分配,假设当前系统有X个资源,每个请求携带需要占用Y个资源,占用耗时Z秒

题目要求

  • 使用gen_server实现该进程管理
  • 请求资源消息,分配资源,资源不足时,返回失败,耗时结束时释放资源
  • 取消资源消息,立马释放资源

题目解析

首先我们要弄清楚需要实现的需求,有一个共享资源(所有进程都能读取到)为X数量,然后每个进程向服务器请求需要Y个资源,并且每个请求耗时Z秒。假如一个进程A向服务器请求100个资源,且服务器共享资源有1000个,那服务器分配成功后就只剩下900个资源,且下一个进程读取到的是900个资源,注意题目还有一个条件是占用耗时,比如是进程A请求100个单位的资源耗时10s,意思是10s后服务器可用的共享资源要加回这100,且第三个要求说进程可以发送提前取消资源的消息,大概就是这些。

具体实现(客户端)

我们先搭建一个gen_server的框架

整体框架

%%%-------------------------------------------------------------------
%%% @author fengshangjiong
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 28. 十二月 2020 16:43
%%%-------------------------------------------------------------------
-module(client1).
-author("fengshangjiong").

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
    handle_call/3,
    handle_cast/2,
    handle_info/2,
    terminate/2,
    code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {}).

start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).


init([]) ->
    {ok, #state{}}.

handle_call(_Request, _From, State) ->
    {reply, ok, State}.


handle_cast(_Request, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

首先复习一下gen_server的结构,有熟悉的start_link初始化的方法,还有同步/异步接收消息的handle_call和handle_cast方法,以及没有返回值的handle_info,终止terminate和code_change代码热换先不说

我们可以给每个申请进来的进程取个名字,比如test1,test2,需要改一个初始化模块

初始化模块

start(Id) ->
    gen_server:start(?MODULE, [Id], []).
stop() ->
    gen_server:call(?MODULE,stop).

init([Id]) ->
    register(list_to_atom("test" ++ integer_to_list(Id)), self()),
    {ok, #state{}}.


%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @end
%%--------------------------------------------------------------------
-spec(start_link() ->
    {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
start_link(Id) ->
    gen_server:start(?MODULE, [Id], []).

不用原有的start_link我们传入一个Id,给每个客户端申请一个新的名字

这个时候客户端的创建流程就完成了,然后是逻辑代码的编写,请求资源消息和提前取消资源的消息

业务代码

req_resourse(Req_Resources_Amount, Req_time, Name)->
    gen_server:call(server1, {apply_resourse, Req_Resources_Amount, Req_time, Name}).
%% 提前取消
req_cancel(Name) ->
    Reply = gen_server:call(server1, {req_cancel_early, Name}),

    io:format("~p~n", [Reply]).

这里我给服务器取名就叫server1,在这里直接call就行,然后服务端编写对应的接收代码即可

到这里客户端代码其实已经没什么需要添加的,可以在handle_info里接收一个状态信息

handle_info(Msg, State) ->
    io:format("~p~n",[Msg]),
    {noreply, State}.

然后看看服务端对应接收

具体实现(服务端)

基本框架+初始化

start() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
stop() ->
    gen_server:call(?MODULE,stop).
start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
init([]) ->
    {ok, ets:new(?MODULE, [set, named_table])}.

初始化这里我是创建了跟文件名同名的ets,准备是用来存资源分配信息的,然后ets的第一个键我准备存系统的全局资源,这样也好符合共享的条件,所有进程过来都能读取到这个资源

然后是处理客户端的请求

处理请求

handle_call({apply_resourse,Req_Resources_Amount, Req_time, Name}, _From, Tab) ->
    %%第一次进来,ets里没存系统资源有多少,就会存一下,后面进来就有系统资源数据数据了 则不存
    case ets:member(Tab,admin) of
        false ->
            ets:insert(Tab,{admin,1000});
        _ ->
            ok
    end,
    %%读取系统资源
    [{_,Resources}] = ets:lookup(Tab, admin),
    Reply = case ets:lookup(Tab, Name) of
                [] when Resources >= Req_Resources_Amount  ->
                    ets:insert(Tab, {Name, Req_Resources_Amount}),
                    NewResources = Resources - Req_Resources_Amount,
%%                    ets:delete(Tab,admin),
                    ets:insert(Tab, {admin, NewResources}),
                    erlang:send_after(Req_time, self(), {realse, Name}),
                    {distribution_success};
                [] ->
                    {resourse_not_enough};
                [_] ->
                    {already_request}
            end,
    {reply, Reply, Tab};

这里是第一个请求处理,就是请求资源的处理,包括几种情况,在ets查找该进程是否有请求的记录,如果有就不给请求了,如果没有,还要看请求的资源是否大于当前系统的剩余资源,都符合才给请求,每个case最后一句都是返回给终端的打印消息,类似于日志,最后按照gen_server中的handle_call返回标准消息即可

注意其中有一句

erlang:send_after(Req_time, self(), {realse, Name}),

这里设置了一个定时器,是用来实现请求时间到期后,服务端释放资源的方法,时间到了服务端给自己发送一个信号,给这哥们释放了不让他继续用了,然后下面是释放资源的处理函数

handle_call({realse,Name}, _From, Tab) ->
    Reply = do(Tab, Name),
    Name ! {realse_success},
    {reply, Reply, Tab};

以及客户端提前取消资源的函数

handle_call({req_cancel_early, Pid}, _From, Tab) ->
%%    io:format("ccccccccc"),
    Reply = do(Tab, Pid),
%%    io:format("xxxxxxxxxxxx"),
    {reply, Reply, Tab};

handle_call(stop, _From, Tab) ->
    {stop, normal, stopped, Tab}.

这里的do是处理消息释放的函数,我单独抽取出来封装了,因为在第二个handle_call中调用了自己,当然不能再handle_call中调用handle_call的方法,你不死锁谁死锁,要在handle_info里处理服务端自己发送释放的消息(其实我感觉这里可以在客户端做,但是不知道咋实现)

handle_info({realse,Name}, Tab) ->
    Reply = do(Tab, Name),
    Name ! {realse_success},
    {noreply, Tab}.

释放请求的代码

%%%===================================================================
%%% Internal functions
%%%===================================================================


do(Tab, Pid) ->
    case ets:lookup(Tab, Pid) of
        [_] ->
            [{_,Resources}] = ets:lookup(Tab, admin),
            [{_,Release_Resources}] = ets:lookup(Tab, Pid),
            NewResources = Resources + Release_Resources,
            ets:delete(Tab, Pid),
%%            ets:delete(Tab,admin),
            ets:insert(Tab, {admin, NewResources}),
            {realse_success};
        [] ->
            %%可能被客户端提前取消,即使定时器未关闭,
            %% 等定时器到点后也会走这里在ets中找不到数据返回
            {already_realse}
    end.

我们到这里差不多了,运行下看看,代码可能写的比较累赘,但是逻辑还是很清晰的,如果没看懂,后面有完整的代码,先看运行结果

运行结果

服务端启动->客户端启动->客户端申请资源->正常结束释放->客户端再申请资源->客户端发送提前释放信号->释放成功

erlang小demo2_gen_server模拟游戏服务器进程管理

我们也可以用observer工具观察ets中的存储情况

erlang小demo2_gen_server模拟游戏服务器进程管理

可以明显发现,ets中有读取和释放操作

完整代码

client1.erl

%%%-------------------------------------------------------------------
%%% @author fengshangjiong
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 29. 十二月 2020 9:02
%%%-------------------------------------------------------------------
-module(client1).
-author("fengshangjiong").

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
    handle_call/3,
    handle_cast/2,
    handle_info/2,
    terminate/2,
    code_change/3]).

-export([
    start/1,
    start_link/1,
    stop/0,
    req_resourse/3,
    req_cancel/1
]).

-define(SERVER, ?MODULE).

-record(state, {}).


%%题目:在游戏中,有很多需要使用共享资源来处理的功能,这时候就需要单独管理进程来分配,
%% 并且按照请求有序执行资源分配,假设当前系统有X个资源,每个请求携带需要占用Y个资源,占用耗时Z秒
%% 1) 使用gen_server实现该管理进程
%% 2) 请求资源消息, 分配资源, 资源不足时, 返回失败, 耗时结束时释放资源
%% 3) 取消资源消息, 立马释放资源

%%%===================================================================
%%% API
%%%===================================================================



start(Id) ->
    gen_server:start(?MODULE, [Id], []).
stop() ->
    gen_server:call(?MODULE,stop).

req_resourse(Req_Resources_Amount, Req_time, Name)->
    gen_server:call(server1, {apply_resourse, Req_Resources_Amount, Req_time, Name}).
%% 提前取消
req_cancel(Name) ->
    {Reply} = gen_server:call(server1, {req_cancel_early, Name}),

    io:format("~p~n", [Reply]).


%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @end
%%--------------------------------------------------------------------
-spec(start_link() ->
    {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
start_link(Id) ->
    gen_server:start(?MODULE, [Id], []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore |
%%                     {stop, Reason}
%% @end
%%--------------------------------------------------------------------
-spec(init(Args :: term()) ->
    {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term()} | ignore).
init([Id]) ->
    register(list_to_atom("test" ++ integer_to_list(Id)), self()),
    {ok, #state{}}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
    State :: #state{}) ->
    {reply, Reply :: term(), NewState :: #state{}} |
    {reply, Reply :: term(), NewState :: #state{}, timeout() | hibernate} |
    {noreply, NewState :: #state{}} |
    {noreply, NewState :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term(), Reply :: term(), NewState :: #state{}} |
    {stop, Reason :: term(), NewState :: #state{}}).
handle_call(_Request, _From, State) ->
    {reply, ok, State};

handle_call(stop, _From, State) ->
    {stop, normal, stopped, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @end
%%--------------------------------------------------------------------
-spec(handle_cast(Request :: term(), State :: #state{}) ->
    {noreply, NewState :: #state{}} |
    {noreply, NewState :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term(), NewState :: #state{}}).
handle_cast(_Request, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%%                                   {noreply, State, Timeout} |
%%                                   {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
-spec(handle_info(Info :: timeout() | term(), State :: #state{}) ->
    {noreply, NewState :: #state{}} |
    {noreply, NewState :: #state{}, timeout() | hibernate} |
    {stop, Reason :: term(), NewState :: #state{}}).
handle_info(Msg, State) ->
    io:format("~p~n",[Msg]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
    State :: #state{}) -> term()).
terminate(_Reason, _State) ->
    ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
    Extra :: term()) ->
    {ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================

server1.erl

%%%-------------------------------------------------------------------
%%% @author fengshangjiong
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 29. 十二月 2020 9:04
%%%-------------------------------------------------------------------
-module(server1).
-author("fengshangjiong").

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
    handle_call/3,
    handle_cast/2,
    handle_info/2,
    terminate/2,
    code_change/3]).

-export([
    start/0,
    stop/0
]).

-define(SERVER, ?MODULE).

-record(state, {}).

%%题目:在游戏中,有很多需要使用共享资源来处理的功能,这时候就需要单独管理进程来分配,
%% 并且按照请求有序执行资源分配,假设当前系统有X个资源,每个请求携带需要占用Y个资源,占用耗时Z秒
%% 1) 使用gen_server实现该管理进程
%% 2) 请求资源消息, 分配资源, 资源不足时, 返回失败, 耗时结束时释放资源
%% 3) 取消资源消息, 立马释放资源

%%%===================================================================
%%% API
%%%===================================================================

start() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
stop() ->
    gen_server:call(?MODULE,stop).





%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @end
%%--------------------------------------------------------------------

start_link() ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%%%===================================================================
%%% gen_server callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore |
%%                     {stop, Reason}
%% @end
%%--------------------------------------------------------------------

init([]) ->
    {ok, ets:new(?MODULE, [set, named_table])}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @end
%%--------------------------------------------------------------------


handle_call({apply_resourse,Req_Resources_Amount, Req_time, Name}, _From, Tab) ->
    %%第一次进来,ets里没存系统资源有多少,就会存一下,后面进来就有系统资源数据数据了 则不存
    case ets:member(Tab,admin) of
        false ->
            ets:insert(Tab,{admin,1000});
        _ ->
            ok
    end,
    %%读取系统资源
    [{_,Resources}] = ets:lookup(Tab, admin),
    Reply = case ets:lookup(Tab, Name) of
                [] when Resources >= Req_Resources_Amount  ->
                    ets:insert(Tab, {Name, Req_Resources_Amount}),
                    NewResources = Resources - Req_Resources_Amount,
%%                    ets:delete(Tab,admin),
                    ets:insert(Tab, {admin, NewResources}),
                    erlang:send_after(Req_time, self(), {realse, Name}),
                    {distribution_success};
                [] ->
                    {resourse_not_enough};
                [_] ->
                    {already_request}
            end,
    {reply, Reply, Tab};

handle_call({realse,Name}, _From, Tab) ->
    Reply = do(Tab, Name),
    Name ! {realse_success},
    {reply, Reply, Tab};

handle_call({req_cancel_early, Pid}, _From, Tab) ->
%%    io:format("ccccccccc"),
    Reply = do(Tab, Pid),
%%    io:format("xxxxxxxxxxxx"),
    {reply, Reply, Tab};

handle_call(stop, _From, Tab) ->
    {stop, normal, stopped, Tab}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @end
%%--------------------------------------------------------------------

handle_cast(_Request, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%%                                   {noreply, State, Timeout} |
%%                                   {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------

handle_info({realse,Name}, Tab) ->
    Reply = do(Tab, Name),
    Name ! {realse_success},
    {noreply, Tab}.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, Tab) ->
    io:format("im over  ~p~n",[_Reason]),
    ok.

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #state{},
    Extra :: term()) ->
    {ok, NewState :: #state{}} | {error, Reason :: term()}).
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================


do(Tab, Pid) ->
    case ets:lookup(Tab, Pid) of
        [_] ->
            [{_,Resources}] = ets:lookup(Tab, admin),
            [{_,Release_Resources}] = ets:lookup(Tab, Pid),
            NewResources = Resources + Release_Resources,
            ets:delete(Tab, Pid),
%%            ets:delete(Tab,admin),
            ets:insert(Tab, {admin, NewResources}),
            {realse_success};
        [] ->
            %%可能被客户端提前取消,即使定时器未关闭,
            %% 等定时器到点后也会走这里在ets中找不到数据返回
            {already_realse}
    end.
上一篇:javaweb遇到的问题


下一篇:浅谈Java中的深拷贝和浅拷贝(转载)