Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_stream.h>
0011 
0012 
0013 typedef struct {
0014     uint32_t                      percent;
0015     ngx_stream_variable_value_t   value;
0016 } ngx_stream_split_clients_part_t;
0017 
0018 
0019 typedef struct {
0020     ngx_stream_complex_value_t    value;
0021     ngx_array_t                   parts;
0022 } ngx_stream_split_clients_ctx_t;
0023 
0024 
0025 static char *ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd,
0026     void *conf);
0027 static char *ngx_stream_split_clients(ngx_conf_t *cf, ngx_command_t *dummy,
0028     void *conf);
0029 
0030 static ngx_command_t  ngx_stream_split_clients_commands[] = {
0031 
0032     { ngx_string("split_clients"),
0033       NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE2,
0034       ngx_conf_split_clients_block,
0035       NGX_STREAM_MAIN_CONF_OFFSET,
0036       0,
0037       NULL },
0038 
0039       ngx_null_command
0040 };
0041 
0042 
0043 static ngx_stream_module_t  ngx_stream_split_clients_module_ctx = {
0044     NULL,                                  /* preconfiguration */
0045     NULL,                                  /* postconfiguration */
0046 
0047     NULL,                                  /* create main configuration */
0048     NULL,                                  /* init main configuration */
0049 
0050     NULL,                                  /* create server configuration */
0051     NULL                                   /* merge server configuration */
0052 };
0053 
0054 
0055 ngx_module_t  ngx_stream_split_clients_module = {
0056     NGX_MODULE_V1,
0057     &ngx_stream_split_clients_module_ctx,  /* module context */
0058     ngx_stream_split_clients_commands,     /* module directives */
0059     NGX_STREAM_MODULE,                     /* module type */
0060     NULL,                                  /* init master */
0061     NULL,                                  /* init module */
0062     NULL,                                  /* init process */
0063     NULL,                                  /* init thread */
0064     NULL,                                  /* exit thread */
0065     NULL,                                  /* exit process */
0066     NULL,                                  /* exit master */
0067     NGX_MODULE_V1_PADDING
0068 };
0069 
0070 
0071 static ngx_int_t
0072 ngx_stream_split_clients_variable(ngx_stream_session_t *s,
0073     ngx_stream_variable_value_t *v, uintptr_t data)
0074 {
0075     ngx_stream_split_clients_ctx_t *ctx =
0076                                        (ngx_stream_split_clients_ctx_t *) data;
0077 
0078     uint32_t                          hash;
0079     ngx_str_t                         val;
0080     ngx_uint_t                        i;
0081     ngx_stream_split_clients_part_t  *part;
0082 
0083     *v = ngx_stream_variable_null_value;
0084 
0085     if (ngx_stream_complex_value(s, &ctx->value, &val) != NGX_OK) {
0086         return NGX_OK;
0087     }
0088 
0089     hash = ngx_murmur_hash2(val.data, val.len);
0090 
0091     part = ctx->parts.elts;
0092 
0093     for (i = 0; i < ctx->parts.nelts; i++) {
0094 
0095         ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
0096                        "stream split: %uD %uD", hash, part[i].percent);
0097 
0098         if (hash < part[i].percent || part[i].percent == 0) {
0099             *v = part[i].value;
0100             return NGX_OK;
0101         }
0102     }
0103 
0104     return NGX_OK;
0105 }
0106 
0107 
0108 static char *
0109 ngx_conf_split_clients_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0110 {
0111     char                                *rv;
0112     uint32_t                             sum, last;
0113     ngx_str_t                           *value, name;
0114     ngx_uint_t                           i;
0115     ngx_conf_t                           save;
0116     ngx_stream_variable_t               *var;
0117     ngx_stream_split_clients_ctx_t      *ctx;
0118     ngx_stream_split_clients_part_t     *part;
0119     ngx_stream_compile_complex_value_t   ccv;
0120 
0121     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_split_clients_ctx_t));
0122     if (ctx == NULL) {
0123         return NGX_CONF_ERROR;
0124     }
0125 
0126     value = cf->args->elts;
0127 
0128     ngx_memzero(&ccv, sizeof(ngx_stream_compile_complex_value_t));
0129 
0130     ccv.cf = cf;
0131     ccv.value = &value[1];
0132     ccv.complex_value = &ctx->value;
0133 
0134     if (ngx_stream_compile_complex_value(&ccv) != NGX_OK) {
0135         return NGX_CONF_ERROR;
0136     }
0137 
0138     name = value[2];
0139 
0140     if (name.data[0] != '$') {
0141         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0142                            "invalid variable name \"%V\"", &name);
0143         return NGX_CONF_ERROR;
0144     }
0145 
0146     name.len--;
0147     name.data++;
0148 
0149     var = ngx_stream_add_variable(cf, &name, NGX_STREAM_VAR_CHANGEABLE);
0150     if (var == NULL) {
0151         return NGX_CONF_ERROR;
0152     }
0153 
0154     var->get_handler = ngx_stream_split_clients_variable;
0155     var->data = (uintptr_t) ctx;
0156 
0157     if (ngx_array_init(&ctx->parts, cf->pool, 2,
0158                        sizeof(ngx_stream_split_clients_part_t))
0159         != NGX_OK)
0160     {
0161         return NGX_CONF_ERROR;
0162     }
0163 
0164     save = *cf;
0165     cf->ctx = ctx;
0166     cf->handler = ngx_stream_split_clients;
0167     cf->handler_conf = conf;
0168 
0169     rv = ngx_conf_parse(cf, NULL);
0170 
0171     *cf = save;
0172 
0173     if (rv != NGX_CONF_OK) {
0174         return rv;
0175     }
0176 
0177     sum = 0;
0178     last = 0;
0179     part = ctx->parts.elts;
0180 
0181     for (i = 0; i < ctx->parts.nelts; i++) {
0182         sum = part[i].percent ? sum + part[i].percent : 10000;
0183         if (sum > 10000) {
0184             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0185                                "percent total is greater than 100%%");
0186             return NGX_CONF_ERROR;
0187         }
0188 
0189         if (part[i].percent) {
0190             last += part[i].percent * (uint64_t) 0xffffffff / 10000;
0191             part[i].percent = last;
0192         }
0193     }
0194 
0195     return rv;
0196 }
0197 
0198 
0199 static char *
0200 ngx_stream_split_clients(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
0201 {
0202     ngx_int_t                         n;
0203     ngx_str_t                        *value;
0204     ngx_stream_split_clients_ctx_t   *ctx;
0205     ngx_stream_split_clients_part_t  *part;
0206 
0207     ctx = cf->ctx;
0208     value = cf->args->elts;
0209 
0210     part = ngx_array_push(&ctx->parts);
0211     if (part == NULL) {
0212         return NGX_CONF_ERROR;
0213     }
0214 
0215     if (value[0].len == 1 && value[0].data[0] == '*') {
0216         part->percent = 0;
0217 
0218     } else {
0219         if (value[0].len == 0 || value[0].data[value[0].len - 1] != '%') {
0220             goto invalid;
0221         }
0222 
0223         n = ngx_atofp(value[0].data, value[0].len - 1, 2);
0224         if (n == NGX_ERROR || n == 0) {
0225             goto invalid;
0226         }
0227 
0228         part->percent = (uint32_t) n;
0229     }
0230 
0231     part->value.len = value[1].len;
0232     part->value.valid = 1;
0233     part->value.no_cacheable = 0;
0234     part->value.not_found = 0;
0235     part->value.data = value[1].data;
0236 
0237     return NGX_CONF_OK;
0238 
0239 invalid:
0240 
0241     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0242                        "invalid percent value \"%V\"", &value[0]);
0243     return NGX_CONF_ERROR;
0244 }