changeset 6632:787dcc15b802

Stream: split_clients module.
author Vladimir Homutov <vl@nginx.com>
date Tue, 12 Jul 2016 17:34:52 +0300
parents 80875b75d27e
children d82b3c344e7e
files auto/modules auto/options src/stream/ngx_stream_split_clients_module.c
diffstat 3 files changed, 60 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/auto/modules
+++ b/auto/modules
@@ -1054,6 +1054,16 @@ if [ $STREAM != NO ]; then
         . auto/module
     fi
 
+    if [ $STREAM_SPLIT_CLIENTS = YES ]; then
+        ngx_module_name=ngx_stream_split_clients_module
+        ngx_module_deps=
+        ngx_module_srcs=src/stream/ngx_stream_split_clients_module.c
+        ngx_module_libs=
+        ngx_module_link=$STREAM_SPLIT_CLIENTS
+
+        . auto/module
+    fi
+
     if [ $STREAM_RETURN = YES ]; then
         ngx_module_name=ngx_stream_return_module
         ngx_module_deps=
--- a/auto/options
+++ b/auto/options
@@ -120,6 +120,7 @@ STREAM_ACCESS=YES
 STREAM_GEO=YES
 STREAM_GEOIP=NO
 STREAM_MAP=YES
+STREAM_SPLIT_CLIENTS=YES
 STREAM_RETURN=YES
 STREAM_UPSTREAM_HASH=YES
 STREAM_UPSTREAM_LEAST_CONN=YES
@@ -303,6 +304,8 @@ use the \"--with-mail_ssl_module\" optio
         --without-stream_access_module)  STREAM_ACCESS=NO           ;;
         --without-stream_geo_module)     STREAM_GEO=NO              ;;
         --without-stream_map_module)     STREAM_MAP=NO              ;;
+        --without-stream_split_clients_module)
+                                         STREAM_SPLIT_CLIENTS=NO    ;;
         --without-stream_return_module)  STREAM_RETURN=NO           ;;
         --without-stream_upstream_hash_module)
                                          STREAM_UPSTREAM_HASH=NO    ;;
@@ -506,6 +509,8 @@ cat << END
   --without-stream_access_module     disable ngx_stream_access_module
   --without-stream_geo_module        disable ngx_stream_geo_module
   --without-stream_map_module        disable ngx_stream_map_module
+  --without-stream_split_clients_module
+                                     disable ngx_stream_split_clients_module
   --without-stream_return_module     disable ngx_stream_return_module
   --without-stream_upstream_hash_module
                                      disable ngx_stream_upstream_hash_module
copy from src/http/modules/ngx_http_split_clients_module.c
copy to src/stream/ngx_stream_split_clients_module.c
--- a/src/http/modules/ngx_http_split_clients_module.c
+++ b/src/stream/ngx_stream_split_clients_module.c
@@ -7,32 +7,32 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_http.h>
+#include <ngx_stream.h>
 
 
 typedef struct {
-    uint32_t                    percent;
-    ngx_http_variable_value_t   value;
-} ngx_http_split_clients_part_t;
+    uint32_t                      percent;
+    ngx_stream_variable_value_t   value;
+} ngx_stream_split_clients_part_t;
 
 
 typedef struct {
-    ngx_http_complex_value_t    value;
-    ngx_array_t                 parts;
-} ngx_http_split_clients_ctx_t;
+    ngx_stream_complex_value_t    value;
+    ngx_array_t                   parts;
+} ngx_stream_split_clients_ctx_t;
 
 
 static char *ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
-static char *ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy,
+static char *ngx_stream_split_clients(ngx_conf_t *cf, ngx_command_t *dummy,
     void *conf);
 
-static ngx_command_t  ngx_http_split_clients_commands[] = {
+static ngx_command_t  ngx_stream_split_clients_commands[] = {
 
     { ngx_string("split_clients"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
+      NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
       ngx_conf_split_clients_block,
-      NGX_HTTP_MAIN_CONF_OFFSET,
+      NGX_STREAM_MAIN_CONF_OFFSET,
       0,
       NULL },
 
@@ -40,7 +40,7 @@ static ngx_command_t  ngx_http_split_cli
 };
 
 
-static ngx_http_module_t  ngx_http_split_clients_module_ctx = {
+static ngx_stream_module_t  ngx_stream_split_clients_module_ctx = {
     NULL,                                  /* preconfiguration */
     NULL,                                  /* postconfiguration */
 
@@ -48,18 +48,15 @@ static ngx_http_module_t  ngx_http_split
     NULL,                                  /* init main configuration */
 
     NULL,                                  /* create server configuration */
-    NULL,                                  /* merge server configuration */
-
-    NULL,                                  /* create location configuration */
-    NULL                                   /* merge location configuration */
+    NULL                                   /* merge server configuration */
 };
 
 
-ngx_module_t  ngx_http_split_clients_module = {
+ngx_module_t  ngx_stream_split_clients_module = {
     NGX_MODULE_V1,
-    &ngx_http_split_clients_module_ctx,    /* module context */
-    ngx_http_split_clients_commands,       /* module directives */
-    NGX_HTTP_MODULE,                       /* module type */
+    &ngx_stream_split_clients_module_ctx,  /* module context */
+    ngx_stream_split_clients_commands,     /* module directives */
+    NGX_STREAM_MODULE,                     /* module type */
     NULL,                                  /* init master */
     NULL,                                  /* init module */
     NULL,                                  /* init process */
@@ -72,19 +69,20 @@ ngx_module_t  ngx_http_split_clients_mod
 
 
 static ngx_int_t
-ngx_http_split_clients_variable(ngx_http_request_t *r,
-    ngx_http_variable_value_t *v, uintptr_t data)
+ngx_stream_split_clients_variable(ngx_stream_session_t *s,
+    ngx_stream_variable_value_t *v, uintptr_t data)
 {
-    ngx_http_split_clients_ctx_t *ctx = (ngx_http_split_clients_ctx_t *) data;
+    ngx_stream_split_clients_ctx_t *ctx =
+                                       (ngx_stream_split_clients_ctx_t *) data;
 
-    uint32_t                        hash;
-    ngx_str_t                       val;
-    ngx_uint_t                      i;
-    ngx_http_split_clients_part_t  *part;
+    uint32_t                          hash;
+    ngx_str_t                         val;
+    ngx_uint_t                        i;
+    ngx_stream_split_clients_part_t  *part;
 
-    *v = ngx_http_variable_null_value;
+    *v = ngx_stream_variable_null_value;
 
-    if (ngx_http_complex_value(r, &ctx->value, &val) != NGX_OK) {
+    if (ngx_stream_complex_value(s, &ctx->value, &val) != NGX_OK) {
         return NGX_OK;
     }
 
@@ -94,8 +92,8 @@ ngx_http_split_clients_variable(ngx_http
 
     for (i = 0; i < ctx->parts.nelts; i++) {
 
-        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
-                       "http split: %uD %uD", hash, part[i].percent);
+        ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
+                       "stream split: %uD %uD", hash, part[i].percent);
 
         if (hash < part[i].percent || part[i].percent == 0) {
             *v = part[i].value;
@@ -115,25 +113,25 @@ ngx_conf_split_clients_block(ngx_conf_t 
     ngx_str_t                           *value, name;
     ngx_uint_t                           i;
     ngx_conf_t                           save;
-    ngx_http_variable_t                 *var;
-    ngx_http_split_clients_ctx_t        *ctx;
-    ngx_http_split_clients_part_t       *part;
-    ngx_http_compile_complex_value_t     ccv;
+    ngx_stream_variable_t               *var;
+    ngx_stream_split_clients_ctx_t      *ctx;
+    ngx_stream_split_clients_part_t     *part;
+    ngx_stream_compile_complex_value_t   ccv;
 
-    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_split_clients_ctx_t));
+    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_split_clients_ctx_t));
     if (ctx == NULL) {
         return NGX_CONF_ERROR;
     }
 
     value = cf->args->elts;
 
-    ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+    ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t));
 
     ccv.cf = cf;
     ccv.value = &value[1];
     ccv.complex_value = &ctx->value;
 
-    if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+    if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) {
         return NGX_CONF_ERROR;
     }
 
@@ -148,16 +146,16 @@ ngx_conf_split_clients_block(ngx_conf_t 
     name.len--;
     name.data++;
 
-    var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
+    var = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE);
     if (var == NULL) {
         return NGX_CONF_ERROR;
     }
 
-    var->get_handler = ngx_http_split_clients_variable;
+    var->get_handler = ngx_stream_split_clients_variable;
     var->data = (uintptr_t) ctx;
 
     if (ngx_array_init(&ctx->parts, cf->pool, 2,
-                       sizeof(ngx_http_split_clients_part_t))
+                       sizeof(ngx_stream_split_clients_part_t))
         != NGX_OK)
     {
         return NGX_CONF_ERROR;
@@ -165,7 +163,7 @@ ngx_conf_split_clients_block(ngx_conf_t 
 
     save = *cf;
     cf->ctx = ctx;
-    cf->handler = ngx_http_split_clients;
+    cf->handler = ngx_stream_split_clients;
     cf->handler_conf = conf;
 
     rv = ngx_conf_parse(cf, NULL);
@@ -199,12 +197,12 @@ ngx_conf_split_clients_block(ngx_conf_t 
 
 
 static char *
-ngx_http_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
+ngx_stream_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
 {
-    ngx_int_t                       n;
-    ngx_str_t                      *value;
-    ngx_http_split_clients_ctx_t   *ctx;
-    ngx_http_split_clients_part_t  *part;
+    ngx_int_t                         n;
+    ngx_str_t                        *value;
+    ngx_stream_split_clients_ctx_t   *ctx;
+    ngx_stream_split_clients_part_t  *part;
 
     ctx = cf->ctx;
     value = cf->args->elts;