@@ -140,6 +140,7 @@ static ngx_int_t ngx_http_lua_socket_tcp_read_resume(ngx_http_request_t *r);
140
140
static ngx_int_t ngx_http_lua_socket_tcp_write_resume (ngx_http_request_t * r );
141
141
static ngx_int_t ngx_http_lua_socket_tcp_resume_helper (ngx_http_request_t * r ,
142
142
int socket_op );
143
+ static void ngx_http_lua_tcp_queue_conn_op_cleanup (void * data );
143
144
static void ngx_http_lua_tcp_resolve_cleanup (void * data );
144
145
static void ngx_http_lua_coctx_cleanup (void * data );
145
146
static void ngx_http_lua_socket_free_pool (ngx_log_t * log ,
@@ -630,6 +631,10 @@ ngx_http_lua_socket_tcp_connect_helper(lua_State *L,
630
631
u -> write_co_ctx = ctx -> cur_co_ctx ;
631
632
632
633
conn_op_ctx -> u = u ;
634
+ ctx -> cur_co_ctx -> cleanup =
635
+ ngx_http_lua_tcp_queue_conn_op_cleanup ;
636
+ ctx -> cur_co_ctx -> data = conn_op_ctx ;
637
+
633
638
ngx_memzero (& conn_op_ctx -> event , sizeof (ngx_event_t ));
634
639
conn_op_ctx -> event .handler =
635
640
ngx_http_lua_socket_tcp_conn_op_timeout_handler ;
@@ -3977,25 +3982,10 @@ ngx_http_lua_socket_tcp_resume_conn_op(ngx_http_lua_socket_pool_t *spool)
3977
3982
* is exiting.
3978
3983
*/
3979
3984
if (ngx_queue_empty (& spool -> wait_connect_op )) {
3980
- #if (NGX_DEBUG )
3981
- ngx_http_lua_assert (!(spool -> backlog >= 0
3982
- && spool -> connections > spool -> size ));
3983
-
3984
- #else
3985
- if (spool -> backlog >= 0 && spool -> connections > spool -> size ) {
3986
- ngx_log_error (NGX_LOG_ERR , ngx_cycle -> log , 0 ,
3987
- "lua tcp socket connections count mismatched for "
3988
- "connection pool \"%s\", connections: %i, size: %i" ,
3989
- spool -> key , spool -> connections , spool -> size );
3990
- spool -> connections = spool -> size ;
3991
- }
3992
- #endif
3993
-
3994
3985
return ;
3995
3986
}
3996
3987
3997
3988
q = ngx_queue_head (& spool -> wait_connect_op );
3998
- ngx_queue_remove (q );
3999
3989
conn_op_ctx = ngx_queue_data (q , ngx_http_lua_socket_tcp_conn_op_ctx_t ,
4000
3990
queue );
4001
3991
ngx_log_debug4 (NGX_LOG_DEBUG_HTTP , ngx_cycle -> log , 0 ,
@@ -4034,27 +4024,52 @@ ngx_http_lua_socket_tcp_conn_op_ctx_cleanup(void *data)
4034
4024
static void
4035
4025
ngx_http_lua_socket_tcp_conn_op_resume_handler (ngx_event_t * ev )
4036
4026
{
4037
- ngx_http_lua_socket_tcp_upstream_t * u ;
4038
- ngx_http_lua_ctx_t * ctx ;
4027
+ ngx_queue_t * q ;
4039
4028
ngx_connection_t * c ;
4029
+ ngx_http_lua_ctx_t * ctx ;
4040
4030
ngx_http_request_t * r ;
4041
4031
ngx_http_cleanup_t * cln ;
4042
4032
ngx_http_lua_co_ctx_t * coctx ;
4033
+ ngx_http_lua_socket_pool_t * spool ;
4034
+ ngx_http_lua_socket_tcp_upstream_t * u ;
4043
4035
ngx_http_lua_socket_tcp_conn_op_ctx_t * conn_op_ctx ;
4044
4036
4045
4037
conn_op_ctx = ev -> data ;
4046
4038
u = conn_op_ctx -> u ;
4047
4039
r = u -> request ;
4040
+ spool = u -> socket_pool ;
4041
+
4042
+ if (ngx_queue_empty (& spool -> wait_connect_op )) {
4043
+ #if (NGX_DEBUG )
4044
+ ngx_http_lua_assert (!(spool -> backlog >= 0
4045
+ && spool -> connections > spool -> size ));
4046
+
4047
+ #else
4048
+ if (spool -> backlog >= 0 && spool -> connections > spool -> size ) {
4049
+ ngx_log_error (NGX_LOG_ERR , ngx_cycle -> log , 0 ,
4050
+ "lua tcp socket connections count mismatched for "
4051
+ "connection pool \"%s\", connections: %i, size: %i" ,
4052
+ spool -> key , spool -> connections , spool -> size );
4053
+ spool -> connections = spool -> size ;
4054
+ }
4055
+ #endif
4056
+
4057
+ return ;
4058
+ }
4059
+
4060
+ q = ngx_queue_head (& spool -> wait_connect_op );
4061
+ ngx_queue_remove (q );
4048
4062
4049
4063
coctx = u -> write_co_ctx ;
4050
4064
coctx -> cleanup = NULL ;
4051
4065
/* note that we store conn_op_ctx in coctx->data instead of u */
4052
4066
coctx -> data = conn_op_ctx ;
4067
+ /* clear ngx_http_lua_tcp_queue_conn_op_cleanup */
4053
4068
u -> write_co_ctx = NULL ;
4054
4069
4055
4070
ctx = ngx_http_get_module_ctx (r , ngx_http_lua_module );
4056
4071
if (ctx == NULL ) {
4057
- ngx_queue_insert_head (& u -> socket_pool -> cache_connect_op ,
4072
+ ngx_queue_insert_head (& spool -> cache_connect_op ,
4058
4073
& conn_op_ctx -> queue );
4059
4074
return ;
4060
4075
}
@@ -6021,6 +6036,36 @@ ngx_http_lua_socket_tcp_resume_helper(ngx_http_request_t *r, int socket_op)
6021
6036
}
6022
6037
6023
6038
6039
+ static void
6040
+ ngx_http_lua_tcp_queue_conn_op_cleanup (void * data )
6041
+ {
6042
+ ngx_http_lua_co_ctx_t * coctx = data ;
6043
+ ngx_http_lua_socket_tcp_upstream_t * u ;
6044
+ ngx_http_lua_socket_tcp_conn_op_ctx_t * conn_op_ctx ;
6045
+
6046
+ conn_op_ctx = coctx -> data ;
6047
+ u = conn_op_ctx -> u ;
6048
+
6049
+ ngx_log_debug2 (NGX_LOG_DEBUG_HTTP , ngx_cycle -> log , 0 ,
6050
+ "lua tcp socket abort queueing, conn_op_ctx: %p, u: %p" ,
6051
+ conn_op_ctx , u );
6052
+
6053
+ if (conn_op_ctx -> event .posted ) {
6054
+ ngx_delete_posted_event (& conn_op_ctx -> event );
6055
+
6056
+ } else if (conn_op_ctx -> event .timer_set ) {
6057
+ ngx_del_timer (& conn_op_ctx -> event );
6058
+ }
6059
+
6060
+ ngx_queue_remove (& conn_op_ctx -> queue );
6061
+ ngx_queue_insert_head (& u -> socket_pool -> cache_connect_op ,
6062
+ & conn_op_ctx -> queue );
6063
+
6064
+ u -> socket_pool -> connections -- ;
6065
+ ngx_http_lua_socket_tcp_resume_conn_op (u -> socket_pool );
6066
+ }
6067
+
6068
+
6024
6069
static void
6025
6070
ngx_http_lua_tcp_resolve_cleanup (void * data )
6026
6071
{
0 commit comments