p2: 120s write timeout and flags after the URL
WriteTimeout 30s was cutting digest mid-flight (queue+fetch+digest) and tailscale then 502'd. 120s write, 90s handler deadline. sift fetch URL --question now parses; extra positionals error.
This commit is contained in:
+13
-2
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
@@ -24,7 +25,11 @@ import (
|
||||
"git.wntrmute.dev/kyle/sift/internal/selfip"
|
||||
)
|
||||
|
||||
const maxReqBody = 64 << 10
|
||||
const (
|
||||
maxReqBody = 64 << 10
|
||||
writeTimeout = 120 * time.Second
|
||||
requestDeadline = 90 * time.Second
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Version string
|
||||
@@ -74,7 +79,7 @@ func (s *Server) HTTPServer(addr string) *http.Server {
|
||||
Handler: s,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
WriteTimeout: writeTimeout,
|
||||
IdleTimeout: 30 * time.Second,
|
||||
MaxHeaderBytes: 16 << 10,
|
||||
}
|
||||
@@ -82,6 +87,12 @@ func (s *Server) HTTPServer(addr string) *http.Server {
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxReqBody)
|
||||
switch r.URL.Path {
|
||||
case "/v1/fetch", "/v2/scrape":
|
||||
ctx, cancel := context.WithTimeout(r.Context(), requestDeadline)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
}
|
||||
s.Mux.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.wntrmute.dev/kyle/sift/internal/auth"
|
||||
"git.wntrmute.dev/kyle/sift/internal/fetch"
|
||||
@@ -240,3 +241,11 @@ func TestJQTooLarge(t *testing.T) {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPServerWriteTimeout(t *testing.T) {
|
||||
s := New("test", &guard.Guard{}, nil, nil, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
hs := s.HTTPServer("127.0.0.1:0")
|
||||
if hs.WriteTimeout != 120*time.Second {
|
||||
t.Fatalf("WriteTimeout %s", hs.WriteTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user