Skip to content
This repository was archived by the owner on Apr 5, 2026. It is now read-only.

Commit 1eb2fa8

Browse files
committed
feat: add diagnostic logging and stats for direct mode connections
Add per-connection close logging (DC number, duration, which side closed) and two new aggregate counters for monitoring direct mode health: - direct_dc_connections_failed: connections that failed to establish - direct_dc_connections_dc_closed: connections closed by the DC side Exposed in plain text stats, Prometheus metrics, and verbose logs. Helps diagnose incomplete media loading reported in #64.
1 parent dbd8935 commit 1eb2fa8

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

mtproto/mtproto-proxy.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ struct worker_stats {
407407
long long tot_forwarded_simple_acks, dropped_simple_acks;
408408
long long mtproto_proxy_errors;
409409
long long direct_dc_connections_created, direct_dc_connections_active;
410+
long long direct_dc_connections_failed, direct_dc_connections_dc_closed;
410411

411412
long long connections_failed_lru, connections_failed_flood;
412413

@@ -433,6 +434,7 @@ long long tot_forwarded_responses, dropped_responses;
433434
long long tot_forwarded_simple_acks, dropped_simple_acks;
434435
long long mtproto_proxy_errors;
435436
long long direct_dc_connections_created, direct_dc_connections_active;
437+
long long direct_dc_connections_failed, direct_dc_connections_dc_closed;
436438

437439
char proxy_tag[16];
438440
int proxy_tag_set;
@@ -466,10 +468,12 @@ static void update_local_stats_copy (struct worker_stats *S) {
466468
UPD (mtproto_proxy_errors);
467469
UPD (direct_dc_connections_created);
468470
UPD (direct_dc_connections_active);
471+
UPD (direct_dc_connections_failed);
472+
UPD (direct_dc_connections_dc_closed);
469473
UPD (connections_failed_lru);
470474
UPD (connections_failed_flood);
471-
UPD (ext_connections);
472-
UPD (ext_connections_created);
475+
UPD (ext_connections);
476+
UPD (ext_connections_created);
473477
UPD (http_queries);
474478
UPD (http_bad_headers);
475479
{ int _i; for (_i = 0; _i < 16; _i++) {
@@ -547,6 +551,8 @@ static inline void add_stats (struct worker_stats *W) {
547551
UPD (mtproto_proxy_errors);
548552
UPD (direct_dc_connections_created);
549553
UPD (direct_dc_connections_active);
554+
UPD (direct_dc_connections_failed);
555+
UPD (direct_dc_connections_dc_closed);
550556
UPD (connections_failed_lru);
551557
UPD (connections_failed_flood);
552558
UPD (ext_connections);
@@ -680,6 +686,8 @@ void mtfront_prepare_stats (stats_buffer_t *sb) {
680686
"direct_mode\t%d\n"
681687
"direct_dc_connections_created\t%lld\n"
682688
"direct_dc_connections_active\t%lld\n"
689+
"direct_dc_connections_failed\t%lld\n"
690+
"direct_dc_connections_dc_closed\t%lld\n"
683691
"version\t" VERSION_STR " compiled at " __DATE__ " " __TIME__ " by gcc " __VERSION__ " "
684692
#ifdef __LP64__
685693
"64-bit"
@@ -749,7 +757,9 @@ void mtfront_prepare_stats (stats_buffer_t *sb) {
749757
proxy_tag_set,
750758
direct_mode,
751759
S(direct_dc_connections_created),
752-
S(direct_dc_connections_active)
760+
S(direct_dc_connections_active),
761+
S(direct_dc_connections_failed),
762+
S(direct_dc_connections_dc_closed)
753763
);
754764

755765
{ int _sc = tcp_rpcs_get_ext_secret_count();
@@ -843,7 +853,13 @@ void mtfront_prepare_prometheus_stats (stats_buffer_t *sb) {
843853
"mtproxy_ip_acl_rejected_total %lld\n"
844854
"# HELP mtproxy_direct_dc_connections_created_total Direct DC connections created.\n"
845855
"# TYPE mtproxy_direct_dc_connections_created_total counter\n"
846-
"mtproxy_direct_dc_connections_created_total %lld\n",
856+
"mtproxy_direct_dc_connections_created_total %lld\n"
857+
"# HELP mtproxy_direct_dc_connections_failed_total Direct DC connections that failed to establish.\n"
858+
"# TYPE mtproxy_direct_dc_connections_failed_total counter\n"
859+
"mtproxy_direct_dc_connections_failed_total %lld\n"
860+
"# HELP mtproxy_direct_dc_connections_dc_closed_total Direct DC connections closed by the DC side.\n"
861+
"# TYPE mtproxy_direct_dc_connections_dc_closed_total counter\n"
862+
"mtproxy_direct_dc_connections_dc_closed_total %lld\n",
847863
S(get_queries),
848864
S(tot_forwarded_queries),
849865
S(expired_forwarded_queries),
@@ -862,7 +878,9 @@ void mtfront_prepare_prometheus_stats (stats_buffer_t *sb) {
862878
S(http_queries),
863879
S(http_bad_headers),
864880
S(conn.accept_ip_acl_rejected),
865-
S(direct_dc_connections_created)
881+
S(direct_dc_connections_created),
882+
S(direct_dc_connections_failed),
883+
S(direct_dc_connections_dc_closed)
866884
);
867885

868886
/* gauges */

net/net-tcp-rpc-ext-server.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ int tcp_proxy_pass_write_packet (connection_job_t C, struct raw_message *raw) {
178178
extern int direct_mode;
179179
extern int workers;
180180
extern long long direct_dc_connections_created, direct_dc_connections_active;
181+
extern long long direct_dc_connections_failed, direct_dc_connections_dc_closed;
181182
extern long long per_secret_connections[16], per_secret_connections_created[16];
182183
extern long long per_secret_connections_rejected[16];
183184

@@ -278,14 +279,26 @@ static int tcp_direct_dc_parse_execute (connection_job_t C) {
278279

279280
static int tcp_direct_close (connection_job_t C, int who) {
280281
struct connection_info *c = CONN_INFO(C);
281-
vkprintf (1, "closing direct connection #%d %s:%d -> %s:%d\n", c->fd, show_our_ip (C), c->our_port, show_remote_ip (C), c->remote_port);
282-
if ((c->type == &ct_direct_client || c->type == &ct_direct_client_drs) && direct_dc_connections_active > 0) {
282+
int is_client = (c->type == &ct_direct_client || c->type == &ct_direct_client_drs);
283+
int is_dc = (c->type == &ct_direct_dc);
284+
int target_dc = TCP_RPC_DATA(C)->extra_int4;
285+
double duration = precise_now - c->query_start_time;
286+
287+
vkprintf (1, "direct: closing %s connection #%d (DC %d) after %.1fs, %s:%d -> %s:%d, who=%d\n",
288+
is_client ? "client" : "DC", c->fd, target_dc, duration,
289+
show_our_ip (C), c->our_port, show_remote_ip (C), c->remote_port, who);
290+
291+
if (is_client && direct_dc_connections_active > 0) {
283292
direct_dc_connections_active--;
284293
int sid = TCP_RPC_DATA(C)->extra_int2;
285294
if (sid > 0 && sid <= 16) {
286295
per_secret_connections[sid - 1]--;
287296
}
288297
}
298+
if (is_dc && who != 0) {
299+
/* DC side closed unexpectedly (not by us tearing down the pair) */
300+
direct_dc_connections_dc_closed++;
301+
}
289302
if (c->extra) {
290303
job_t E = PTR_MOVE (c->extra);
291304
fail_connection (E, -23);
@@ -389,7 +402,8 @@ static int direct_connect_to_dc (connection_job_t C, int target_dc) {
389402

390403
const struct dc_address *dc = direct_dc_lookup (target_dc);
391404
if (!dc) {
392-
vkprintf (1, "direct mode: unknown DC %d, closing connection\n", target_dc);
405+
kprintf ("direct mode: unknown DC %d, closing connection\n", target_dc);
406+
direct_dc_connections_failed++;
393407
fail_connection (C, -1);
394408
return 0;
395409
}
@@ -408,6 +422,7 @@ static int direct_connect_to_dc (connection_job_t C, int target_dc) {
408422
int cfd = client_socket (dc->ipv4, dc->port, 0);
409423
if (cfd < 0) {
410424
kprintf ("direct mode: failed to connect to DC %d: %m\n", target_dc);
425+
direct_dc_connections_failed++;
411426
fail_connection (C, -27);
412427
return 0;
413428
}
@@ -417,7 +432,8 @@ static int direct_connect_to_dc (connection_job_t C, int target_dc) {
417432
ntohl (dc->ipv4), NULL, dc->port);
418433

419434
if (!EJ) {
420-
kprintf ("direct mode: failed to create DC connection\n");
435+
kprintf ("direct mode: failed to create DC connection for DC %d\n", target_dc);
436+
direct_dc_connections_failed++;
421437
job_decref_f (C);
422438
fail_connection (C, -37);
423439
return 0;

0 commit comments

Comments
 (0)