Skip to content

Commit 9d8cea5

Browse files
optimize: add upstream server information to the error log of cosocket.
1 parent 948008a commit 9d8cea5

13 files changed

+893
-88
lines changed

src/ngx_stream_lua_socket_tcp.c

Lines changed: 124 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ static int ngx_stream_lua_socket_tcp_settransparent(lua_State *L);
4545
static void ngx_stream_lua_socket_tcp_handler(ngx_event_t *ev);
4646
static ngx_int_t ngx_stream_lua_socket_tcp_get_peer(ngx_peer_connection_t *pc,
4747
void *data);
48-
static void ngx_stream_lua_socket_init_peer_connection_addr_text(
49-
ngx_peer_connection_t *pc);
5048
static void ngx_stream_lua_socket_read_handler(ngx_stream_lua_request_t *r,
5149
ngx_stream_lua_socket_tcp_upstream_t *u);
5250
static void ngx_stream_lua_socket_send_handler(ngx_stream_lua_request_t *r,
@@ -473,6 +471,46 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
473471
}
474472

475473

474+
static u_char *
475+
ngx_stream_lua_socket_tcp_log_error(ngx_log_t *log, u_char *buf, size_t len)
476+
{
477+
u_char *p;
478+
in_port_t port;
479+
ngx_stream_lua_request_t *r;
480+
481+
ngx_stream_lua_socket_tcp_upstream_t *u;
482+
483+
u = log->data;
484+
port = ngx_inet_get_port((struct sockaddr *) &u->sockaddr);
485+
if (port == 0) {
486+
p = ngx_snprintf(buf, len, ", upstream: %s", u->host);
487+
488+
} else {
489+
p = ngx_snprintf(buf, len, ", upstream: %s:%ud",
490+
u->host, port);
491+
}
492+
493+
len -= p - buf;
494+
if (u->peer.sockaddr != NULL) {
495+
int addr_text_len;
496+
u_char addr_text[NGX_UNIX_ADDRSTRLEN];
497+
498+
buf = p;
499+
addr_text_len = ngx_sock_ntop(u->peer.sockaddr, u->peer.socklen,
500+
addr_text, NGX_UNIX_ADDRSTRLEN, 0);
501+
p = ngx_snprintf(buf, len, "(%*s)", addr_text_len, addr_text);
502+
len -= p - buf;
503+
}
504+
505+
r = u->request;
506+
if (r != NULL) {
507+
return r->connection->log->handler(r->connection->log, p, len);
508+
}
509+
510+
return p;
511+
}
512+
513+
476514
static int
477515
ngx_stream_lua_socket_tcp(lua_State *L)
478516
{
@@ -737,6 +775,11 @@ ngx_stream_lua_socket_tcp_connect_helper(lua_State *L,
737775
url.default_port = port;
738776
url.no_resolve = 1;
739777

778+
u->log = *r->connection->log;
779+
u->peer.log = &u->log;
780+
u->log.data = u;
781+
u->log.handler = ngx_stream_lua_socket_tcp_log_error;
782+
740783
coctx = ctx->cur_co_ctx;
741784

742785
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
@@ -778,13 +821,29 @@ ngx_stream_lua_socket_tcp_connect_helper(lua_State *L,
778821
u->resolved->sockaddr = url.addrs[0].sockaddr;
779822
u->resolved->socklen = url.addrs[0].socklen;
780823
u->resolved->naddrs = 1;
781-
u->resolved->host = url.addrs[0].name;
824+
if (url.family == AF_UNIX) {
825+
u->resolved->host = url.addrs[0].name;
826+
827+
} else {
828+
u->resolved->host = url.host;
829+
}
782830

783831
} else {
784832
u->resolved->host = host;
785833
u->resolved->port = url.default_port;
786834
}
787835

836+
if (u->resolved->host.len < sizeof(u->host)) {
837+
ngx_memcpy(u->host, u->resolved->host.data, u->resolved->host.len);
838+
839+
} else {
840+
ngx_memcpy(u->host, u->resolved->host.data, sizeof(u->host) - 4);
841+
u->host[sizeof(u->host) - 4] = '.';
842+
u->host[sizeof(u->host) - 3] = '.';
843+
u->host[sizeof(u->host) - 2] = '.';
844+
u->host[sizeof(u->host) - 1] = '\0';
845+
}
846+
788847
if (u->resolved->sockaddr) {
789848
rc = ngx_stream_lua_socket_resolve_retval_handler(r, u, L);
790849
if (rc == NGX_AGAIN && !resuming) {
@@ -1163,10 +1222,22 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
11631222
u->request = r; /* set the controlling request */
11641223

11651224
u->conf = llcf;
1225+
if (len < sizeof(u->host)) {
1226+
ngx_memcpy(u->host, p, len);
1227+
u->host[len] = '\0';
11661228

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

1169-
pc->log = r->connection->log;
1237+
u->log = *r->connection->log;
1238+
pc = &u->peer;
1239+
pc->sockaddr = (struct sockaddr *) &u->sockaddr;
1240+
pc->log = &u->log;
11701241
pc->log_error = NGX_ERROR_ERR;
11711242

11721243
dd("lua peer connection log: %p", pc->log);
@@ -1351,11 +1422,7 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
13511422

13521423
socklen = ur->addrs[i].socklen;
13531424

1354-
sockaddr = ngx_palloc(r->pool, socklen);
1355-
if (sockaddr == NULL) {
1356-
goto nomem;
1357-
}
1358-
1425+
sockaddr = (struct sockaddr *) &u->sockaddr;
13591426
ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
13601427

13611428
switch (sockaddr->sa_family) {
@@ -1419,50 +1486,6 @@ ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
14191486
}
14201487

14211488

1422-
static void
1423-
ngx_stream_lua_socket_init_peer_connection_addr_text(ngx_peer_connection_t *pc)
1424-
{
1425-
ngx_connection_t *c;
1426-
size_t addr_text_max_len;
1427-
1428-
c = pc->connection;
1429-
1430-
switch (pc->sockaddr->sa_family) {
1431-
1432-
#if (NGX_HAVE_INET6)
1433-
case AF_INET6:
1434-
addr_text_max_len = NGX_INET6_ADDRSTRLEN;
1435-
break;
1436-
#endif
1437-
1438-
#if (NGX_HAVE_UNIX_DOMAIN)
1439-
case AF_UNIX:
1440-
addr_text_max_len = NGX_UNIX_ADDRSTRLEN;
1441-
break;
1442-
#endif
1443-
1444-
case AF_INET:
1445-
addr_text_max_len = NGX_INET_ADDRSTRLEN;
1446-
break;
1447-
1448-
default:
1449-
addr_text_max_len = NGX_SOCKADDR_STRLEN;
1450-
break;
1451-
}
1452-
1453-
c->addr_text.data = ngx_pnalloc(c->pool, addr_text_max_len);
1454-
if (c->addr_text.data == NULL) {
1455-
ngx_log_error(NGX_LOG_ERR, pc->log, 0,
1456-
"init peer connection addr_text failed: no memory");
1457-
return;
1458-
}
1459-
1460-
c->addr_text.len = ngx_sock_ntop(pc->sockaddr, pc->socklen,
1461-
c->addr_text.data,
1462-
addr_text_max_len, 0);
1463-
}
1464-
1465-
14661489
static int
14671490
ngx_stream_lua_socket_resolve_retval_handler(ngx_stream_lua_request_t *r,
14681491
ngx_stream_lua_socket_tcp_upstream_t *u, lua_State *L)
@@ -1490,6 +1513,7 @@ ngx_stream_lua_socket_resolve_retval_handler(ngx_stream_lua_request_t *r,
14901513
pc->sockaddr = ur->sockaddr;
14911514
pc->socklen = ur->socklen;
14921515
pc->name = &ur->host;
1516+
ngx_memcpy(&u->sockaddr, ur->sockaddr, ur->socklen);
14931517

14941518
} else {
14951519
lua_pushnil(L);
@@ -2087,7 +2111,6 @@ ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c)
20872111
int waiting;
20882112
lua_State *L;
20892113
ngx_int_t rc;
2090-
ngx_connection_t *dc; /* downstream connection */
20912114
ngx_stream_lua_request_t *r;
20922115

20932116
ngx_stream_lua_ctx_t *ctx;
@@ -2107,7 +2130,6 @@ ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c)
21072130

21082131
waiting = u->conn_waiting;
21092132

2110-
dc = r->connection;
21112133
L = u->write_co_ctx->co;
21122134

21132135
if (c->read->timedout) {
@@ -2133,7 +2155,7 @@ ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c)
21332155
llcf = ngx_stream_lua_get_module_loc_conf(r,
21342156
ngx_stream_lua_module);
21352157
if (llcf->log_socket_errors) {
2136-
ngx_log_error(NGX_LOG_ERR, dc->log, 0, "stream lua ssl "
2158+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0, "stream lua ssl "
21372159
"certificate verify error: (%s)", err);
21382160
}
21392161

@@ -2151,7 +2173,7 @@ ngx_stream_lua_ssl_handshake_handler(ngx_connection_t *c)
21512173
llcf = ngx_stream_lua_get_module_loc_conf(r,
21522174
ngx_stream_lua_module);
21532175
if (llcf->log_socket_errors) {
2154-
ngx_log_error(NGX_LOG_ERR, dc->log, 0, "stream lua ssl "
2176+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0, "stream lua ssl "
21552177
"certificate does not match host \"%V\"",
21562178
&u->ssl_name);
21572179
}
@@ -2877,7 +2899,14 @@ ngx_stream_lua_socket_tcp_receiveany(lua_State *L)
28772899
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
28782900

28792901
if (llcf->log_socket_errors) {
2880-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2902+
ngx_log_t *log;
2903+
2904+
log = r->connection->log;
2905+
if (u != NULL && u->peer.connection != NULL) {
2906+
log = u->peer.log;
2907+
}
2908+
2909+
ngx_log_error(NGX_LOG_ERR, log, 0,
28812910
"attempt to receive data on a closed "
28822911
"socket: u:%p, c:%p, ft:%d eof:%d",
28832912
u, u ? u->peer.connection : NULL,
@@ -2954,7 +2983,14 @@ ngx_stream_lua_socket_tcp_receive(lua_State *L)
29542983
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
29552984

29562985
if (llcf->log_socket_errors) {
2957-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2986+
ngx_log_t *log;
2987+
2988+
log = r->connection->log;
2989+
if (u != NULL && u->peer.connection != NULL) {
2990+
log = u->peer.log;
2991+
}
2992+
2993+
ngx_log_error(NGX_LOG_ERR, log, 0,
29582994
"stream attempt to receive data on a closed "
29592995
"socket: u:%p, c:%p, ft:%d eof:%d",
29602996
u, u ? u->peer.connection : NULL,
@@ -3367,7 +3403,14 @@ ngx_stream_lua_socket_tcp_send(lua_State *L)
33673403
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
33683404

33693405
if (llcf->log_socket_errors) {
3370-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
3406+
ngx_log_t *log;
3407+
3408+
log = r->connection->log;
3409+
if (u != NULL && u->peer.connection != NULL) {
3410+
log = u->peer.log;
3411+
}
3412+
3413+
ngx_log_error(NGX_LOG_ERR, log, 0,
33713414
"attempt to send data on a closed socket: u:%p, "
33723415
"c:%p, ft:%d eof:%d",
33733416
u, u ? u->peer.connection : NULL,
@@ -4151,7 +4194,7 @@ ngx_stream_lua_socket_read_handler(ngx_stream_lua_request_t *r,
41514194
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
41524195

41534196
if (llcf->log_socket_errors) {
4154-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
4197+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
41554198
"stream lua tcp socket read timed out");
41564199
}
41574200

@@ -4181,14 +4224,14 @@ ngx_stream_lua_socket_send_handler(ngx_stream_lua_request_t *r,
41814224

41824225
c = u->peer.connection;
41834226

4184-
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, r->connection->log, 0,
4227+
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, u->peer.log, 0,
41854228
"stream lua tcp socket send handler");
41864229

41874230
if (c->write->timedout) {
41884231
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
41894232

41904233
if (llcf->log_socket_errors) {
4191-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
4234+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
41924235
"stream lua tcp socket write timed out");
41934236
}
41944237

@@ -4552,12 +4595,8 @@ ngx_stream_lua_socket_connected_handler(ngx_stream_lua_request_t *r,
45524595
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
45534596

45544597
if (llcf->log_socket_errors) {
4555-
ngx_stream_lua_socket_init_peer_connection_addr_text(&u->peer);
4556-
4557-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
4558-
"stream lua tcp socket connect timed out,"
4559-
" when connecting to %V:%ud",
4560-
&c->addr_text, ngx_inet_get_port(u->peer.sockaddr));
4598+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
4599+
"stream lua tcp socket connect timed out");
45614600
}
45624601

45634602
ngx_stream_lua_socket_handle_conn_error(r, u,
@@ -4776,7 +4815,7 @@ ngx_stream_lua_socket_tcp_conn_op_timeout_handler(ngx_event_t *ev)
47764815
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);
47774816

47784817
if (llcf->log_socket_errors) {
4779-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
4818+
ngx_log_error(NGX_LOG_ERR, u->peer.log, 0,
47804819
"stream lua tcp socket queued connect "
47814820
"timed out, when trying to connect to %V:%ud",
47824821
&conn_op_ctx->host, conn_op_ctx->port);
@@ -6193,6 +6232,8 @@ ngx_stream_lua_socket_tcp_setkeepalive(lua_State *L)
61936232

61946233
item->socklen = pc->socklen;
61956234
ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen);
6235+
ngx_memcpy(&item->host, u->host, sizeof(u->host));
6236+
61966237
item->reused = u->reused;
61976238

61986239
if (c->read->ready) {
@@ -6252,6 +6293,13 @@ ngx_stream_lua_get_keepalive_peer(ngx_stream_lua_request_t *r,
62526293
"stream lua tcp socket get keepalive peer: "
62536294
"using connection %p, fd:%d", c, c->fd);
62546295

6296+
pc->socklen = item->socklen;
6297+
ngx_memcpy(pc->sockaddr, &item->sockaddr, item->socklen);
6298+
ngx_memcpy(u->host, item->host, sizeof(item->host));
6299+
6300+
u->log.handler = ngx_stream_lua_socket_tcp_log_error;
6301+
u->log.data = u;
6302+
62556303
c->idle = 0;
62566304
c->log = pc->log;
62576305
c->pool->log = pc->log;
@@ -6270,7 +6318,12 @@ ngx_stream_lua_get_keepalive_peer(ngx_stream_lua_request_t *r,
62706318

62716319
pc->connection = c;
62726320
pc->cached = 1;
6321+
pc->socklen = item->socklen;
6322+
ngx_memcpy(&u->sockaddr, &item->sockaddr, item->socklen);
6323+
ngx_memcpy(u->host, item->host, sizeof(item->host));
62736324

6325+
u->log.handler = ngx_stream_lua_socket_tcp_log_error;
6326+
u->log.data = u;
62746327
u->reused = item->reused + 1;
62756328

62766329
#if 1

0 commit comments

Comments
 (0)