diff --git a/internal/gateway/draw.go b/internal/gateway/draw.go index 80448a2d0..a76846667 100644 --- a/internal/gateway/draw.go +++ b/internal/gateway/draw.go @@ -755,12 +755,13 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url, } defer res.Body.Close() answered := time.Since(asked) - b, err := io.ReadAll(io.LimitReader(res.Body, 256<<20)) + rb := &tracked{r: res.Body, last: time.Now()} + b, err := io.ReadAll(io.LimitReader(rb, 256<<20)) if err != nil { if ctx.Err() != nil { // not a bare "context deadline exceeded" (#1576): the vendor // began answering, and its answer is what ran out of time - return nil, 504, fmt.Errorf("%s began answering after %s (%d %s) but hadn't sent all of its answer in %s", p.Name, answered.Round(time.Second), res.StatusCode, http.StatusText(res.StatusCode), drawTimeout) + return nil, 504, fmt.Errorf("%s began answering after %s (%d %s) but hadn't sent all of its answer in %s: %s", p.Name, answered.Round(time.Second), res.StatusCode, http.StatusText(res.StatusCode), drawTimeout, stalledAt(b, time.Since(rb.last))) } return nil, 502, err } @@ -774,6 +775,47 @@ func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url, return b, res.StatusCode, nil } +// tracked is a body that notes when its last bytes came. +type tracked struct { + r io.Reader + last time.Time +} + +func (t *tracked) Read(p []byte) (int, error) { + n, err := t.r.Read(p) + if n > 0 { + t.last = time.Now() + } + return n, err +} + +// stalledAt says where an answer that didn't finish stopped (#1576): how +// much had come, how long nothing more had, and whether the image was +// coming as a link (so the answer itself stuck, after its start) or as +// b64_json (so it was the image's own bytes that came too slowly). A relay +// may answer one size with either, so the two point at different fixes. +func stalledAt(got []byte, quiet time.Duration) string { + where := fmt.Sprintf("%s had come, and nothing more for the last %s", sizeOf(len(got)), quiet.Round(time.Second)) + switch { + case bytes.Contains(got, []byte(`"b64_json"`)): + return "it was sending the image itself as b64_json; " + where + case bytes.Contains(got, []byte(`"url"`)): + return "it was answering with the image's url, not the image; " + where + } + return "what came doesn't yet say whether the image was coming as a url or as b64_json; " + where +} + +// sizeOf is n bytes for a reader: 386 B, 12.4 KB, 3.0 MB. +func sizeOf(n int) string { + switch { + case n < 1000: + return fmt.Sprintf("%d B", n) + case n < 1000*1000: + return fmt.Sprintf("%.1f KB", float64(n)/1000) + } + return fmt.Sprintf("%.1f MB", float64(n)/1000/1000) +} + // vendorMessage is the message of a vendor's error, or its body. func vendorMessage(b []byte) string { var e struct { diff --git a/internal/gateway/draw_fetch_test.go b/internal/gateway/draw_fetch_test.go index c4a2c7126..10d9f2f6a 100644 --- a/internal/gateway/draw_fetch_test.go +++ b/internal/gateway/draw_fetch_test.go @@ -155,7 +155,45 @@ func TestVendorThatStopsMidAnswerIsSaid(t *testing.T) { } }) code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"relay/gpt-image-2.5-sunburst","prompt":"a 4K city"}`) - if code != 504 || !strings.Contains(raw, "Relay began answering after 0s (200 OK) but hadn't sent all of its answer in 300ms") { + if code != 504 || !strings.Contains(raw, "Relay began answering after 0s (200 OK) but hadn't sent all of its answer in 300ms: what came doesn't yet say whether the image was coming as a url or as b64_json; 21 B had come") { t.Fatalf("%d %s", code, raw) } } + +// david-zdw's relay (#1576) answered 2048x1152 on one domain with a 3 MB +// b64_json body that took 380 s, and on another with a 416 B url. Which of +// the two stalled is said, with how much had come. +func TestStalledAnswerSaysUrlOrB64(t *testing.T) { + was := drawTimeout + drawTimeout = 500 * time.Millisecond + t.Cleanup(func() { drawTimeout = was }) + for _, c := range []struct { + name, sent, want string + }{ + {"b64_json", `{"created":1,"data":[{"b64_json":"iVBORw0KGgo` + strings.Repeat("A", 3000) + `"`, "it was sending the image itself as b64_json; 3.0 KB had come, and nothing more for the last "}, + {"url", `{"created":1,"data":[{"url":"https://files.example/a.png"}]`, "it was answering with the image's url, not the image; 59 B had come"}, + } { + t.Run(c.name, func(t *testing.T) { + s, _ := relayed(t, func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, c.sent) + w.(http.Flusher).Flush() + select { + case <-r.Context().Done(): + case <-time.After(5 * time.Second): + } + }) + code, _, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"relay/gpt-image-2.5-sunburst","prompt":"a 2K city","size":"2048x1152"}`) + if code != 504 || !strings.Contains(raw, c.want) { + t.Fatalf("%d %s", code, raw) + } + }) + } +} + +func TestSizeOf(t *testing.T) { + for n, want := range map[int]string{386: "386 B", 12400: "12.4 KB", 3_000_000: "3.0 MB", 19_700_000: "19.7 MB"} { + if got := sizeOf(n); got != want { + t.Errorf("sizeOf(%d) = %q, want %q", n, got, want) + } + } +}