Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 204 lines (179 sloc) 8.296 kb
5d10a86f » nniclausse
2004-08-10 add explicit exception in the GPL for linking EPL modules (obvious, b…
1 %%% This program is free software; you can redistribute it and/or modify
2 %%% it under the terms of the GNU General Public License as published by
3 %%% the Free Software Foundation; either version 2 of the License, or
4 %%% (at your option) any later version.
5 %%%
6 %%% This program is distributed in the hope that it will be useful,
7 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
8 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 %%% GNU General Public License for more details.
10 %%%
11 %%% You should have received a copy of the GNU General Public License
12 %%% along with this program; if not, write to the Free Software
13 %%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 %%%
15 %%% In addition, as a special exception, you have the permission to
16 %%% link the code of this program with any library released under
17 %%% the EPL license and distribute linked combinations including
18 %%% the two.
19
764c3cbb » nniclausse
2003-12-03 add cache for sessions
20 %%%-------------------------------------------------------------------
21 %%% File : ts_session_cache.erl
22 %%% Author : Nicolas Niclausse <nniclausse@schultze.ird.idealx.com>
8ac455b4 » nniclausse
2004-01-15 -add function for Debug message only (not executed unless compiled wi…
23 %%% Description : cache sessions request from ts_config_server
764c3cbb » nniclausse
2003-12-03 add cache for sessions
24 %%%
41e965d3 » nniclausse
2005-11-27 update email
25 %%% Created : 2 Dec 2003 by Nicolas Niclausse <nicolas@niclux.org>
764c3cbb » nniclausse
2003-12-03 add cache for sessions
26 %%%-------------------------------------------------------------------
5d10a86f » nniclausse
2004-08-10 add explicit exception in the GPL for linking EPL modules (obvious, b…
27
28
764c3cbb » nniclausse
2003-12-03 add cache for sessions
29 -module(ts_session_cache).
30
31 -behaviour(gen_server).
32 %%--------------------------------------------------------------------
33 %% Include files
34 %%--------------------------------------------------------------------
35
36 %%--------------------------------------------------------------------
37 %% External exports
5596ca17 » nniclausse
2008-11-26 refactoring: use a separate process for caching stats (ts_mon_cache)
38 -export([start/0, get_req/2, get_user_agent/0]).
764c3cbb » nniclausse
2003-12-03 add cache for sessions
39
40 %% gen_server callbacks
692924a9 » nniclausse
2005-09-13 big performance improvment for very high load (>10k req/s): use the s…
41 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
4109590a » nniclausse
2007-04-27 minor: update comments
42 code_change/3]).
764c3cbb » nniclausse
2003-12-03 add cache for sessions
43
44
45 -record(state, {
46 table, % ets table
47 hit =0.0, % number of hits
5596ca17 » nniclausse
2008-11-26 refactoring: use a separate process for caching stats (ts_mon_cache)
48 total=0.0 % total number of requests
764c3cbb » nniclausse
2003-12-03 add cache for sessions
49 }).
50
4cec511b » nniclausse
2008-11-18 make edoc happy (TSUN-12)
51 -define(DUMP_STATS_INTERVAL, 500). % in milliseconds
692924a9 » nniclausse
2005-09-13 big performance improvment for very high load (>10k req/s): use the s…
52
583ea5e3 » nniclausse
2012-08-22 put macros in a separated header file; add NOW macro
53 -include("ts_macros.hrl").
764c3cbb » nniclausse
2003-12-03 add cache for sessions
54
55 %%====================================================================
56 %% External functions
57 %%====================================================================
58 %%--------------------------------------------------------------------
59 %% Function: start_link/0
60 %% Description: Starts the server
61 %%--------------------------------------------------------------------
62 start() ->
8ac455b4 » nniclausse
2004-01-15 -add function for Debug message only (not executed unless compiled wi…
63 ?LOG("Starting~n",?INFO),
764c3cbb » nniclausse
2003-12-03 add cache for sessions
64 gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
65
4109590a » nniclausse
2007-04-27 minor: update comments
66 %%--------------------------------------------------------------------
67 %% Function: get_req/2
68 %% Description: get next request from session 'Id'
69 %%--------------------------------------------------------------------
764c3cbb » nniclausse
2003-12-03 add cache for sessions
70 get_req(Id, Count)->
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
71 gen_server:call(?MODULE,{get_req, Id, Count}).
764c3cbb » nniclausse
2003-12-03 add cache for sessions
72
4109590a » nniclausse
2007-04-27 minor: update comments
73 %%--------------------------------------------------------------------
74 %% Function: get_user_agent/0
75 %%--------------------------------------------------------------------
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
76 get_user_agent()->
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
77 gen_server:call(?MODULE,{get_user_agent}).
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
78
764c3cbb » nniclausse
2003-12-03 add cache for sessions
79 %%====================================================================
80 %% Server functions
81 %%====================================================================
82
83 %%--------------------------------------------------------------------
84 %% Function: init/1
85 %% Description: Initiates the server
86 %% Returns: {ok, State} |
87 %% {ok, State, Timeout} |
88 %% ignore |
89 %% {stop, Reason}
90 %%--------------------------------------------------------------------
91 init([]) ->
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
92 Table = ets:new(sessiontable, [set, private]),
764c3cbb » nniclausse
2003-12-03 add cache for sessions
93 {ok, #state{table=Table}}.
94
95 %%--------------------------------------------------------------------
96 %% Function: handle_call/3
97 %% Description: Handling call messages
98 %% Returns: {reply, Reply, State} |
99 %% {reply, Reply, State, Timeout} |
100 %% {noreply, State} |
101 %% {noreply, State, Timeout} |
102 %% {stop, Reason, Reply, State} | (terminate/2 is called)
103 %% {stop, Reason, State} (terminate/2 is called)
104 %%--------------------------------------------------------------------
105 %% get Nth request from given session Id
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
106 handle_call({get_req, Id, N}, _From, State) ->
107 Tab = State#state.table,
764c3cbb » nniclausse
2003-12-03 add cache for sessions
108 Total = State#state.total+1,
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
109 ?DebugF("look for ~p th request in session ~p for ~p~n",[N,Id,_From]),
110 case ets:lookup(Tab, {Id, N}) of
111 [{_Key, Session}] ->
764c3cbb » nniclausse
2003-12-03 add cache for sessions
112 Hit = State#state.hit+1,
6c17cc92 » nniclausse
2007-10-23 fix: make it compile with debug_info
113 ?DebugF("ok, found in cache for ~p~n",[_From]),
8ac455b4 » nniclausse
2004-01-15 -add function for Debug message only (not executed unless compiled wi…
114 ?DebugF("hitrate is ~.3f~n",[100.0*Hit/Total]),
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
115 {reply, Session, State#state{hit= Hit, total = Total}};
116 [] -> %% no match, ask the config_server
117 ?DebugF("not found in cache (~p th request in session ~p for ~p)~n",[N,Id,_From]),
5f570de3 » nniclausse
2005-09-19 use catch
118 case catch ts_config_server:get_req(Id, N) of
119 {'EXIT',Reason} ->
120 {reply, {error, Reason}, State};
121 Reply ->
122 %% cache the response FIXME: handle bad response ?
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
123 ets:insert(Tab, {{Id, N}, Reply}),
5f570de3 » nniclausse
2005-09-19 use catch
124 {reply, Reply, State#state{total = Total}}
125 end;
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
126 Other -> %%
8ac455b4 » nniclausse
2004-01-15 -add function for Debug message only (not executed unless compiled wi…
127 ?LOGF("error ! (~p)~n",[Other],?WARN),
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
128 {reply, {error, Other}, State}
129 end;
764c3cbb » nniclausse
2003-12-03 add cache for sessions
130
1eca127c » nniclausse
2005-12-04 remove warnings
131 handle_call({get_user_agent}, _From, State) ->
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
132 Tab = State#state.table,
133 case ets:lookup(Tab, {http_user_agent, value}) of
134 [] -> %% no match, ask the config_server
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
135 ?Debug("user agents not found in cache~n"),
136 UserAgents = ts_config_server:get_user_agents(),
137 %% cache the response FIXME: handle bad response ?
138 ?DebugF("Useragents: got from config_server~p~n",[UserAgents]),
139 ets:insert(Tab, {{http_user_agent, value}, UserAgents}),
140 {ok, Reply} = choose_user_agent(UserAgents),
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
141 {reply, Reply, State};
142 [{_, [{_Freq, Value}]}] -> %single user agent defined
143 {reply, Value, State};
144 [{_, empty }] ->
145 {reply, "tsung", State};
146 [{_, UserAgents }] when is_list(UserAgents)->
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
147 {ok, Reply} = choose_user_agent(UserAgents),
c49fb0d0 » nniclausse
2007-08-24 remove tabs and useless spaces
148 {reply, Reply, State}
149 end;
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
150
e3512806 » nniclausse
2004-10-14 avoid warnings for unused variables
151 handle_call(_Request, _From, State) ->
764c3cbb » nniclausse
2003-12-03 add cache for sessions
152 Reply = ok,
153 {reply, Reply, State}.
154
155 %%--------------------------------------------------------------------
156 %% Function: handle_cast/2
157 %% Description: Handling cast messages
158 %% Returns: {noreply, State} |
159 %% {noreply, State, Timeout} |
160 %% {stop, Reason, State} (terminate/2 is called)
161 %%--------------------------------------------------------------------
e3512806 » nniclausse
2004-10-14 avoid warnings for unused variables
162 handle_cast(_Msg, State) ->
764c3cbb » nniclausse
2003-12-03 add cache for sessions
163 {noreply, State}.
164
165 %%--------------------------------------------------------------------
166 %% Function: handle_info/2
167 %% Description: Handling all non call/cast messages
168 %% Returns: {noreply, State} |
169 %% {noreply, State, Timeout} |
170 %% {stop, Reason, State} (terminate/2 is called)
171 %%--------------------------------------------------------------------
e3512806 » nniclausse
2004-10-14 avoid warnings for unused variables
172 handle_info(_Info, State) ->
764c3cbb » nniclausse
2003-12-03 add cache for sessions
173 {noreply, State}.
174
175 %%--------------------------------------------------------------------
176 %% Function: terminate/2
177 %% Description: Shutdown the server
178 %% Returns: any (ignored by gen_server)
179 %%--------------------------------------------------------------------
e3512806 » nniclausse
2004-10-14 avoid warnings for unused variables
180 terminate(Reason, _State) ->
764c3cbb » nniclausse
2003-12-03 add cache for sessions
181 ?LOGF("Die ! (~p)~n",[Reason],?ERR),
182 ok.
183
184 %%--------------------------------------------------------------------
185 %% Func: code_change/3
186 %% Purpose: Convert process state when code is changed
187 %% Returns: {ok, NewState}
188 %%--------------------------------------------------------------------
e3512806 » nniclausse
2004-10-14 avoid warnings for unused variables
189 code_change(_OldVsn, State, _Extra) ->
764c3cbb » nniclausse
2003-12-03 add cache for sessions
190 {ok, State}.
191
192 %%--------------------------------------------------------------------
193 %%% Internal functions
194 %%--------------------------------------------------------------------
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
195
5345a3b5 » nniclausse
2005-11-27 rename idx-tsunami to tsung step 3
196 choose_user_agent(empty) -> {ok, "tsung"};
d395f88d » nniclausse
2005-10-09 fix single user agent case
197 choose_user_agent([{_P, Val}]) -> {ok, Val};
c640d11e » nniclausse
2005-07-11 http: user_agent is now customizable in the xml config file.
198 choose_user_agent(UserAgents) ->
199 choose_user_agent(UserAgents, random:uniform(100),0).
200
201 choose_user_agent([{P, Val} | _],Rand, Cur) when Rand =< P+Cur->
202 {ok, Val};
203 choose_user_agent([{P, _Val} | SList], Rand, Cur) ->
204 choose_user_agent(SList, Rand, Cur+P).
205
Something went wrong with that request. Please try again.