[nginx] Stream: the $session_time variable.

Vladimir Homutov vl at nginx.com
Fri Aug 26 12:34:58 UTC 2016


details:   http://hg.nginx.org/nginx/rev/164a0824ce20
branches:  
changeset: 6669:164a0824ce20
user:      Vladimir Homutov <vl at nginx.com>
date:      Fri Aug 26 15:33:04 2016 +0300
description:
Stream: the $session_time variable.

The variable keeps time spent on processing the stream session.

diffstat:

 src/stream/ngx_stream.h           |   2 ++
 src/stream/ngx_stream_handler.c   |   5 +++++
 src/stream/ngx_stream_variables.c |  34 ++++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 0 deletions(-)

diffs (92 lines):

diff -r 5e2821c2de46 -r 164a0824ce20 src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h	Fri Aug 26 15:33:02 2016 +0300
+++ b/src/stream/ngx_stream.h	Fri Aug 26 15:33:04 2016 +0300
@@ -155,6 +155,8 @@ struct ngx_stream_session_s {
     ngx_connection_t              *connection;
 
     off_t                          received;
+    time_t                         start_sec;
+    ngx_msec_t                     start_msec;
 
     ngx_log_handler_pt             log_handler;
 
diff -r 5e2821c2de46 -r 164a0824ce20 src/stream/ngx_stream_handler.c
--- a/src/stream/ngx_stream_handler.c	Fri Aug 26 15:33:02 2016 +0300
+++ b/src/stream/ngx_stream_handler.c	Fri Aug 26 15:33:04 2016 +0300
@@ -28,6 +28,7 @@ ngx_stream_init_connection(ngx_connectio
     size_t                        len;
     ngx_int_t                     rc;
     ngx_uint_t                    i;
+    ngx_time_t                   *tp;
     struct sockaddr              *sa;
     ngx_stream_port_t            *port;
     struct sockaddr_in           *sin;
@@ -158,6 +159,10 @@ ngx_stream_init_connection(ngx_connectio
         return;
     }
 
+    tp = ngx_timeofday();
+    s->start_sec = tp->sec;
+    s->start_msec = tp->msec;
+
     if (cmcf->limit_conn_handler) {
         rc = cmcf->limit_conn_handler(s);
 
diff -r 5e2821c2de46 -r 164a0824ce20 src/stream/ngx_stream_variables.c
--- a/src/stream/ngx_stream_variables.c	Fri Aug 26 15:33:02 2016 +0300
+++ b/src/stream/ngx_stream_variables.c	Fri Aug 26 15:33:04 2016 +0300
@@ -23,6 +23,8 @@ static ngx_int_t ngx_stream_variable_ser
     ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_bytes(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_stream_variable_session_time(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_stream_variable_connection(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data);
 
@@ -63,6 +65,9 @@ static ngx_stream_variable_t  ngx_stream
     { ngx_string("bytes_received"), NULL, ngx_stream_variable_bytes,
       1, 0, 0 },
 
+    { ngx_string("session_time"), NULL, ngx_stream_variable_session_time,
+      0, NGX_STREAM_VAR_NOCACHEABLE, 0 },
+
     { ngx_string("connection"), NULL,
       ngx_stream_variable_connection, 0, 0, 0 },
 
@@ -491,6 +496,35 @@ ngx_stream_variable_bytes(ngx_stream_ses
 
 
 static ngx_int_t
+ngx_stream_variable_session_time(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
+{
+    u_char          *p;
+    ngx_time_t      *tp;
+    ngx_msec_int_t   ms;
+
+    p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4);
+    if (p == NULL) {
+        return NGX_ERROR;
+    }
+
+    tp = ngx_timeofday();
+
+    ms = (ngx_msec_int_t)
+             ((tp->sec - s->start_sec) * 1000 + (tp->msec - s->start_msec));
+    ms = ngx_max(ms, 0);
+
+    v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = p;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_stream_variable_connection(ngx_stream_session_t *s,
     ngx_stream_variable_value_t *v, uintptr_t data)
 {



More information about the nginx-devel mailing list