Skip to content

Commit ec950f8

Browse files
optimize: add upstream server information to the error log of cosocket.
1 parent 9397e1a commit ec950f8

14 files changed

+851
-36
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 114 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,46 @@ ngx_http_lua_inject_req_socket_api(lua_State *L)
428428
}
429429

430430

431+
static u_char *
432+
ngx_http_lua_socket_tcp_log_error(ngx_log_t *log, u_char *buf, size_t len)
433+
{
434+
u_char *p;
435+
in_port_t port;
436+
ngx_http_request_t *r;
437+
438+
ngx_http_lua_socket_tcp_upstream_t *u;
439+
440+
u = log->data;
441+
port = ngx_inet_get_port((struct sockaddr *) &u->sockaddr);
442+
if (port == 0) {
443+
p = ngx_snprintf(buf, len, ", upstream: %s", u->host);
444+
445+
} else {
446+
p = ngx_snprintf(buf, len, ", upstream: %s:%ud",
447+
u->host, port);
448+
}
449+
450+
len -= p - buf;
451+
if (u->peer.sockaddr != NULL) {
452+
int addr_text_len;
453+
u_char addr_text[NGX_UNIX_ADDRSTRLEN];
454+
455+
buf = p;
456+
addr_text_len = ngx_sock_ntop(u->peer.sockaddr, u->peer.socklen,
457+
addr_text, NGX_UNIX_ADDRSTRLEN, 0);
458+
p = ngx_snprintf(buf, len, "(%*s)", addr_text_len, addr_text);
459+
len -= p - buf;
460+
}
461+
462+
r = u->request;
463+
if (r != NULL) {
464+
return r->connection->log->handler(r->connection->log, p, len);
465+
}
466+
467+
return p;
468+
}
469+
470+
431471
static int
432472
ngx_http_lua_socket_tcp(lua_State *L)
433473
{
@@ -685,6 +725,9 @@ ngx_http_lua_socket_tcp_connect_helper(lua_State *L,
685725
url.default_port = port;
686726
url.no_resolve = 1;
687727

728+
u->log.data = u;
729+
u->log.handler = ngx_http_lua_socket_tcp_log_error;
730+
688731
coctx = ctx->cur_co_ctx;
689732

690733
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
@@ -723,13 +766,29 @@ ngx_http_lua_socket_tcp_connect_helper(lua_State *L,
723766
u->resolved->sockaddr = url.addrs[0].sockaddr;
724767
u->resolved->socklen = url.addrs[0].socklen;
725768
u->resolved->naddrs = 1;
726-
u->resolved->host = url.addrs[0].name;
769+
if (url.family == AF_UNIX) {
770+
u->resolved->host = url.addrs[0].name;
771+
772+
} else {
773+
u->resolved->host = url.host;
774+
}
727775

728776
} else {
729-
u->resolved->host = host;
777+
u->resolved->host = url.host;
730778
u->resolved->port = url.default_port;
731779
}
732780

781+
if (u->resolved->host.len < sizeof(u->host)) {
782+
ngx_memcpy(u->host, u->resolved->host.data, u->resolved->host.len);
783+
784+
} else {
785+
ngx_memcpy(u->host, u->resolved->host.data, sizeof(u->host) - 4);
786+
u->host[sizeof(u->host) - 4] = '.';
787+
u->host[sizeof(u->host) - 3] = '.';
788+
u->host[sizeof(u->host) - 2] = '.';
789+
u->host[sizeof(u->host) - 1] = '\0';
790+
}
791+
733792
if (u->resolved->sockaddr) {
734793
rc = ngx_http_lua_socket_resolve_retval_handler(r, u, L);
735794
if (rc == NGX_AGAIN && !resuming) {
@@ -1161,10 +1220,22 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
11611220
u->request = r; /* set the controlling request */
11621221

11631222
u->conf = llcf;
1223+
if (len < sizeof(u->host)) {
1224+
ngx_memcpy(u->host, p, len);
1225+
u->host[len] = '\0';
11641226

1165-
pc = &u->peer;
1227+
} else {
1228+
ngx_memcpy(u->host, p, sizeof(u->host) - 4);
1229+
u->host[sizeof(u->host) - 4] = '.';
1230+
u->host[sizeof(u->host) - 3] = '.';
1231+
u->host[sizeof(u->host) - 2] = '.';
1232+
u->host[sizeof(u->host) - 1] = '\0';
1233+
}
11661234

1167-
pc->log = r->connection->log;
1235+
u->log = *r->connection->log;
1236+
pc = &u->peer;
1237+
pc->sockaddr = (struct sockaddr *) &u->sockaddr;
1238+
pc->log = &u->log;
11681239
pc->log_error = NGX_ERROR_ERR;
11691240

11701241
dd("lua peer connection log: %p", pc->log);
@@ -1336,10 +1407,7 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
13361407

13371408
socklen = ur->addrs[i].socklen;
13381409

1339-
sockaddr = ngx_palloc(r->pool, socklen);
1340-
if (sockaddr == NULL) {
1341-
goto nomem;
1342-
}
1410+
sockaddr = (struct sockaddr *) &u->sockaddr;
13431411

13441412
ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
13451413

@@ -1476,6 +1544,7 @@ ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
14761544
pc->sockaddr = ur->sockaddr;
14771545
pc->socklen = ur->socklen;
14781546
pc->name = &ur->host;
1547+
ngx_memcpy(&u->sockaddr, ur->sockaddr, ur->socklen);
14791548

14801549
} else {
14811550
lua_pushnil(L);
@@ -2011,7 +2080,7 @@ ngx_http_lua_ssl_handshake_handler(ngx_connection_t *c)
20112080

20122081
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
20132082
if (llcf->log_socket_errors) {
2014-
ngx_log_error(NGX_LOG_ERR, dc->log, 0, "lua ssl "
2083+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0, "lua ssl "
20152084
"certificate verify error: (%d: %s)",
20162085
rc, u->error_ret);
20172086
}
@@ -2028,7 +2097,7 @@ ngx_http_lua_ssl_handshake_handler(ngx_connection_t *c)
20282097

20292098
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
20302099
if (llcf->log_socket_errors) {
2031-
ngx_log_error(NGX_LOG_ERR, dc->log, 0, "lua ssl "
2100+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0, "lua ssl "
20322101
"certificate does not match host \"%V\"",
20332102
&u->ssl_name);
20342103
}
@@ -2409,7 +2478,14 @@ ngx_http_lua_socket_tcp_receiveany(lua_State *L)
24092478
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
24102479

24112480
if (llcf->log_socket_errors) {
2412-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2481+
ngx_log_t *log;
2482+
2483+
log = r->connection->log;
2484+
if (u != NULL && u->peer.connection != NULL) {
2485+
log = u->peer.log;
2486+
}
2487+
2488+
ngx_log_error(NGX_LOG_ERR, log, 0,
24132489
"attempt to receive data on a closed socket: u:%p, "
24142490
"c:%p, ft:%d eof:%d",
24152491
u, u ? u->peer.connection : NULL,
@@ -2485,7 +2561,14 @@ ngx_http_lua_socket_tcp_receive(lua_State *L)
24852561
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
24862562

24872563
if (llcf->log_socket_errors) {
2488-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2564+
ngx_log_t *log;
2565+
2566+
log = r->connection->log;
2567+
if (u != NULL && u->peer.connection != NULL) {
2568+
log = u->peer.log;
2569+
}
2570+
2571+
ngx_log_error(NGX_LOG_ERR, log, 0,
24892572
"attempt to receive data on a closed socket: u:%p, "
24902573
"c:%p, ft:%d eof:%d",
24912574
u, u ? u->peer.connection : NULL,
@@ -3065,7 +3148,14 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
30653148
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
30663149

30673150
if (llcf->log_socket_errors) {
3068-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
3151+
ngx_log_t *log;
3152+
3153+
log = r->connection->log;
3154+
if (u != NULL && u->peer.connection != NULL) {
3155+
log = u->peer.log;
3156+
}
3157+
3158+
ngx_log_error(NGX_LOG_ERR, log, 0,
30693159
"attempt to send data on a closed socket: u:%p, "
30703160
"c:%p, ft:%d eof:%d",
30713161
u, u ? u->peer.connection : NULL,
@@ -3582,7 +3672,7 @@ ngx_http_lua_socket_read_handler(ngx_http_request_t *r,
35823672
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
35833673

35843674
if (llcf->log_socket_errors) {
3585-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
3675+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
35863676
"lua tcp socket read timed out");
35873677
}
35883678

@@ -3619,7 +3709,7 @@ ngx_http_lua_socket_send_handler(ngx_http_request_t *r,
36193709
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
36203710

36213711
if (llcf->log_socket_errors) {
3622-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
3712+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
36233713
"lua tcp socket write timed out");
36243714
}
36253715

@@ -3980,10 +4070,8 @@ ngx_http_lua_socket_connected_handler(ngx_http_request_t *r,
39804070

39814071
if (llcf->log_socket_errors) {
39824072
ngx_http_lua_socket_init_peer_connection_addr_text(&u->peer);
3983-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
3984-
"lua tcp socket connect timed out,"
3985-
" when connecting to %V:%ud",
3986-
&c->addr_text, ngx_inet_get_port(u->peer.sockaddr));
4073+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
4074+
"lua tcp socket connect timed out");
39874075
}
39884076

39894077
ngx_http_lua_socket_handle_conn_error(r, u,
@@ -4193,7 +4281,7 @@ ngx_http_lua_socket_tcp_conn_op_timeout_handler(ngx_event_t *ev)
41934281
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
41944282

41954283
if (llcf->log_socket_errors) {
4196-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
4284+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
41974285
"lua tcp socket queued connect timed out,"
41984286
" when trying to connect to %V:%ud",
41994287
&conn_op_ctx->host, conn_op_ctx->port);
@@ -5723,6 +5811,7 @@ ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
57235811

57245812
item->socklen = pc->socklen;
57255813
ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen);
5814+
ngx_memcpy(item->host, u->host, sizeof(u->host));
57265815
item->reused = u->reused;
57275816
item->udata_queue = u->udata_queue;
57285817
u->udata_queue = NULL;
@@ -5800,7 +5889,12 @@ ngx_http_lua_get_keepalive_peer(ngx_http_request_t *r,
58005889

58015890
pc->connection = c;
58025891
pc->cached = 1;
5892+
pc->socklen = item->socklen;
5893+
ngx_memcpy(&u->sockaddr, &item->sockaddr, item->socklen);
5894+
ngx_memcpy(u->host, item->host, sizeof(item->host));
58035895

5896+
u->log.handler = ngx_http_lua_socket_tcp_log_error;
5897+
u->log.data = u;
58045898
u->reused = item->reused + 1;
58055899
u->udata_queue = item->udata_queue;
58065900
item->udata_queue = NULL;

src/ngx_http_lua_socket_tcp.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
#define NGX_HTTP_LUA_SOCKET_FT_CLIENTABORT 0x0080
2222
#define NGX_HTTP_LUA_SOCKET_FT_SSL 0x0100
2323

24+
/* max cosocket host length, just for logging,
25+
* length greater are omitted
26+
*/
27+
#ifndef COSOCKET_HOST_LEN
28+
#define COSOCKET_HOST_LEN 32
29+
#endif
30+
2431

2532
typedef struct ngx_http_lua_socket_tcp_upstream_s
2633
ngx_http_lua_socket_tcp_upstream_t;
@@ -117,6 +124,11 @@ struct ngx_http_lua_socket_tcp_upstream_s {
117124
ngx_http_lua_co_ctx_t *write_co_ctx;
118125

119126
ngx_uint_t reused;
127+
struct sockaddr_storage sockaddr;
128+
socklen_t socklen;
129+
130+
ngx_log_t log;
131+
char host[COSOCKET_HOST_LEN];
120132

121133
#if (NGX_HTTP_SSL)
122134
ngx_str_t ssl_name;
@@ -174,6 +186,7 @@ typedef struct {
174186

175187
socklen_t socklen;
176188
struct sockaddr_storage sockaddr;
189+
char host[COSOCKET_HOST_LEN];
177190

178191
ngx_uint_t reused;
179192

0 commit comments

Comments
 (0)