fix: 改进 notifier 包的错误处理和连接复用

- notifier.go: Send 使用 errors.Join 收集所有通道的错误,不再丢弃中间错误
- bark.go/gotify.go/webhook.go: 响应体在关闭前先 drain,确保 HTTP 连接可复用

Co-authored-by: HoshinoAya <HoshinoDesu@users.noreply.github.com>
This commit is contained in:
Cursor AgentandHoshinoAya committed 2026-03-06 06:39:00 +00:00
1 parent a032cbf8a8
commit c8ba5f6516
4 files changed
+17 -11

No files matched your search

+5 -1
View File
@@ -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)
+5 -1
View File
@@ -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)
+2 -8
View File
@@ -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 根据配置创建通道
+5 -1
View File
@@ -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)