1010#include <ngx_http.h>
1111#include <hiredis/hiredis.h>
1212
13-
1413u_char * redis_ip = NULL , * block_second ;
1514
15+ redisContext * c ;
16+ redisReply * reply ;
17+
1618typedef struct {
1719 u_char color ;
1820 u_char dummy ;
@@ -71,9 +73,9 @@ static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
7173static ngx_int_t ngx_http_limit_req_init (ngx_conf_t * cf );
7274
7375static ngx_conf_enum_t ngx_http_limit_req_log_levels [] = { { ngx_string ("info" ),
74- NGX_LOG_INFO }, { ngx_string ("notice" ), NGX_LOG_NOTICE }, { ngx_string (
76+ NGX_LOG_INFO }, { ngx_string ("notice" ), NGX_LOG_NOTICE }, { ngx_string (
7577 "warn" ), NGX_LOG_WARN }, { ngx_string ("error" ), NGX_LOG_ERR }, {
76- ngx_null_string , 0 } };
78+ ngx_null_string , 0 } };
7779
7880static ngx_conf_num_bounds_t ngx_http_limit_req_status_bounds = {
7981 ngx_conf_check_num_bounds , 400 , 599 };
@@ -152,19 +154,19 @@ static ngx_int_t ngx_http_limit_req_handler(ngx_http_request_t *r) {
152154 limit = NULL ;
153155#endif
154156
155- redisContext * c ;
156- redisReply * reply ;
157-
158157 char Host [256 ];
159158 struct timeval timeout = { 1 , 500000 }; // 1.5 seconds
160-
161- c = redisConnectWithTimeout ((char * ) redis_ip , 6379 , timeout );
159+ if (c == NULL ) {
160+ c = redisConnectWithTimeout ((char * ) redis_ip , 6379 , timeout );
161+ }
162162 if (c == NULL || c -> err ) {
163163 if (c ) {
164164 if (redis_ip ) {
165165 ngx_log_error (lrcf -> limit_log_level , r -> connection -> log , 0 ,
166166 "redis connection error: %s %s\n" , c -> errstr ,
167- redis_ip ? redis_ip : (u_char * )"[ No configuration of redis]" );
167+ redis_ip ?
168+ redis_ip :
169+ (u_char * )"[ No configuration of redis]" );
168170 }
169171 redisFree (c );
170172 /* Redis if the connection is wrong,
@@ -215,16 +217,16 @@ static ngx_int_t ngx_http_limit_req_handler(ngx_http_request_t *r) {
215217 "limit_req[%ui]: %i %ui.%03ui %s" , n , rc , excess / 1000 ,
216218 excess % 1000 , r -> headers_in .server .data );
217219
218- if (rc && strncmp ((char * ) Host , "127.0.0.1" , 9 ) != 0 ) {
220+ if (rc && strncmp ((char * ) Host , "127.0.0.1" , 9 ) != 0 ) {
219221
220222 reply = redisCommand (c , "GET white%s" , Host );
221223 if (reply -> str == NULL ) {
222224 reply = redisCommand (c , "SETEX %s %s %s" , Host , block_second ,
223225 Host );
224226 /* Increase the history record */
225- reply = redisCommand (c ,"SELECT 1" );
227+ reply = redisCommand (c , "SELECT 1" );
226228 reply = redisCommand (c , "SET %s %s" , Host , Host );
227- reply = redisCommand (c ,"SELECT 0" );
229+ reply = redisCommand (c , "SELECT 0" );
228230 /* Increase the history record */
229231 }
230232 }
@@ -247,19 +249,19 @@ static ngx_int_t ngx_http_limit_req_handler(ngx_http_request_t *r) {
247249 }
248250 /* return http_status redis*/
249251
250- if (!ngx_strcmp ((char * ) Host , reply -> str )) {
252+ if (!ngx_strcmp ((char * ) Host , reply -> str )) {
251253// if (rc == NGX_BUSY || rc == NGX_ERROR) {
252254
253255 ngx_log_error (lrcf -> limit_log_level , r -> connection -> log , 0 ,
254256 "limiting requests, excess: %ui.%03ui by zone \"%V\" lock=%s length=%d" ,
255- excess / 1000 , excess % 1000 , & limit -> shm_zone -> shm .name , ( char * ) Host ,
256- strlen ((char * )Host ));
257+ excess / 1000 , excess % 1000 , & limit -> shm_zone -> shm .name ,
258+ ( char * ) Host , strlen ((char * )Host ));
257259
258260 ngx_log_debug5 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
259- "limit_lock]: by zone=\"%V\" ip=%s ip2=%V len=%d len2=%d " , & limit -> shm_zone -> shm . name , ( char * ) Host ,
260- & r -> connection -> addr_text , strlen (( char * )Host ) ,
261- r -> connection -> addr_text . len );
262-
261+ "limit_lock]: by zone=\"%V\" ip=%s ip2=%V len=%d len2=%d " ,
262+ & limit -> shm_zone -> shm . name , ( char * )Host ,
263+ & r -> connection -> addr_text , strlen (( char * ) Host ),
264+ r -> connection -> addr_text . len );
263265
264266 while (n -- ) {
265267 ctx = limits [n ].shm_zone -> data ;
@@ -281,8 +283,6 @@ static ngx_int_t ngx_http_limit_req_handler(ngx_http_request_t *r) {
281283 return lrcf -> status_code ;
282284 }
283285
284-
285-
286286 /* rc == NGX_AGAIN || rc == NGX_OK */
287287
288288 if (rc == NGX_AGAIN ) {
@@ -738,7 +738,7 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
738738
739739 ctx = ngx_pcalloc (cf -> pool , sizeof (ngx_http_limit_req_ctx_t ));
740740 if (ctx == NULL ) {
741- return NGX_CONF_ERROR ;
741+ return NGX_CONF_ERROR ;
742742 }
743743
744744 ngx_memzero (& ccv , sizeof (ngx_http_compile_complex_value_t ));
@@ -748,7 +748,7 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
748748 ccv .complex_value = & ctx -> key ;
749749
750750 if (ngx_http_compile_complex_value (& ccv ) != NGX_OK ) {
751- return NGX_CONF_ERROR ;
751+ return NGX_CONF_ERROR ;
752752 }
753753
754754 size = 0 ;
@@ -767,7 +767,7 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
767767 if (p == NULL ) {
768768 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
769769 "invalid zone size \"%V\"" , & value [i ]);
770- return NGX_CONF_ERROR ;
770+ return NGX_CONF_ERROR ;
771771 }
772772
773773 name .len = p - name .data ;
@@ -780,13 +780,13 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
780780 if (size == NGX_ERROR ) {
781781 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
782782 "invalid zone size \"%V\"" , & value [i ]);
783- return NGX_CONF_ERROR ;
783+ return NGX_CONF_ERROR ;
784784 }
785785
786786 if (size < (ssize_t ) (8 * ngx_pagesize )) {
787787 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
788788 "zone \"%V\" is too small" , & value [i ]);
789- return NGX_CONF_ERROR ;
789+ return NGX_CONF_ERROR ;
790790 }
791791
792792 continue ;
@@ -810,7 +810,7 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
810810 if (rate <= 0 ) {
811811 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "invalid rate \"%V\"" ,
812812 & value [i ]);
813- return NGX_CONF_ERROR ;
813+ return NGX_CONF_ERROR ;
814814 }
815815
816816 continue ;
@@ -834,21 +834,21 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
834834
835835 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "invalid parameter \"%V\"" ,
836836 & value [i ]);
837- return NGX_CONF_ERROR ;
837+ return NGX_CONF_ERROR ;
838838 }
839839
840840 if (name .len == 0 ) {
841841 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
842842 "\"%V\" must have \"zone\" parameter" , & cmd -> name );
843- return NGX_CONF_ERROR ;
843+ return NGX_CONF_ERROR ;
844844 }
845845
846846 ctx -> rate = rate * 1000 / scale ;
847847
848848 shm_zone = ngx_shared_memory_add (cf , & name , size ,
849849 & ngx_http_dynamic_limit_req_module );
850850 if (shm_zone == NULL ) {
851- return NGX_CONF_ERROR ;
851+ return NGX_CONF_ERROR ;
852852 }
853853
854854 if (shm_zone -> data ) {
@@ -857,7 +857,7 @@ ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
857857 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
858858 "%V \"%V\" is already bound to key \"%V\"" , & cmd -> name , & name ,
859859 & ctx -> key .value );
860- return NGX_CONF_ERROR ;
860+ return NGX_CONF_ERROR ;
861861 }
862862
863863 shm_zone -> init = ngx_http_limit_req_init_zone ;
@@ -892,7 +892,7 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
892892 shm_zone = ngx_shared_memory_add (cf , & s , 0 ,
893893 & ngx_http_dynamic_limit_req_module );
894894 if (shm_zone == NULL ) {
895- return NGX_CONF_ERROR ;
895+ return NGX_CONF_ERROR ;
896896 }
897897
898898 continue ;
@@ -904,7 +904,7 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
904904 if (burst <= 0 ) {
905905 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
906906 "invalid burst rate \"%V\"" , & value [i ]);
907- return NGX_CONF_ERROR ;
907+ return NGX_CONF_ERROR ;
908908 }
909909
910910 continue ;
@@ -917,21 +917,21 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
917917
918918 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "invalid parameter \"%V\"" ,
919919 & value [i ]);
920- return NGX_CONF_ERROR ;
920+ return NGX_CONF_ERROR ;
921921 }
922922
923923 if (shm_zone == NULL ) {
924924 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 ,
925925 "\"%V\" must have \"zone\" parameter" , & cmd -> name );
926- return NGX_CONF_ERROR ;
926+ return NGX_CONF_ERROR ;
927927 }
928928
929929 limits = lrcf -> limits .elts ;
930930
931931 if (limits == NULL ) {
932932 if (ngx_array_init (& lrcf -> limits , cf -> pool , 1 ,
933933 sizeof (ngx_http_limit_req_limit_t )) != NGX_OK ) {
934- return NGX_CONF_ERROR ;
934+ return NGX_CONF_ERROR ;
935935 }
936936 }
937937
@@ -943,7 +943,7 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
943943
944944 limit = ngx_array_push (& lrcf -> limits );
945945 if (limit == NULL ) {
946- return NGX_CONF_ERROR ;
946+ return NGX_CONF_ERROR ;
947947 }
948948
949949 limit -> shm_zone = shm_zone ;
0 commit comments