From c8ba5f65160514174ac1058e2e97ae489a8d265a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 6 Mar 2026 06:39:00 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E8=BF=9B=20notifier=20?= =?UTF-8?q?=E5=8C=85=E7=9A=84=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=92=8C?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - notifier.go: Send 使用 errors.Join 收集所有通道的错误,不再丢弃中间错误 - bark.go/gotify.go/webhook.go: 响应体在关闭前先 drain,确保 HTTP 连接可复用 Co-authored-by: HoshinoAya --- internal/notifier/bark.go | 6 +++++- internal/notifier/gotify.go | 6 +++++- internal/notifier/notifier.go | 10 ++-------- internal/notifier/webhook.go | 6 +++++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/internal/notifier/bark.go b/internal/notifier/bark.go index 1f8a83f..794e62f 100644 --- a/internal/notifier/bark.go +++ b/internal/notifier/bark.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "net/http" "github.com/CyberPigeon/internal/config" @@ -55,7 +56,10 @@ func (b *BarkChannel) Send(msg Message) error { if err != nil { return err } - defer resp.Body.Close() + defer func() { + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + }() if resp.StatusCode != http.StatusOK { return fmt.Errorf("Bark 返回错误状态: %d", resp.StatusCode) diff --git a/internal/notifier/gotify.go b/internal/notifier/gotify.go index 8f6cd9c..54cbceb 100644 --- a/internal/notifier/gotify.go +++ b/internal/notifier/gotify.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "net/http" "github.com/CyberPigeon/internal/config" @@ -61,7 +62,10 @@ func (g *GotifyChannel) Send(msg Message) error { if err != nil { return err } - defer resp.Body.Close() + defer func() { + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + }() if resp.StatusCode != http.StatusOK { return fmt.Errorf("Gotify 返回错误状态: %d", resp.StatusCode) diff --git a/internal/notifier/notifier.go b/internal/notifier/notifier.go index f5e441d..bb04dfb 100644 --- a/internal/notifier/notifier.go +++ b/internal/notifier/notifier.go @@ -1,6 +1,7 @@ package notifier import ( + "errors" "fmt" "log/slog" "sync" @@ -95,14 +96,7 @@ func (n *Notifier) Send(msg Message) error { wg.Wait() - // 返回最后一个错误 - var lastErr error - for _, err := range errs { - if err != nil { - lastErr = err - } - } - return lastErr + return errors.Join(errs...) } // createChannel 根据配置创建通道 diff --git a/internal/notifier/webhook.go b/internal/notifier/webhook.go index a2c634f..5da74aa 100644 --- a/internal/notifier/webhook.go +++ b/internal/notifier/webhook.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "net" "net/http" "net/url" @@ -139,7 +140,10 @@ func (w *WebhookChannel) Send(msg Message) error { if err != nil { return err } - defer resp.Body.Close() + defer func() { + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + }() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return fmt.Errorf("Webhook 返回错误状态: %d", resp.StatusCode)