Permalink
Browse files

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

  • Loading branch information...
1 parent 4583071 commit 86d7177521ae2d76d148b0f7a4e9a4633a533c7e @nniclausse nniclausse committed Sep 7, 2011
View
@@ -10,6 +10,7 @@
prev_host, % previous hostname
timestamp=0, % last request date
ext_file_id, % counter of external files (use for ex. in HTTP POST req)
+ plugin_state, % can be used by the plugin to store some state
plugin,
thinktime_low = 1000 % dot not record thinktime less than this
% value (msec)
@@ -148,10 +148,11 @@ handle_info({Msg,Socket},State=#proxy{http_version = HTTPVersion,
{noreply, State#proxy{serversock=undefined}, ?lifetime}
end;
-handle_info({Msg, _Socket}, State) when Msg == tcp_closed;
+handle_info({Msg, Socket}, State=#proxy{plugin=Plugin}) when Msg == tcp_closed;
Msg == ssl_closed->
?LOG("socket closed by client~n",?INFO),
- {stop, normal, State};
+ NewState = Plugin:client_close(Socket, State),
+ {stop, normal, NewState};
% Log properly who caused an error, and exit.
handle_info({Msg, Socket, Reason}, State) when Msg == tcp_error;
@@ -35,6 +35,7 @@
-export([parse/4, record_request/2, socket_opts/0]).
-export([decode_basic_auth/1, gettype/0]).
+-export([client_close/2]).
-export([rewrite_serverdata/1]).
-export([rewrite_ssl/1]).
@@ -65,6 +66,12 @@ rewrite_ssl(Data)->
%% FIXME: content length may have changed !
ts_utils:to_https(Data).
+%%--------------------------------------------------------------------
+%% Func: client_close/2
+%%--------------------------------------------------------------------
+client_close(_Socket,State)->
+ State.
+
%%--------------------------------------------------------------------
%% Func: parse/4
%% Purpose: parse HTTP request
@@ -33,6 +33,7 @@
-export([parse/4, record_request/2, socket_opts/0, gettype/0]).
+-export([client_close/2]).
-export([rewrite_serverdata/1]).
-export([rewrite_ssl/1]).
@@ -57,6 +58,13 @@ rewrite_serverdata(Data)->{ok, Data}.
%%--------------------------------------------------------------------
rewrite_ssl(Data)->{ok, Data}.
+%%--------------------------------------------------------------------
+%% Func: client_close/2
+%%--------------------------------------------------------------------
+client_close(_Socket,State)->
+ ts_proxy_recorder:dorecord({#pgsql_request{type=close}}),
+ State.
+
%%--------------------------------------------------------------------
%% Func: parse/4
%% Purpose: parse PGSQL request
@@ -285,18 +293,29 @@ do_split(<<H:1/binary,Tail/binary>>,Pattern,Head,L) ->
%% Purpose: record request given State=#state_rec and Request=#pgsql_request
%% Returns: {ok, NewState}
%%--------------------------------------------------------------------
-record_request(State=#state_rec{logfd=Fd},
+record_request(State=#state_rec{logfd=Fd, plugin_state=connected},
#pgsql_request{type=connect, username=User, database=DB})->
+ %% connect request while already connected
+ ?LOG("PGSQL: connect request but we are already connected ! record a close request first ~n", ?WARN),
+ io:format(Fd,"<!-- close forced by Tsung; maybe several clients were using the recorder ?-->~n <request><pgsql type='close'/></request>~n", []),
io:format(Fd,"<request><pgsql type='connect' database='~s' username='~s'/>", [DB,User]),
io:format(Fd,"</request>~n",[]),
{ok,State};
+record_request(State=#state_rec{logfd=Fd},
+ #pgsql_request{type=connect, username=User, database=DB})->
+ io:format(Fd,"<request><pgsql type='connect' database='~s' username='~s'/>", [DB,User]),
+ io:format(Fd,"</request>~n",[]),
+ {ok,State#state_rec{plugin_state=connected }};
record_request(State=#state_rec{logfd=Fd}, #pgsql_request{type=sql, sql=SQL})->
io:format(Fd,"<request> <pgsql type='sql'><![CDATA[~s]]></pgsql>", [SQL]),
io:format(Fd,"</request>~n",[]),
{ok,State};
+record_request(State=#state_rec{plugin_state=undefined}, #pgsql_request{type=close})->
+ %% not connected, don't record
+ {ok,State};
record_request(State=#state_rec{logfd=Fd}, #pgsql_request{type=close})->
io:format(Fd,"<request><pgsql type='close'/></request>~n", []),
- {ok,State};
+ {ok,State#state_rec{plugin_state=undefined}};
record_request(State=#state_rec{logfd=Fd}, #pgsql_request{type=sync})->
io:format(Fd,"<request><pgsql type='sync'/></request>~n", []),
{ok,State};
@@ -147,6 +147,9 @@ handle_cast({record, endsession}, State) ->
io:format(State#state_rec.logfd,"</session>"),
{noreply, State};
+handle_cast({record, {Request,PluginState}}, State) ->
+ handle_cast({record, {Request}}, State#state_rec{plugin_state=PluginState});
+
handle_cast({record, {Request}}, State=#state_rec{timestamp=0,plugin=Plugin}) -> % first record
Name= ts_utils:datestr(),
Type = Plugin:gettype(),
@@ -35,6 +35,7 @@
-export([parse/4, record_request/2, socket_opts/0]).
-export([gettype/0]).
+-export([client_close/2]).
-export([rewrite_serverdata/1]).
-export([rewrite_ssl/1]).
@@ -60,6 +61,12 @@ rewrite_serverdata(Data)->
rewrite_ssl(Data)->
ts_utils:to_https(Data).
+%%--------------------------------------------------------------------
+%% Func: client_close/2
+%%--------------------------------------------------------------------
+client_close(Data,State)->
+ ts_proxy_http:client_close(Data,State).
+
%%--------------------------------------------------------------------
%% Func: parse/4
%% Purpose: parse HTTP/WEBDAV request

0 comments on commit 86d7177

Please sign in to comment.