Доработайте публикацию и сверку вложений по итогам ревью
This commit is contained in:
+20
-10
@@ -39,20 +39,23 @@ type Deps struct {
|
||||
Sessions *redis.Client
|
||||
SessionTTL time.Duration
|
||||
MaxBodyBytes int64
|
||||
// RequestTimeout bounds every API request; zero means the default.
|
||||
RequestTimeout time.Duration
|
||||
InstanceID string
|
||||
// Zero timeouts use the defaults below.
|
||||
RequestTimeout time.Duration
|
||||
BinaryRequestTimeout time.Duration
|
||||
InstanceID string
|
||||
// IdempotencyTTL zero means the contract's 24 hours.
|
||||
IdempotencyTTL time.Duration
|
||||
Blobs attachment.Blobs
|
||||
MaxUploadBytes int64
|
||||
MaxImagePixels int64
|
||||
AttachmentsCfg attachment.Config
|
||||
}
|
||||
|
||||
// defaultRequestTimeout stays below the server's 10s write timeout, so a
|
||||
// defaultRequestTimeout stays below the server's write timeout, so a
|
||||
// request that gives up on a dependency can still write its 503.
|
||||
const defaultRequestTimeout = 5 * time.Second
|
||||
|
||||
// Binary transfers and decoding share this budget, below the 35s socket timeout.
|
||||
const defaultBinaryRequestTimeout = 30 * time.Second
|
||||
|
||||
const defaultIdempotencyTTL = 24 * time.Hour
|
||||
|
||||
func New(d Deps) *App {
|
||||
@@ -61,7 +64,8 @@ func New(d Deps) *App {
|
||||
if keyTTL == 0 {
|
||||
keyTTL = defaultIdempotencyTTL
|
||||
}
|
||||
chats := chat.NewService(chatstore.NewChats(d.DB), chatstore.NewMessages(d.DB), users, keyTTL)
|
||||
chatRepository := chatstore.NewChats(d.DB)
|
||||
chats := chat.NewService(chatRepository, chatstore.NewMessages(d.DB), users, keyTTL)
|
||||
|
||||
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes, d.InstanceID)
|
||||
probes := health.New(
|
||||
@@ -81,9 +85,15 @@ func New(d Deps) *App {
|
||||
auth.NewHandler(sessions).Register(public, protected)
|
||||
user.NewHandler(users).Register(public, protected)
|
||||
chat.NewHandler(chats).Register(protected)
|
||||
attachments := attachment.NewService(attachmentstore.NewAttachments(d.DB, d.Blobs), d.Blobs, d.MaxUploadBytes, d.MaxImagePixels)
|
||||
binary := engine.Group("/api/v1", httpx.Timeout(30*time.Second), auth.RequireSession(sessions))
|
||||
attachment.NewHandler(attachments).Register(protected, binary)
|
||||
attachments := attachment.NewService(
|
||||
attachmentstore.NewAttachments(d.DB), chatRepository, d.Blobs, d.AttachmentsCfg,
|
||||
)
|
||||
binaryTimeout := d.BinaryRequestTimeout
|
||||
if binaryTimeout == 0 {
|
||||
binaryTimeout = defaultBinaryRequestTimeout
|
||||
}
|
||||
binaryProtected := engine.Group("/api/v1", httpx.Timeout(binaryTimeout), auth.RequireSession(sessions))
|
||||
attachment.NewHandler(attachments).Register(protected, binaryProtected)
|
||||
|
||||
return &App{Engine: engine, Health: probes, Chats: chats}
|
||||
}
|
||||
|
||||
@@ -30,32 +30,54 @@ func TestAttachmentUploadAuthenticatesBeforeParsing(t *testing.T) {
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/chats/not-a-uuid/attachments?filename=x.png", "", "invalid image", map[string]string{"Content-Type": "image/png"}), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
func TestAttachmentMetadataUsesOrdinaryRequestDeadline(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
actor := ts.user("deadline_user", "User")
|
||||
ts.a = app.New(app.Deps{Log: slog.New(slog.NewTextHandler(io.Discard, nil)), DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 65536, InstanceID: testInstance, RequestTimeout: 40 * time.Millisecond})
|
||||
var held []*pgxpool.Conn
|
||||
for range db.Pool.Config().MaxConns {
|
||||
conn, err := db.Pool.Acquire(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
func TestAttachmentRoutesUseConfiguredDeadlines(t *testing.T) {
|
||||
for _, binary := range []bool{false, true} {
|
||||
name := "metadata"
|
||||
requestTimeout := 40 * time.Millisecond
|
||||
var binaryTimeout time.Duration
|
||||
if binary {
|
||||
name = "upload"
|
||||
requestTimeout = time.Second
|
||||
binaryTimeout = 40 * time.Millisecond
|
||||
}
|
||||
held = append(held, conn)
|
||||
}
|
||||
defer func() {
|
||||
for _, conn := range held {
|
||||
conn.Release()
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 400*time.Millisecond)
|
||||
defer cancel()
|
||||
req := httptest.NewRequest("GET", "/api/v1/attachments/11111111-1111-4111-8111-111111111111", nil).WithContext(ctx)
|
||||
req.Header.Set("Authorization", "Bearer "+actor.token)
|
||||
rec := httptest.NewRecorder()
|
||||
started := time.Now()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
if rec.Code != 503 || time.Since(started) > 200*time.Millisecond {
|
||||
t.Fatalf("metadata exceeded ordinary budget: status%d elapsed%v body%s", rec.Code, time.Since(started), rec.Body.String())
|
||||
t.Run(name, func(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
actor := ts.user("deadline_user", "User")
|
||||
ts.a = app.New(app.Deps{
|
||||
Log: slog.New(slog.NewTextHandler(io.Discard, nil)), DB: db.Pool, Sessions: kv.Client,
|
||||
SessionTTL: time.Hour, MaxBodyBytes: 65536, InstanceID: testInstance,
|
||||
RequestTimeout: requestTimeout, BinaryRequestTimeout: binaryTimeout,
|
||||
})
|
||||
var held []*pgxpool.Conn
|
||||
for range db.Pool.Config().MaxConns {
|
||||
conn, err := db.Pool.Acquire(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
held = append(held, conn)
|
||||
}
|
||||
defer func() {
|
||||
for _, conn := range held {
|
||||
conn.Release()
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 400*time.Millisecond)
|
||||
defer cancel()
|
||||
path := "/api/v1/attachments/11111111-1111-4111-8111-111111111111"
|
||||
req := httptest.NewRequest("GET", path, nil).WithContext(ctx)
|
||||
if binary {
|
||||
path = "/api/v1/chats/11111111-1111-4111-8111-111111111111/attachments?filename=x.png"
|
||||
req = httptest.NewRequest("POST", path, strings.NewReader("body")).WithContext(ctx)
|
||||
req.Header.Set("Content-Type", "image/png")
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+actor.token)
|
||||
rec := httptest.NewRecorder()
|
||||
started := time.Now()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
if rec.Code != 503 || time.Since(started) > 200*time.Millisecond {
|
||||
t.Fatalf("route exceeded configured budget: status%d elapsed%v body%s", rec.Code, time.Since(started), rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user