diff src/stream/ngx_stream_variables.c @ 6669:164a0824ce20

Stream: the $session_time variable. The variable keeps time spent on processing the stream session.
author Vladimir Homutov <vl@nginx.com>
date Fri, 26 Aug 2016 15:33:04 +0300
parents 5e2821c2de46
children c6372a40c2a7
line wrap: on
line diff
--- a/src/stream/ngx_stream_variables.c
+++ b/src/stream/ngx_stream_variables.c
@@ -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)
 {