[nginx] Stream: proxy_requests directive.

Vladimir Homutov vl at nginx.com
Tue Nov 13 13:41:25 UTC 2018


details:   http://hg.nginx.org/nginx/rev/4698cede59ff
branches:  
changeset: 7393:4698cede59ff
user:      Vladimir Homutov <vl at nginx.com>
date:      Mon Nov 12 16:29:30 2018 +0300
description:
Stream: proxy_requests directive.

The directive allows to drop binding between a client and existing UDP stream
session after receiving a specified number of packets.  First packet from the
same client address and port will start a new session.  Old session continues
to exist and will terminate at moment defined by configuration: either after
receiving the expected number of responses, or after timeout, as specified by
the "proxy_responses" and/or "proxy_timeout" directives.

By default, proxy_requests is zero (disabled).

diffstat:

 src/event/ngx_event.h                |   1 +
 src/event/ngx_event_udp.c            |  15 +++++++++++++--
 src/stream/ngx_stream_proxy_module.c |  27 +++++++++++++++++++++++++--
 3 files changed, 39 insertions(+), 4 deletions(-)

diffs (127 lines):

diff -r 04ff25798002 -r 4698cede59ff src/event/ngx_event.h
--- a/src/event/ngx_event.h	Mon Nov 12 12:05:03 2018 +0300
+++ b/src/event/ngx_event.h	Mon Nov 12 16:29:30 2018 +0300
@@ -509,6 +509,7 @@ void ngx_event_recvmsg(ngx_event_t *ev);
 void ngx_udp_rbtree_insert_value(ngx_rbtree_node_t *temp,
     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
 #endif
+void ngx_delete_udp_connection(void *data);
 ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle);
 ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle);
 u_char *ngx_accept_log_error(ngx_log_t *log, u_char *buf, size_t len);
diff -r 04ff25798002 -r 4698cede59ff src/event/ngx_event_udp.c
--- a/src/event/ngx_event_udp.c	Mon Nov 12 12:05:03 2018 +0300
+++ b/src/event/ngx_event_udp.c	Mon Nov 12 16:29:30 2018 +0300
@@ -23,7 +23,6 @@ static void ngx_close_accepted_udp_conne
 static ssize_t ngx_udp_shared_recv(ngx_connection_t *c, u_char *buf,
     size_t size);
 static ngx_int_t ngx_insert_udp_connection(ngx_connection_t *c);
-static void ngx_delete_udp_connection(void *data);
 static ngx_connection_t *ngx_lookup_udp_connection(ngx_listening_t *ls,
     struct sockaddr *sockaddr, socklen_t socklen,
     struct sockaddr *local_sockaddr, socklen_t local_socklen);
@@ -558,11 +557,15 @@ ngx_insert_udp_connection(ngx_connection
 }
 
 
-static void
+void
 ngx_delete_udp_connection(void *data)
 {
     ngx_connection_t  *c = data;
 
+    if (c->udp == NULL) {
+        return;
+    }
+
     ngx_rbtree_delete(&c->listening->rbtree, &c->udp->node);
 
     c->udp = NULL;
@@ -643,4 +646,12 @@ ngx_lookup_udp_connection(ngx_listening_
     return NULL;
 }
 
+#else
+
+void
+ngx_delete_udp_connection(void *data)
+{
+    return;
+}
+
 #endif
diff -r 04ff25798002 -r 4698cede59ff src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Mon Nov 12 12:05:03 2018 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Mon Nov 12 16:29:30 2018 +0300
@@ -26,6 +26,7 @@ typedef struct {
     size_t                           buffer_size;
     size_t                           upload_rate;
     size_t                           download_rate;
+    ngx_uint_t                       requests;
     ngx_uint_t                       responses;
     ngx_uint_t                       next_upstream_tries;
     ngx_flag_t                       next_upstream;
@@ -195,6 +196,13 @@ static ngx_command_t  ngx_stream_proxy_c
       offsetof(ngx_stream_proxy_srv_conf_t, download_rate),
       NULL },
 
+    { ngx_string("proxy_requests"),
+      NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_num_slot,
+      NGX_STREAM_SRV_CONF_OFFSET,
+      offsetof(ngx_stream_proxy_srv_conf_t, requests),
+      NULL },
+
     { ngx_string("proxy_responses"),
       NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_num_slot,
@@ -1341,11 +1349,14 @@ ngx_stream_proxy_process_connection(ngx_
 
         } else {
             if (s->connection->type == SOCK_DGRAM) {
-                if (pscf->responses == NGX_MAX_INT32_VALUE) {
+
+                if (pscf->responses == NGX_MAX_INT32_VALUE
+                    || (u->responses >= pscf->responses * u->requests))
+                {
 
                     /*
                      * successfully terminate timed out UDP session
-                     * with unspecified number of responses
+                     * if expected number of responses was received
                      */
 
                     handler = c->log->handler;
@@ -1692,6 +1703,14 @@ ngx_stream_proxy_test_finalize(ngx_strea
 
     if (c->type == SOCK_DGRAM) {
 
+        if (pscf->requests && u->requests < pscf->requests) {
+            return NGX_DECLINED;
+        }
+
+        if (pscf->requests) {
+            ngx_delete_udp_connection(c);
+        }
+
         if (pscf->responses == NGX_MAX_INT32_VALUE
             || u->responses < pscf->responses * u->requests)
         {
@@ -1943,6 +1962,7 @@ ngx_stream_proxy_create_srv_conf(ngx_con
     conf->buffer_size = NGX_CONF_UNSET_SIZE;
     conf->upload_rate = NGX_CONF_UNSET_SIZE;
     conf->download_rate = NGX_CONF_UNSET_SIZE;
+    conf->requests = NGX_CONF_UNSET_UINT;
     conf->responses = NGX_CONF_UNSET_UINT;
     conf->next_upstream_tries = NGX_CONF_UNSET_UINT;
     conf->next_upstream = NGX_CONF_UNSET;
@@ -1987,6 +2007,9 @@ ngx_stream_proxy_merge_srv_conf(ngx_conf
     ngx_conf_merge_size_value(conf->download_rate,
                               prev->download_rate, 0);
 
+    ngx_conf_merge_uint_value(conf->requests,
+                              prev->requests, 0);
+
     ngx_conf_merge_uint_value(conf->responses,
                               prev->responses, NGX_MAX_INT32_VALUE);
 


More information about the nginx-devel mailing list