Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

pgsql recorder: record a close before a connect if necessary (TSUN-190)

  • Loading branch information...
commit 86d7177521ae2d76d148b0f7a4e9a4633a533c7e 1 parent 4583071
Nicolas Niclausse authored September 07, 2011
1  include/ts_recorder.hrl
@@ -10,6 +10,7 @@
10 10
                     prev_host,   % previous hostname
11 11
                     timestamp=0, % last request date
12 12
                     ext_file_id, % counter of external files (use for ex. in HTTP POST req)
  13
+                    plugin_state, % can be used by the plugin to store some state
13 14
                     plugin,
14 15
                     thinktime_low = 1000 % dot not record thinktime less than this
15 16
                                                 % value (msec)
5  src/tsung_recorder/ts_client_proxy.erl
@@ -148,10 +148,11 @@ handle_info({Msg,Socket},State=#proxy{http_version = HTTPVersion,
148 148
             {noreply, State#proxy{serversock=undefined}, ?lifetime}
149 149
     end;
150 150
 
151  
-handle_info({Msg, _Socket}, State) when Msg == tcp_closed;
  151
+handle_info({Msg, Socket}, State=#proxy{plugin=Plugin}) when Msg == tcp_closed;
152 152
                                        Msg == ssl_closed->
153 153
     ?LOG("socket closed by client~n",?INFO),
154  
-    {stop, normal, State};
  154
+    NewState = Plugin:client_close(Socket, State),
  155
+    {stop, normal, NewState};
155 156
 
156 157
 % Log properly who caused an error, and exit.
157 158
 handle_info({Msg, Socket, Reason}, State) when Msg == tcp_error;
7  src/tsung_recorder/ts_proxy_http.erl
@@ -35,6 +35,7 @@
35 35
 -export([parse/4, record_request/2, socket_opts/0]).
36 36
 -export([decode_basic_auth/1, gettype/0]).
37 37
 
  38
+-export([client_close/2]).
38 39
 -export([rewrite_serverdata/1]).
39 40
 -export([rewrite_ssl/1]).
40 41
 
@@ -66,6 +67,12 @@ rewrite_ssl(Data)->
66 67
     ts_utils:to_https(Data).
67 68
 
68 69
 %%--------------------------------------------------------------------
  70
+%% Func: client_close/2
  71
+%%--------------------------------------------------------------------
  72
+client_close(_Socket,State)->
  73
+    State.
  74
+
  75
+%%--------------------------------------------------------------------
69 76
 %% Func: parse/4
70 77
 %% Purpose: parse HTTP request
71 78
 %% Returns: {ok, NewState}
23  src/tsung_recorder/ts_proxy_pgsql.erl
@@ -33,6 +33,7 @@
33 33
 
34 34
 
35 35
 -export([parse/4, record_request/2, socket_opts/0, gettype/0]).
  36
+-export([client_close/2]).
36 37
 
37 38
 -export([rewrite_serverdata/1]).
38 39
 -export([rewrite_ssl/1]).
@@ -58,6 +59,13 @@ rewrite_serverdata(Data)->{ok, Data}.
58 59
 rewrite_ssl(Data)->{ok, Data}.
59 60
 
60 61
 %%--------------------------------------------------------------------
  62
+%% Func: client_close/2
  63
+%%--------------------------------------------------------------------
  64
+client_close(_Socket,State)->
  65
+    ts_proxy_recorder:dorecord({#pgsql_request{type=close}}),
  66
+    State.
  67
+
  68
+%%--------------------------------------------------------------------
61 69
 %% Func: parse/4
62 70
 %% Purpose: parse PGSQL request
63 71
 %% Returns: {ok, NewState}
@@ -285,18 +293,29 @@ do_split(<<H:1/binary,Tail/binary>>,Pattern,Head,L) ->
285 293
 %% Purpose: record request given State=#state_rec and Request=#pgsql_request
286 294
 %% Returns: {ok, NewState}
287 295
 %%--------------------------------------------------------------------
288  
-record_request(State=#state_rec{logfd=Fd},
  296
+record_request(State=#state_rec{logfd=Fd, plugin_state=connected},
289 297
                #pgsql_request{type=connect, username=User, database=DB})->
  298
+    %% connect request while already connected
  299
+    ?LOG("PGSQL: connect request but we are already connected ! record a close request first ~n", ?WARN),
  300
+    io:format(Fd,"<!-- close forced by Tsung; maybe several clients were using the recorder ?-->~n <request><pgsql type='close'/></request>~n", []),
290 301
     io:format(Fd,"<request><pgsql type='connect' database='~s' username='~s'/>", [DB,User]),
291 302
     io:format(Fd,"</request>~n",[]),
292 303
     {ok,State};
  304
+record_request(State=#state_rec{logfd=Fd},
  305
+               #pgsql_request{type=connect, username=User, database=DB})->
  306
+    io:format(Fd,"<request><pgsql type='connect' database='~s' username='~s'/>", [DB,User]),
  307
+    io:format(Fd,"</request>~n",[]),
  308
+    {ok,State#state_rec{plugin_state=connected }};
293 309
 record_request(State=#state_rec{logfd=Fd}, #pgsql_request{type=sql, sql=SQL})->
294 310
     io:format(Fd,"<request> <pgsql type='sql'><![CDATA[~s]]></pgsql>", [SQL]),
295 311
     io:format(Fd,"</request>~n",[]),
296 312
     {ok,State};
  313
+record_request(State=#state_rec{plugin_state=undefined}, #pgsql_request{type=close})->
  314
+    %% not connected, don't record
  315
+    {ok,State};
297 316
 record_request(State=#state_rec{logfd=Fd}, #pgsql_request{type=close})->
298 317
     io:format(Fd,"<request><pgsql type='close'/></request>~n", []),
299  
-    {ok,State};
  318
+    {ok,State#state_rec{plugin_state=undefined}};
300 319
 record_request(State=#state_rec{logfd=Fd}, #pgsql_request{type=sync})->
301 320
     io:format(Fd,"<request><pgsql type='sync'/></request>~n", []),
302 321
     {ok,State};
3  src/tsung_recorder/ts_proxy_recorder.erl
@@ -147,6 +147,9 @@ handle_cast({record, endsession}, State) ->
147 147
     io:format(State#state_rec.logfd,"</session>"),
148 148
     {noreply, State};
149 149
 
  150
+handle_cast({record, {Request,PluginState}}, State) ->
  151
+    handle_cast({record, {Request}}, State#state_rec{plugin_state=PluginState});
  152
+
150 153
 handle_cast({record, {Request}}, State=#state_rec{timestamp=0,plugin=Plugin}) -> % first record
151 154
     Name= ts_utils:datestr(),
152 155
     Type = Plugin:gettype(),
7  src/tsung_recorder/ts_proxy_webdav.erl
@@ -35,6 +35,7 @@
35 35
 -export([parse/4, record_request/2, socket_opts/0]).
36 36
 -export([gettype/0]).
37 37
 
  38
+-export([client_close/2]).
38 39
 -export([rewrite_serverdata/1]).
39 40
 -export([rewrite_ssl/1]).
40 41
 
@@ -61,6 +62,12 @@ rewrite_ssl(Data)->
61 62
     ts_utils:to_https(Data).
62 63
 
63 64
 %%--------------------------------------------------------------------
  65
+%% Func: client_close/2
  66
+%%--------------------------------------------------------------------
  67
+client_close(Data,State)->
  68
+    ts_proxy_http:client_close(Data,State).
  69
+
  70
+%%--------------------------------------------------------------------
64 71
 %% Func: parse/4
65 72
 %% Purpose: parse HTTP/WEBDAV request
66 73
 %% Returns: {ok, NewState}

0 notes on commit 86d7177

Please sign in to comment.
Something went wrong with that request. Please try again.