reset tcp connection if error happens
All checks were successful
build container / build-container (push) Successful in 4m33s
run go test / test (push) Successful in 3m56s

This commit is contained in:
guochao 2025-02-22 23:27:41 +08:00
parent ab852a6520
commit 27634d016b

View File

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -332,7 +333,30 @@ func (server *Server) streamOnline(w http.ResponseWriter, r *http.Request, mtime
} }
if err != nil { if err != nil {
slog.With("error", err, "upstreamIdx", selectedIdx).Error("something happened during download. will not cache this response") logger := slog.With("upstreamIdx", selectedIdx)
logger.Error("something happened during download. will not cache this response. setting lingering to reset the connection.")
hijacker, ok := w.(http.Hijacker)
if !ok {
logger.Warn("response writer is not a hijacker. failed to set lingering")
return
}
conn, _, err := hijacker.Hijack()
if err != nil {
logger.With("error", err).Warn("hijack failed. failed to set lingering")
return
}
defer conn.Close()
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
logger.With("error", err).Warn("connection is not a *net.TCPConn. failed to set lingering")
return
}
if err := tcpConn.SetLinger(0); err != nil {
logger.With("error", err).Warn("failed to set lingering")
return
}
logger.Debug("connection set to linger. it will be reset once the conn.Close is called")
} }
go func() { go func() {
defer func() { defer func() {