chore: log where and after how many bytes a file transfer breaks

This commit is contained in:
redtea committed 2026-10-14 18:49:07 +03:00
1 parent 19f8b5291e
commit 438b20dae0
2 files changed
+17 -4

No files matched your search

+6
View File
@@ -5,11 +5,13 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"syscall" "syscall"
"time"
"github.com/x-grk/longpolling-remote-access/internal/proto" "github.com/x-grk/longpolling-remote-access/internal/proto"
) )
@@ -62,6 +64,7 @@ func (a *Agent) doFSUpload(ctx context.Context, env proto.TaskEnvelope) proto.Ta
} }
n, err := io.Copy(tmp, io.LimitReader(resp.Body, proto.FSFileCap+1)) n, err := io.Copy(tmp, io.LimitReader(resp.Body, proto.FSFileCap+1))
if err != nil { if err != nil {
log.Printf("fs receive %s: broke after %d of %d bytes: %v", args.Path, n, args.Size, err)
return proto.TaskResult{Error: "загрузка прервана: " + err.Error()} return proto.TaskResult{Error: "загрузка прервана: " + err.Error()}
} }
if n > proto.FSFileCap { if n > proto.FSFileCap {
@@ -123,13 +126,16 @@ func (a *Agent) doFSGet(ctx context.Context, env proto.TaskEnvelope) proto.TaskR
req.ContentLength = info.Size() req.ContentLength = info.Size()
req.Header.Set("X-Size", strconv.FormatInt(info.Size(), 10)) req.Header.Set("X-Size", strconv.FormatInt(info.Size(), 10))
a.authHeaders(req) a.authHeaders(req)
start := time.Now()
resp, err := a.stream.Do(req) resp, err := a.stream.Do(req)
if err != nil { if err != nil {
log.Printf("fs send %s (%d bytes): failed after %s: %v", args.Path, info.Size(), time.Since(start).Round(time.Millisecond), err)
return proto.TaskResult{Error: err.Error()} return proto.TaskResult{Error: err.Error()}
} }
io.Copy(io.Discard, resp.Body) io.Copy(io.Discard, resp.Body)
resp.Body.Close() resp.Body.Close()
if resp.StatusCode != http.StatusNoContent { if resp.StatusCode != http.StatusNoContent {
log.Printf("fs send %s (%d bytes): server answered %d after %s", args.Path, info.Size(), resp.StatusCode, time.Since(start).Round(time.Millisecond))
return proto.TaskResult{Error: "браузер не дочитал файл"} return proto.TaskResult{Error: "браузер не дочитал файл"}
} }
data, _ := json.Marshal(map[string]any{"path": args.Path, "size": info.Size()}) data, _ := json.Marshal(map[string]any{"path": args.Path, "size": info.Size()})
+11 -4
View File
@@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
"log"
"net/http" "net/http"
"path" "path"
"strconv" "strconv"
@@ -154,7 +155,10 @@ func (s *Server) handleFSUpload(w http.ResponseWriter, r *http.Request) {
copied := make(chan struct{}) copied := make(chan struct{})
go func() { go func() {
defer close(copied) defer close(copied)
_, err := io.Copy(st.w, body) n, err := io.Copy(st.w, body)
if err != nil && !errors.Is(err, io.ErrClosedPipe) {
log.Printf("fs upload %s to %s: browser stream broke after %d of %d bytes: %v", p, agentID, n, r.ContentLength, err)
}
st.w.CloseWithError(err) // nil: the reader sees the end of the file st.w.CloseWithError(err) // nil: the reader sees the end of the file
}() }()
@@ -246,9 +250,10 @@ func (s *Server) handleFSDownload(w http.ResponseWriter, r *http.Request) {
if st.size >= 0 { if st.size >= 0 {
w.Header().Set("Content-Length", strconv.FormatInt(st.size, 10)) w.Header().Set("Content-Length", strconv.FormatInt(st.size, 10))
} }
if _, err := io.Copy(w, st.r); err != nil { if n, err := io.Copy(w, st.r); err != nil {
// Cut the response off, so the browser does not take a part of // Cut the response off, so the browser does not take a part of
// the file for all of it. // the file for all of it.
log.Printf("fs download %s from %s: cut off after %d of %d bytes: %v", p, agentID, n, st.size, err)
st.r.CloseWithError(err) st.r.CloseWithError(err)
panic(http.ErrAbortHandler) panic(http.ErrAbortHandler)
} }
@@ -310,9 +315,10 @@ func (s *Server) handleAgentFSStreamGet(w http.ResponseWriter, r *http.Request)
if st.size >= 0 { if st.size >= 0 {
w.Header().Set("Content-Length", strconv.FormatInt(st.size, 10)) w.Header().Set("Content-Length", strconv.FormatInt(st.size, 10))
} }
if _, err := io.Copy(w, st.r); err != nil { if n, err := io.Copy(w, st.r); err != nil {
// The browser broke off: cut the response, so the agent does not // The browser broke off: cut the response, so the agent does not
// take a part of the file for all of it. // take a part of the file for all of it.
log.Printf("fs upload to %s: cut off after %d of %d bytes: %v", a.ID, n, st.size, err)
panic(http.ErrAbortHandler) panic(http.ErrAbortHandler)
} }
} }
@@ -338,9 +344,10 @@ func (s *Server) handleAgentFSStreamPost(w http.ResponseWriter, r *http.Request)
http.Error(w, "gone", http.StatusGone) http.Error(w, "gone", http.StatusGone)
return return
} }
_, err = io.Copy(st.w, http.MaxBytesReader(w, r.Body, proto.FSFileCap)) n, err := io.Copy(st.w, http.MaxBytesReader(w, r.Body, proto.FSFileCap))
st.w.CloseWithError(err) st.w.CloseWithError(err)
if err != nil { if err != nil {
log.Printf("fs download from %s: agent stream broke after %d of %d bytes: %v", a.ID, n, size, err)
http.Error(w, "gone", http.StatusGone) http.Error(w, "gone", http.StatusGone)
return return
} }