pooled_gs.erl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. error_on_call/1,
  21. do_work/2,
  22. stop/1
  23. ]).
  24. %% ------------------------------------------------------------------
  25. %% gen_server Function Exports
  26. %% ------------------------------------------------------------------
  27. -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
  28. terminate/2, code_change/3]).
  29. %% ------------------------------------------------------------------
  30. %% API Function Definitions
  31. %% ------------------------------------------------------------------
  32. start_link(Args ={_Type}) ->
  33. % not registered
  34. gen_server:start_link(?MODULE, Args, []);
  35. start_link(Args ={_Type, _InitFun}) ->
  36. % not registered
  37. gen_server:start_link(?MODULE, Args, []).
  38. %% @doc return the type argument passed to this worker at start time
  39. %% along with this worker's unique id.
  40. get_id(S) ->
  41. gen_server:call(S, get_id).
  42. %% @doc In processing this request, the server will sleep for a time
  43. %% determined by a call to `random:uniform(T)'. Can be useful in
  44. %% stress-testing the pool to simulate consumers taking a member out
  45. %% of the pool for a varied request time.
  46. do_work(S, T) ->
  47. gen_server:call(S, {do_work, T}).
  48. ping(S) ->
  49. gen_server:call(S, ping).
  50. ping_count(S) ->
  51. gen_server:call(S, ping_count).
  52. crash(S) ->
  53. gen_server:cast(S, crash),
  54. sent_crash_request.
  55. error_on_call(S) ->
  56. gen_server:call(S, error_on_call).
  57. stop(S) ->
  58. gen_server:call(S, stop).
  59. %% ------------------------------------------------------------------
  60. %% gen_server Function Definitions
  61. %% ------------------------------------------------------------------
  62. -record(state, {
  63. type = "" :: string(),
  64. id :: reference(),
  65. ping_count = 0 :: non_neg_integer()
  66. }).
  67. init({Type}) ->
  68. {ok, #state{type = Type, id = make_ref()}};
  69. init({Type, StartFun}) ->
  70. StartFun(),
  71. {ok, #state{type = Type, id = make_ref()}}.
  72. handle_call(get_id, _From, State) ->
  73. {reply, {State#state.type, State#state.id}, State};
  74. handle_call({do_work, T}, _From, State) ->
  75. Sleep = random:uniform(T),
  76. timer:sleep(Sleep),
  77. {reply, {ok, Sleep}, State};
  78. handle_call(ping, _From, #state{ping_count = C } = State) ->
  79. State1 = State#state{ping_count = C + 1},
  80. {reply, pong, State1};
  81. handle_call(ping_count, _From, #state{ping_count = C } = State) ->
  82. {reply, C, State};
  83. handle_call(error_on_call, _From, _State) ->
  84. erlang:error({pooled_gs, requested_error});
  85. handle_call(stop, _From, State) ->
  86. {stop, normal, stop_ok, State};
  87. handle_call(_Request, _From, State) ->
  88. {noreply, ok, State}.
  89. handle_cast(crash, _State) ->
  90. erlang:error({pooled_gs, requested_crash});
  91. handle_cast(_Msg, State) ->
  92. {noreply, State}.
  93. handle_info(_Info, State) ->
  94. {noreply, State}.
  95. terminate(_Reason, _State) ->
  96. ok.
  97. code_change(_OldVsn, State, _Extra) ->
  98. {ok, State}.