From a6ae497c022fe503d5fc206cd223af2d581ec2c3 Mon Sep 17 00:00:00 2001 From: Phi Date: Mon, 30 Oct 2023 09:36:15 +0000 Subject: [PATCH 1/2] Check UDP-buffer-size Raise alert if UDP-buffer size is not high enough --- node/builder.go | 2 ++ node/modules/alerts.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/node/builder.go b/node/builder.go index f1a825be922..128a99f8714 100644 --- a/node/builder.go +++ b/node/builder.go @@ -90,6 +90,7 @@ const ( // health checks CheckFDLimit CheckFvmConcurrency + CheckUDPBufferSize LegacyMarketsEOL // libp2p @@ -169,6 +170,7 @@ func defaults() []Option { Override(CheckFDLimit, modules.CheckFdLimit(build.DefaultFDLimit)), Override(CheckFvmConcurrency, modules.CheckFvmConcurrency()), + Override(CheckUDPBufferSize, modules.CheckUDPBufferSize(2048*1024)), Override(new(system.MemoryConstraints), modules.MemoryConstraints), Override(InitMemoryWatchdog, modules.MemoryWatchdog), diff --git a/node/modules/alerts.go b/node/modules/alerts.go index 724761e3c6c..568c39a91a1 100644 --- a/node/modules/alerts.go +++ b/node/modules/alerts.go @@ -1,8 +1,10 @@ package modules import ( + "net" "os" "strconv" + "syscall" "github.com/filecoin-project/lotus/journal/alerting" "github.com/filecoin-project/lotus/lib/ulimit" @@ -35,6 +37,61 @@ func CheckFdLimit(min uint64) func(al *alerting.Alerting) { } } +func CheckUDPBufferSize(wanted int) func(al *alerting.Alerting) { + return func(al *alerting.Alerting) { + conn, err := net.Dial("udp", "localhost:0") + if err != nil { + alert := al.AddAlertType("process", "udp-buffer-size") + al.Raise(alert, map[string]string{ + "message": "Failed to create UDP connection", + "error": err.Error(), + }) + return + } + defer conn.Close() + + udpConn, ok := conn.(*net.UDPConn) + if !ok { + alert := al.AddAlertType("process", "udp-buffer-size") + al.Raise(alert, map[string]string{ + "message": "Failed to cast connection to UDPConn", + }) + return + } + + file, err := udpConn.File() + if err != nil { + alert := al.AddAlertType("process", "udp-buffer-size") + al.Raise(alert, map[string]string{ + "message": "Failed to get file descriptor from UDPConn", + "error": err.Error(), + }) + return + } + defer file.Close() + + size, err := syscall.GetsockoptInt(int(file.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF) + if err != nil { + alert := al.AddAlertType("process", "udp-buffer-size") + al.Raise(alert, map[string]string{ + "message": "Failed to get UDP buffer size", + "error": err.Error(), + }) + return + } + + if size < wanted { + alert := al.AddAlertType("process", "udp-buffer-size") + al.Raise(alert, map[string]interface{}{ + "message": "UDP buffer size is low", + "current_size": size, + "wanted_size": wanted, + "help": "See https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes for details.", + }) + } + } +} + func LegacyMarketsEOL(al *alerting.Alerting) { // Add alert if lotus-miner legacy markets subsystem is still in use alert := al.AddAlertType("system", "EOL") From 7b473f60ae0dc5b247e56a09a55e9d2fa1060c9f Mon Sep 17 00:00:00 2001 From: Phi Date: Mon, 30 Oct 2023 09:59:59 +0000 Subject: [PATCH 2/2] Handle errors from Close() in CheckUDPBufferSize Added error handling for the Close() method calls in the CheckUDPBufferSize function to satisfy linting rules and improve error logging. --- node/modules/alerts.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/node/modules/alerts.go b/node/modules/alerts.go index 568c39a91a1..9976c6d0e42 100644 --- a/node/modules/alerts.go +++ b/node/modules/alerts.go @@ -48,7 +48,11 @@ func CheckUDPBufferSize(wanted int) func(al *alerting.Alerting) { }) return } - defer conn.Close() + defer func() { + if err := conn.Close(); err != nil { + log.Warnf("Failed to close connection: %s", err) + } + }() udpConn, ok := conn.(*net.UDPConn) if !ok { @@ -68,7 +72,11 @@ func CheckUDPBufferSize(wanted int) func(al *alerting.Alerting) { }) return } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + log.Warnf("Failed to close file: %s", err) + } + }() size, err := syscall.GetsockoptInt(int(file.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF) if err != nil {