pooled_gs.erl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. %% @doc A gen_server module that can be pooled by pooler. This module
  2. %% is used to test and demo pooler and plays the role of the pooled
  3. %% member.
  4. %%
  5. %% A pooled_gs is started with a `Type' which is intended to help
  6. %% identify the members for testing multi-pool scenarios. Each
  7. %% pooled_gs also sets a unique id. The type and id are retrieved
  8. %% using `get_id/1'.
  9. -module(pooled_gs).
  10. -behaviour(gen_server).
  11. -define(SERVER, ?MODULE).
  12. %% ------------------------------------------------------------------
  13. %% API Function Exports
  14. %% ------------------------------------------------------------------
  15. -export([start_link/1,
  16. get_id/1,
  17. ping/1,
  18. ping_count/1,
  19. crash/1,
  20. do_work/2,
  21. stop/1
  22. ]).
  23. %% ------------------------------------------------------------------
  24. %% gen_server Function Exports
  25. %% ------------------------------------------------------------------
  26. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  27. terminate/2, code_change/3]).
  28. %% ------------------------------------------------------------------
  29. %% API Function Definitions
  30. %% ------------------------------------------------------------------
  31. start_link(Args ={_Type}) ->
  32. % not registered
  33. gen_server:start_link(?MODULE, Args, []).
  34. %% @doc return the type argument passed to this worker at start time
  35. %% along with this worker's unique id.
  36. get_id(S) ->
  37. gen_server:call(S, get_id).
  38. %% @doc In processing this request, the server will sleep for a time
  39. %% determined by a call to `random:uniform(T)'. Can be useful in
  40. %% stress-testing the pool to simulate consumers taking a member out
  41. %% of the pool for a varied request time.
  42. do_work(S, T) ->
  43. gen_server:call(S, {do_work, T}).
  44. ping(S) ->
  45. gen_server:call(S, ping).
  46. ping_count(S) ->
  47. gen_server:call(S, ping_count).
  48. crash(S) ->
  49. gen_server:cast(S, crash),
  50. sent_crash_request.
  51. stop(S) ->
  52. gen_server:call(S, stop).
  53. %% ------------------------------------------------------------------
  54. %% gen_server Function Definitions
  55. %% ------------------------------------------------------------------
  56. -record(state, {
  57. type = "",
  58. id,
  59. ping_count = 0
  60. }).
  61. init({Type}) ->
  62. {ok, #state{type = Type, id = make_ref()}}.
  63. handle_call(get_id, _From, State) ->
  64. {reply, {State#state.type, State#state.id}, State};
  65. handle_call({do_work, T}, _From, State) ->
  66. Sleep = random:uniform(T),
  67. timer:sleep(Sleep),
  68. {reply, {ok, Sleep}, State};
  69. handle_call(ping, _From, #state{ping_count = C } = State) ->
  70. State1 = State#state{ping_count = C + 1},
  71. {reply, pong, State1};
  72. handle_call(ping_count, _From, #state{ping_count = C } = State) ->
  73. {reply, C, State};
  74. handle_call(stop, _From, State) ->
  75. {stop, normal, stop_ok, State};
  76. handle_call(_Request, _From, State) ->
  77. {noreply, ok, State}.
  78. handle_cast(crash, _State) ->
  79. erlang:error({pooled_gs, requested_crash});
  80. handle_cast(_Msg, State) ->
  81. {noreply, State}.
  82. handle_info(_Info, State) ->
  83. {noreply, State}.
  84. terminate(_Reason, _State) ->
  85. ok.
  86. code_change(_OldVsn, State, _Extra) ->
  87. {ok, State}.