mirror of
https://github.com/nginx/nginx.git
synced 2025-08-01 02:56:15 +08:00
Stream: the $session_time variable.
The variable keeps time spent on processing the stream session.
This commit is contained in:
parent
1258126f0c
commit
f04b65358e
@ -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;
|
||||
|
||||
|
@ -28,6 +28,7 @@ ngx_stream_init_connection(ngx_connection_t *c)
|
||||
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_connection_t *c)
|
||||
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);
|
||||
|
||||
|
@ -23,6 +23,8 @@ static ngx_int_t ngx_stream_variable_server_port(ngx_stream_session_t *s,
|
||||
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_core_variables[] = {
|
||||
{ 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 },
|
||||
|
||||
@ -490,6 +495,35 @@ ngx_stream_variable_bytes(ngx_stream_session_t *s,
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user