feat: restricted S3 credentials, orphan reconciler, upload limits (grade 4)

This commit is contained in:
2026-11-20 04:30:58 +03:00
parent 89e9e7b4d4
commit 1c3011bf28
42 changed files with 1483 additions and 130 deletions
+6 -3
View File
@@ -4,16 +4,19 @@ COPY go.mod go.sum ./
RUN go mod download
COPY cmd ./cmd
COPY internal ./internal
RUN CGO_ENABLED=0 go build -trimpath -o /out/server ./cmd/server
RUN CGO_ENABLED=0 go build -trimpath -o /out/server ./cmd/server \
&& CGO_ENABLED=0 go build -trimpath -o /out/s3-init ./cmd/s3-init \
&& CGO_ENABLED=0 go build -trimpath -o /out/reconcile ./cmd/reconcile \
&& CGO_ENABLED=0 go build -trimpath -o /out/s3tool ./cmd/s3tool
FROM alpine:3.24.1
ARG BUILD_ID=dev
LABEL org.opencontainers.image.revision=$BUILD_ID
RUN adduser -D -H -u 10001 app
COPY --from=build /out/server /usr/local/bin/server
COPY --from=build /out/server /out/s3-init /out/reconcile /out/s3tool /usr/local/bin/
USER app
ENV APP_HOST=0.0.0.0 APP_PORT=8080
EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
CMD wget -q -O /dev/null "http://127.0.0.1:${APP_PORT}/health/ready" || exit 1
ENTRYPOINT ["/usr/local/bin/server"]
ENTRYPOINT ["/usr/local/bin/server"]
+4 -1
View File
@@ -8,7 +8,7 @@ TEST_DATABASE_URL ?= $(shell sed 's/@postgres:5432/@127.0.0.1:55432/' secrets/da
TEST_REDIS_URL ?= redis://127.0.0.1:56379/0
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
.PHONY: help up down check validate test load test-persistence test-failover test-idempotency test-attachments test-go
.PHONY: help up down check validate test load test-persistence test-failover test-idempotency test-attachments test-memory test-go
help:
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
@@ -46,6 +46,9 @@ test-idempotency:
test-attachments:
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/attachments-check.py
test-memory:
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/upload-load.py --concurrency 10 --output evidence/upload-memory.json
test-go:
@TEST_DATABASE_URL="$(TEST_DATABASE_URL)" TEST_REDIS_URL="$(TEST_REDIS_URL)" go test -count=1 -v ./...
+143
View File
@@ -0,0 +1,143 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/config"
"messenger/internal/reconcile"
"messenger/internal/repository/postgres"
"messenger/internal/repository/s3"
)
func main() {
dryRun := flag.Bool("dry-run", true, "report only, do not delete orphan objects")
grace := flag.Duration("grace", time.Hour, "minimum object age before it can be deleted")
prefix := flag.String("prefix", "chats/", "object key prefix to scan")
limit := flag.Int("limit", 10000, "maximum number of objects to scan")
flag.Parse()
if err := run(*dryRun, *grace, *prefix, *limit); err != nil {
fmt.Fprintln(os.Stderr, "reconcile failed:", err)
os.Exit(1)
}
}
func run(dryRun bool, grace time.Duration, prefix string, limit int) error {
if grace < 0 {
return fmt.Errorf("grace must not be negative")
}
if limit <= 0 {
return fmt.Errorf("limit must be positive")
}
started := time.Now()
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
region := getenv("S3_REGION", "us-east-1")
bucket := getenv("S3_BUCKET", "messenger-attachments")
accessKeyID, err := config.ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return err
}
secretAccessKey, err := config.ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return err
}
databaseURL, err := config.ReadSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
store, err := s3.New(endpoint, region, accessKeyID, secretAccessKey, bucket)
if err != nil {
return err
}
scanned, truncated, err := store.List(ctx, prefix, limit)
if err != nil {
return err
}
pool, err := postgres.NewPool(ctx, databaseURL)
if err != nil {
return err
}
defer pool.Close()
rows, err := loadMetadata(ctx, pool)
if err != nil {
return err
}
orphans, missing := reconcile.Plan(toObjects(scanned), rows, prefix, time.Now(), grace)
if truncated {
missing = nil
}
report := reconcile.Report{
Prefix: prefix,
GraceSeconds: int64(grace.Seconds()),
DryRun: dryRun,
ScannedObjects: len(scanned),
ScannedRows: len(rows),
Truncated: truncated,
Orphans: orphans,
Missing: missing,
}
if !dryRun {
for _, orphan := range orphans {
if err := store.Delete(ctx, orphan.Key); err != nil {
report.DeleteFailures++
continue
}
report.Deleted++
}
}
report.DurationMS = time.Since(started).Milliseconds()
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(report); err != nil {
return err
}
if report.DeleteFailures > 0 {
return fmt.Errorf("%d orphan deletions failed", report.DeleteFailures)
}
return nil
}
func loadMetadata(ctx context.Context, pool *pgxpool.Pool) ([]reconcile.Metadata, error) {
rows, err := pool.Query(ctx, `SELECT id, object_key, chat_id, created_at FROM attachments`)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, func(row pgx.CollectableRow) (reconcile.Metadata, error) {
var metadata reconcile.Metadata
err := row.Scan(&metadata.ID, &metadata.ObjectKey, &metadata.ChatID, &metadata.CreatedAt)
return metadata, err
})
}
func toObjects(scanned []s3.ObjectInfo) []reconcile.Object {
objects := make([]reconcile.Object, len(scanned))
for i, info := range scanned {
objects[i] = reconcile.Object{Key: info.Key, Size: info.Size, LastModified: info.LastModified}
}
return objects
}
func getenv(key, def string) string {
if value := os.Getenv(key); value != "" {
return value
}
return def
}
+126
View File
@@ -0,0 +1,126 @@
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/minio/madmin-go/v3"
"messenger/internal/config"
"messenger/internal/repository/s3"
)
const policyName = "messenger-app"
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "s3-init failed:", err)
os.Exit(1)
}
}
func run() error {
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
region := getenv("S3_REGION", "us-east-1")
bucket := getenv("S3_BUCKET", "messenger-attachments")
rootAccessKey, err := config.ReadSecret("S3_ROOT_ACCESS_KEY_ID_FILE", "/run/secrets/s3_root_access_key")
if err != nil {
return err
}
rootSecretKey, err := config.ReadSecret("S3_ROOT_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_root_secret_key")
if err != nil {
return err
}
appAccessKey, err := config.ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return err
}
appSecretKey, err := config.ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
store, err := s3.New(endpoint, region, rootAccessKey, rootSecretKey, bucket)
if err != nil {
return err
}
if err := store.EnsureBucket(ctx); err != nil {
return err
}
fmt.Println("bucket ready:", bucket)
policy, err := policyDocument(bucket)
if err != nil {
return err
}
host, secure := s3.EndpointHost(endpoint)
admin, err := madmin.New(host, rootAccessKey, rootSecretKey, secure)
if err != nil {
return err
}
if err := retry(ctx, func(ctx context.Context) error { return admin.AddCannedPolicy(ctx, policyName, policy) }); err != nil {
return err
}
fmt.Println("policy ready:", policyName)
if err := retry(ctx, func(ctx context.Context) error {
return admin.SetUser(ctx, appAccessKey, appSecretKey, madmin.AccountEnabled)
}); err != nil {
return err
}
fmt.Println("application credentials ready")
if err := retry(ctx, func(ctx context.Context) error {
_, err := admin.AttachPolicy(ctx, madmin.PolicyAssociationReq{Policies: []string{policyName}, User: appAccessKey})
return err
}); err != nil {
return err
}
fmt.Println("policy attached to application credentials")
return nil
}
func retry(ctx context.Context, fn func(context.Context) error) error {
var err error
for attempt := 1; attempt <= 12; attempt++ {
if err = fn(ctx); err == nil {
return nil
}
select {
case <-ctx.Done():
return err
case <-time.After(time.Duration(attempt) * time.Second):
}
}
return err
}
func policyDocument(bucket string) ([]byte, error) {
document := map[string]any{
"Version": "2012-10-17",
"Statement": []map[string]any{
{
"Effect": "Allow",
"Action": []string{"s3:ListBucket", "s3:GetBucketLocation"},
"Resource": []string{"arn:aws:s3:::" + bucket},
},
{
"Effect": "Allow",
"Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"},
"Resource": []string{"arn:aws:s3:::" + bucket + "/*"},
},
},
}
return json.Marshal(document)
}
func getenv(key, def string) string {
if value := os.Getenv(key); value != "" {
return value
}
return def
}
+118
View File
@@ -0,0 +1,118 @@
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"
"messenger/internal/config"
"messenger/internal/repository/s3"
)
func main() {
if len(os.Args) < 2 {
usage()
}
if err := run(os.Args[1], os.Args[2:]); err != nil {
fmt.Fprintln(os.Stderr, "s3tool failed:", err)
os.Exit(1)
}
}
func run(command string, args []string) error {
store, err := newStore()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
switch command {
case "put":
if len(args) != 2 {
usage()
}
content, err := readContent(args[1])
if err != nil {
return err
}
if err := store.Put(ctx, args[0], bytes.NewReader(content), int64(len(content)), "application/octet-stream"); err != nil {
return err
}
fmt.Printf("put %s (%d bytes)\n", args[0], len(content))
case "delete":
if len(args) != 1 {
usage()
}
if err := store.Delete(ctx, args[0]); err != nil {
return err
}
fmt.Printf("deleted %s\n", args[0])
case "list":
prefix := ""
if len(args) > 0 {
prefix = args[0]
}
objects, _, err := store.List(ctx, prefix, 1000)
if err != nil {
return err
}
for _, object := range objects {
fmt.Printf("%s\t%d\t%s\n", object.Key, object.Size, object.LastModified.UTC().Format(time.RFC3339))
}
case "buckets":
buckets, err := store.Buckets(ctx)
if err != nil {
return err
}
for _, bucket := range buckets {
fmt.Println(bucket)
}
case "remove-bucket":
if len(args) != 1 {
usage()
}
if err := store.RemoveBucket(ctx, args[0]); err != nil {
return err
}
fmt.Printf("removed %s\n", args[0])
default:
usage()
}
return nil
}
func readContent(source string) ([]byte, error) {
if source == "-" {
return io.ReadAll(os.Stdin)
}
return os.ReadFile(source)
}
func newStore() (*s3.Store, error) {
accessKeyID, err := config.ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return nil, err
}
secretAccessKey, err := config.ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return nil, err
}
return s3.New(getenv("S3_ENDPOINT", "http://s3:9000"), getenv("S3_REGION", "us-east-1"),
accessKeyID, secretAccessKey, getenv("S3_BUCKET", "messenger-attachments"))
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: s3tool put <key> <file> | delete <key> | list [prefix] | buckets | remove-bucket <name>")
os.Exit(2)
}
func getenv(key, def string) string {
if value := os.Getenv(key); value != "" {
return value
}
return def
}
+2 -4
View File
@@ -66,10 +66,8 @@ func run(log *slog.Logger) error {
if err != nil {
return err
}
if err := objects.EnsureBucket(context.Background()); err != nil {
log.Error("s3 bucket bootstrap failed", "err", err)
}
attachments := service.NewAttachmentService(chats, store, objects, cfg.MaxUploadBytes, cfg.MaxImagePixels)
attachments := service.NewAttachmentService(chats, store, objects, cfg.MaxUploadBytes, cfg.MaxImagePixels,
cfg.MaxConcurrentUploads, cfg.MaxConcurrentDecodes)
deps := []handler.Pinger{store.Ping, sessions.Ping, objects.Ping}
+41 -4
View File
@@ -51,14 +51,14 @@ services:
s3:
image: docker.io/rustfs/rustfs:1.0.6
environment:
RUSTFS_ACCESS_KEY_FILE: /run/secrets/s3_access_key_id
RUSTFS_SECRET_KEY_FILE: /run/secrets/s3_secret_access_key
RUSTFS_ACCESS_KEY_FILE: /run/secrets/s3_root_access_key
RUSTFS_SECRET_KEY_FILE: /run/secrets/s3_root_secret_key
RUSTFS_REGION: ${S3_REGION:-us-east-1}
RUSTFS_CONSOLE_ENABLE: "false"
RUSTFS_OBS_LOGGER_LEVEL: warn
secrets:
- s3_access_key_id
- s3_secret_access_key
- s3_root_access_key
- s3_root_secret_key
volumes:
- s3data:/data
networks:
@@ -75,6 +75,35 @@ services:
memory: 512M
restart: unless-stopped
s3-init:
build: .
image: localhost/messenger-api:lab4
entrypoint: ["/usr/local/bin/s3-init"]
environment:
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ROOT_ACCESS_KEY_ID_FILE: /run/secrets/s3_root_access_key
S3_ROOT_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_root_secret_key
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
secrets:
- s3_root_access_key
- s3_root_secret_key
- s3_access_key_id
- s3_secret_access_key
networks:
- backend
depends_on:
s3:
condition: service_healthy
deploy:
resources:
limits:
cpus: "0.5"
memory: 256M
restart: "no"
migrate:
image: docker.io/migrate/migrate:v4.20.1
entrypoint: ["/bin/sh", "-c"]
@@ -112,6 +141,8 @@ services:
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
MAX_CONCURRENT_UPLOADS: ${MAX_CONCURRENT_UPLOADS:-8}
MAX_CONCURRENT_DECODES: ${MAX_CONCURRENT_DECODES:-1}
secrets:
- database_url
- cache_url
@@ -140,6 +171,8 @@ services:
condition: service_healthy
s3:
condition: service_healthy
s3-init:
condition: service_completed_successfully
restart: unless-stopped
lb:
@@ -168,6 +201,10 @@ secrets:
file: ./secrets/database_url
cache_url:
file: ./secrets/cache_url
s3_root_access_key:
file: ./secrets/s3_root_access_key
s3_root_secret_key:
file: ./secrets/s3_root_secret_key
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
+1 -1
View File
@@ -1,4 +1,4 @@
2026-11-20T03:51:29+03:00
2026-11-20T04:20:26+03:00
make check
python3 scripts/check_contract.py
+85
View File
@@ -0,0 +1,85 @@
2026-11-20T04:14:17+03:00
failure between S3 write and database commit: an object must not stay unaccounted
$ scripts/reconcile.sh # baseline
{
"prefix": "chats/",
"grace_seconds": 3600,
"dry_run": true,
"scanned_objects": 15,
"scanned_rows": 16,
"truncated": false,
"orphans": [],
"missing": [
{
"id": "97121cfe-6b29-4227-867f-3188e72a725c",
"object_key": "chats/9452049e-b86c-4d9a-8060-d98f6966b956/97121cfe-6b29-4227-867f-3188e72a725c",
"chat_id": "9452049e-b86c-4d9a-8060-d98f6966b956",
"created_at": "2026-11-20T01:11:14Z"
}
],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 8
}
$ podman exec -i postgres psql < scripts/sql/inject-db-failure.sql
CREATE FUNCTION
NOTICE: trigger "inject_attachment_insert_failure" for relation "attachments" does not exist, skipping
DROP TRIGGER
CREATE TRIGGER
$ scripts/upload-once.py --expect 503 # insert fails after a successful S3 PUT
status=503
$ scripts/reconcile.sh # same object count: compensating DELETE removed the object
{
"prefix": "chats/",
"grace_seconds": 3600,
"dry_run": true,
"scanned_objects": 15,
"scanned_rows": 16,
"truncated": false,
"orphans": [],
"missing": [
{
"id": "97121cfe-6b29-4227-867f-3188e72a725c",
"object_key": "chats/9452049e-b86c-4d9a-8060-d98f6966b956/97121cfe-6b29-4227-867f-3188e72a725c",
"chat_id": "9452049e-b86c-4d9a-8060-d98f6966b956",
"created_at": "2026-11-20T01:11:14Z"
}
],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 7
}
$ podman exec -i postgres psql < scripts/sql/reset-db-failure.sql
DROP TRIGGER
DROP FUNCTION
$ scripts/upload-once.py --expect 201 # service healthy again
status=201
attachment=a6844b91-5b7b-4694-93fc-87d808b687fb size=653
$ scripts/reconcile.sh # one more accounted object
{
"prefix": "chats/",
"grace_seconds": 3600,
"dry_run": true,
"scanned_objects": 16,
"scanned_rows": 17,
"truncated": false,
"orphans": [],
"missing": [
{
"id": "97121cfe-6b29-4227-867f-3188e72a725c",
"object_key": "chats/9452049e-b86c-4d9a-8060-d98f6966b956/97121cfe-6b29-4227-867f-3188e72a725c",
"chat_id": "9452049e-b86c-4d9a-8060-d98f6966b956",
"created_at": "2026-11-20T01:11:14Z"
}
],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 7
}
+25
View File
@@ -0,0 +1,25 @@
2026-11-20T04:17:09+03:00
separate restricted S3 credentials for the API and idempotent bucket bootstrap
$ podman-compose run --rm s3-init (first run)
bucket ready: messenger-attachments
policy ready: messenger-app
application credentials ready
policy attached to application credentials
$ podman-compose run --rm s3-init (second run: idempotent)
bucket ready: messenger-attachments
policy ready: messenger-app
application credentials ready
policy attached to application credentials
secret files mounted into api vs s3 (hashes differ)
api_1 app secret sha256: 02b5014e8a0f03ac52a0a32d19abe13cd0e64ad9a9c61f178e3eede9989c82ce
s3 root secret sha256: 970780881d80d85eb3f084c147c71e390fa563e0e022fa75cd8170d5a607cacc
secrets per role
messenger-lab4_api_1: cache_url database_url s3_access_key_id s3_secret_access_key
messenger-lab4_s3_1: s3_root_access_key s3_root_secret_key
$ scripts/s3tool.sh remove-bucket messenger-attachments (must be denied)
s3tool failed: dependency unavailable: Access Denied
+2 -2
View File
@@ -1,7 +1,7 @@
2026-11-20T03:53:34+03:00
2026-11-20T04:18:05+03:00
users | chats | messages | attachments | links | db_size
-------+-------+----------+-------------+-------+---------
40 | 11 | 289 | 8 | 2 | 8542 kB
58 | 17 | 303 | 30 | 2 | 8534 kB
(1 row)
+4 -4
View File
@@ -1,10 +1,10 @@
2026-11-20T03:53:33+03:00
2026-11-20T04:18:05+03:00
Linux archlinux 7.3.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 17 Nov 2026 22:28:33 +0000 x86_64 GNU/Linux
12
total used free shared buff/cache available
Mem: 15Gi 5.0Gi 1.3Gi 385Mi 9.6Gi 10Gi
Swap: 8.0Gi 58Mi 7.9Gi
Mem: 15Gi 6.2Gi 2.7Gi 476Mi 7.1Gi 9.2Gi
Swap: 8.0Gi 57Mi 7.9Gi
podman version 6.2.0
podman version 6.2.0
podman-compose version 1.6.0
@@ -16,4 +16,4 @@ docker.io/library/postgres:18.6-alpine3.24 57b23a0326ee
docker.io/library/redis:8.10.3-alpine3.23 9722accf3e8e
docker.io/migrate/migrate:v4.20.1 719a6f5d0ade
docker.io/rustfs/rustfs:1.0.6 7cd1c573d76d
localhost/messenger-api:lab4 ff3adbffb20b
localhost/messenger-api:lab4 e2582dea4a72
+2 -2
View File
@@ -1,7 +1,7 @@
2026-11-20T03:52:00+03:00
2026-11-20T04:14:49+03:00
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 86, "failed_attempts": 0, "first_recovery_seconds": 3.239}
{"observed_instances_before": 2, "successful_writes": 101, "failed_attempts": 0, "first_recovery_seconds": 3.304}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+2 -2
View File
@@ -1,7 +1,7 @@
2026-11-20T03:52:32+03:00
2026-11-20T04:15:25+03:00
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 87, "failed_attempts": 0, "first_recovery_seconds": 3.236}
{"observed_instances_before": 2, "successful_writes": 86, "failed_attempts": 0, "first_recovery_seconds": 3.228}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+2 -2
View File
@@ -1,4 +1,4 @@
2026-11-20T03:51:42+03:00
2026-11-20T04:20:36+03:00
make test-idempotency
BASE_URL="http://localhost:8080" python3 scripts/idempotency-race.py
@@ -7,5 +7,5 @@ successful responses (201): 20
failed requests: 0 []
distinct message ids in responses: 1
messages stored in history: 1
instances that served requests: ['939e096de71a', 'dbc0d13c9b45']
instances that served requests: ['8535d7918d12', 'ad1700491457']
OK: exactly one message created
+5 -5
View File
@@ -1,8 +1,8 @@
2026-11-20T03:53:27+03:00
2026-11-20T04:21:39+03:00
API replicas and single entrypoint
messenger-lab4_api_1 Up About a minute (healthy)
messenger-lab4_api_2 Up 35 seconds (healthy)
messenger-lab4_api_1 Up 26 seconds (healthy)
messenger-lab4_api_2 Up 26 seconds (healthy)
messenger-lab4_lb_1 Up About a minute
messenger-lab4_postgres_1 Up About a minute (healthy)
messenger-lab4_redis_1 Up About a minute (healthy)
@@ -19,5 +19,5 @@ messenger-lab4_s3_1 9000-9001/tcp
X-Instance-Id distribution over 20 requests through LB
10 x-instance-id: 939e096de71a
10 x-instance-id: dbc0d13c9b45
10 x-instance-id: 33839df0eea0
10 x-instance-id: 4e3d4c4f1194
+4 -3
View File
@@ -1,4 +1,4 @@
2026-11-20T03:53:28+03:00
2026-11-20T04:17:58+03:00
per-container limits, networks, secrets
messenger-lab4_api_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend messenger-lab4_edge
@@ -7,13 +7,14 @@ messenger-lab4_lb_1 nano_cpus=500000000 memory=67108864 networks=messenger-lab4_
messenger-lab4_postgres_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend
messenger-lab4_redis_1 nano_cpus=500000000 memory=134217728 networks=messenger-lab4_backend
messenger-lab4_s3_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend
messenger-lab4_s3-init_1 nano_cpus=500000000 memory=268435456 networks=messenger-lab4_backend
messenger-lab4_migrate_1 nano_cpus=500000000 memory=67108864 networks=messenger-lab4_backend
ls /run/secrets per container
ls /run/secrets per running container
messenger-lab4_api_1: cache_url database_url s3_access_key_id s3_secret_access_key
messenger-lab4_api_2: cache_url database_url s3_access_key_id s3_secret_access_key
messenger-lab4_lb_1:
messenger-lab4_postgres_1: postgres_password
messenger-lab4_redis_1:
messenger-lab4_s3_1: s3_access_key_id s3_secret_access_key
messenger-lab4_s3_1: s3_root_access_key s3_root_secret_key
+36
View File
@@ -0,0 +1,36 @@
2026-11-20T04:21:28+03:00
10 concurrent uploads of a 8000x5000 PNG (40,000,000 pixels) per replica; API memory limit 512 MiB; MAX_CONCURRENT_UPLOADS=8, MAX_CONCURRENT_DECODES=1
BASE_URL="http://localhost:8080" python3 scripts/upload-load.py --concurrency 10 --output evidence/upload-memory.json
{
"concurrency": 10,
"uploads": 10,
"fixture": "large.png",
"fixture_bytes": 126609,
"elapsed_seconds": 1.235,
"successful": 10,
"failed": 0,
"failures": [],
"latency_ms": {
"p50": 663.7,
"p95": 1224.7,
"p99": 1224.7
},
"api_containers": [
"messenger-lab4_api_1",
"messenger-lab4_api_2"
],
"peak_bytes_before": {
"messenger-lab4_api_1": 5050368,
"messenger-lab4_api_2": 4820992
},
"peak_bytes_after": {
"messenger-lab4_api_1": 368119808,
"messenger-lab4_api_2": 368185344
},
"peak_delta_bytes": {
"messenger-lab4_api_1": 363069440,
"messenger-lab4_api_2": 363364352
},
"peak_max_mb": 351.13
}
+4 -4
View File
@@ -1,9 +1,9 @@
2026-11-20T03:53:34+03:00
2026-11-20T04:18:05+03:00
migrate container logs and schema version
1/u init (12.217985ms)
2/u message_idempotency (17.93178ms)
3/u attachments (25.639306ms)
1/u init (17.596326ms)
2/u message_idempotency (23.558514ms)
3/u attachments (35.669736ms)
no change
no change
no change
+28
View File
@@ -0,0 +1,28 @@
2026-11-20T04:20:27+03:00
make test-attachments
BASE_URL="http://localhost:8080" python3 scripts/attachments-check.py
ok: upload 201 ready, sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75, size=111817
ok: jpeg upload/download byte-identical; jpeg declared as png -> 415
ok: png with 40,000,000 pixels accepted and downloaded byte-identical
ok: filename with slash -> 400
ok: filename with NUL -> 400
ok: empty filename -> 400
ok: declared jpeg with png body -> 415
ok: truncated png -> 415
ok: 10485761 bytes -> 413
ok: text/plain -> 415
ok: unknown variant -> 400
ok: variant=original -> 200
ok: unknown attachment id -> 404
ok: upload by outsider -> 404
ok: attachment from another chat -> 404
ok: unknown attachment id in message -> 404
ok: duplicate attachment_ids -> 400
ok: eleven attachment_ids -> 400
ok: empty text without attachments -> 400
ok: idempotent replay with same attachment set -> same message
ok: absent attachment_ids equals [] for idempotency
ok: same key with a different attachment set -> 409
ok: history exposes attachment_ids on every message
PASS: 23 attachment contract checks
+13 -13
View File
@@ -1,4 +1,4 @@
2026-11-20T03:51:42+03:00
2026-11-20T04:19:38+03:00
make test-persistence ACTION_SCRIPT=scripts/restart.sh
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
@@ -7,12 +7,12 @@ PASS: persistence preserved confirmed data and existing session. Hook scope need
--- hook scope: containers and volumes before/after scripts/restart.sh ---
messenger-lab4_api_1 dbc0d13c9b45 Up About a minute (healthy)
messenger-lab4_api_2 939e096de71a Up 42 seconds (healthy)
messenger-lab4_lb_1 c2c6a2077627 Up About a minute
messenger-lab4_postgres_1 a0b192c17952 Up About a minute (healthy)
messenger-lab4_redis_1 f787abf8c4bf Up About a minute (healthy)
messenger-lab4_s3_1 49ff0b0c9d92 Up About a minute (healthy)
messenger-lab4_api_1 8535d7918d12 Up 1 second (starting)
messenger-lab4_api_2 ad1700491457 Up 1 second (starting)
messenger-lab4_lb_1 0bed57f994fa Up 1 second
messenger-lab4_postgres_1 53c091ca7f0a Up 1 second (starting)
messenger-lab4_redis_1 e0cfe44337f5 Up 1 second (healthy)
messenger-lab4_s3_1 8548fb0864af Up 1 second (healthy)
messenger-lab4_pgdata
messenger-lab4_redisdata
messenger-lab4_s3data
@@ -20,12 +20,12 @@ messenger-lab4_s3data
$ scripts/restart.sh
messenger-lab4_api_1 dbc0d13c9b45 Up 15 seconds (healthy)
messenger-lab4_api_2 939e096de71a Up 15 seconds (healthy)
messenger-lab4_lb_1 c2c6a2077627 Up 15 seconds
messenger-lab4_postgres_1 a0b192c17952 Up 16 seconds (healthy)
messenger-lab4_redis_1 f787abf8c4bf Up 16 seconds (healthy)
messenger-lab4_s3_1 49ff0b0c9d92 Up 16 seconds (healthy)
messenger-lab4_api_1 8535d7918d12 Up 15 seconds (healthy)
messenger-lab4_api_2 ad1700491457 Up 16 seconds (healthy)
messenger-lab4_lb_1 0bed57f994fa Up 16 seconds
messenger-lab4_postgres_1 53c091ca7f0a Up 15 seconds (healthy)
messenger-lab4_redis_1 e0cfe44337f5 Up 15 seconds (healthy)
messenger-lab4_s3_1 8548fb0864af Up 16 seconds (healthy)
messenger-lab4_pgdata
messenger-lab4_redisdata
messenger-lab4_s3data
+20
View File
@@ -0,0 +1,20 @@
2026-11-20T04:15:49+03:00
bucket privacy and disabled console (checks outside the application API)
$ wget http://s3:9000/messenger-attachments/chats/.../... (anonymous, from backend network)
Connecting to s3:9000 (10.89.5.36:9000)
HTTP/1.1 403 Forbidden
wget: server returned error: HTTP/1.1 403 Forbidden
$ anonymous bucket listing
Connecting to s3:9000 (10.89.5.36:9000)
HTTP/1.1 403 Forbidden
wget: server returned error: HTTP/1.1 403 Forbidden
$ console port check inside s3 container
console_http=000
console-closed
$ published ports
messenger-lab4_api_2 8080/tcp
messenger-lab4_s3_1 9000-9001/tcp
+123
View File
@@ -0,0 +1,123 @@
2026-11-20T04:13:27+03:00
reconciler drill: baseline, grace period, dry-run, apply, repeat
$ scripts/reconcile.sh # dry-run, grace 1h, clean baseline
{
"prefix": "chats/",
"grace_seconds": 3600,
"dry_run": true,
"scanned_objects": 15,
"scanned_rows": 15,
"truncated": false,
"orphans": [],
"missing": [],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 7
}
$ scripts/s3tool.sh put chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 - < scripts/fixtures/sample.jpg
put chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000 (653 bytes)
$ scripts/reconcile.sh # fresh orphan stays: grace 1h
{
"prefix": "chats/",
"grace_seconds": 3600,
"dry_run": true,
"scanned_objects": 16,
"scanned_rows": 15,
"truncated": false,
"orphans": [],
"missing": [],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 7
}
$ scripts/reconcile.sh -grace 0s # orphan detected, dry-run keeps it
{
"prefix": "chats/",
"grace_seconds": 0,
"dry_run": true,
"scanned_objects": 16,
"scanned_rows": 15,
"truncated": false,
"orphans": [
{
"key": "chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000",
"size_bytes": 653,
"last_modified": "2026-11-20T01:13:27Z",
"age_seconds": 0
}
],
"missing": [],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 6
}
$ scripts/reconcile.sh -grace 0s -dry-run=false # apply
{
"prefix": "chats/",
"grace_seconds": 0,
"dry_run": false,
"scanned_objects": 16,
"scanned_rows": 15,
"truncated": false,
"orphans": [
{
"key": "chats/aaaaaaaa-0000-4000-8000-000000000000/bbbbbbbb-0000-4000-8000-000000000000",
"size_bytes": 653,
"last_modified": "2026-11-20T01:13:27Z",
"age_seconds": 3
}
],
"missing": [],
"deleted_objects": 1,
"delete_failures": 0,
"duration_ms": 12
}
$ scripts/reconcile.sh -grace 0s -dry-run=false # repeat: nothing left
{
"prefix": "chats/",
"grace_seconds": 0,
"dry_run": false,
"scanned_objects": 15,
"scanned_rows": 15,
"truncated": false,
"orphans": [],
"missing": [],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 7
}
missing drill: metadata row without object
$ scripts/s3tool.sh delete chats/9452049e-b86c-4d9a-8060-d98f6966b956/97121cfe-6b29-4227-867f-3188e72a725c
deleted chats/9452049e-b86c-4d9a-8060-d98f6966b956/97121cfe-6b29-4227-867f-3188e72a725c
$ scripts/reconcile.sh -grace 0s # reports missing, keeps metadata
{
"prefix": "chats/",
"grace_seconds": 0,
"dry_run": true,
"scanned_objects": 14,
"scanned_rows": 15,
"truncated": false,
"orphans": [],
"missing": [
{
"id": "97121cfe-6b29-4227-867f-3188e72a725c",
"object_key": "chats/9452049e-b86c-4d9a-8060-d98f6966b956/97121cfe-6b29-4227-867f-3188e72a725c",
"chat_id": "9452049e-b86c-4d9a-8060-d98f6966b956",
"created_at": "2026-11-20T01:11:14Z"
}
],
"deleted_objects": 0,
"delete_failures": 0,
"duration_ms": 6
}
metadata rows still present: 1
+1 -1
View File
@@ -1,4 +1,4 @@
2026-11-20T03:51:29+03:00
2026-11-20T04:20:26+03:00
make test
python3 tests/smoke.py --base-url "http://localhost:8080"
+4 -16
View File
@@ -1,17 +1,16 @@
2026-11-20T03:53:23+03:00
2026-11-20T04:18:15+03:00
object bytes live in the RustFS volume, metadata in PostgreSQL
id | object_key | size_bytes | sha256
--------------------------------------+---------------------------------------------------------------------------------+------------+------------------------------------------------------------------
0df7c6fd-6ac6-488b-9803-73fee496586f | chats/70682cfc-bf33-4f15-8de6-e85972fc95e0/0df7c6fd-6ac6-488b-9803-73fee496586f | 111817 | 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75
3dbc5415-9791-4983-a253-bc71fdc79a24 | chats/3ec617cd-fddf-4ae1-ad9d-37a4bb319b76/3dbc5415-9791-4983-a253-bc71fdc79a24 | 126609 | 518fd54cb19f8ceaca99139652fb96ed3a5d502392450846c832924b1722d2bf
(1 row)
S3 volume listing for that object
/data/messenger-attachments/chats/70682cfc-bf33-4f15-8de6-e85972fc95e0/0df7c6fd-6ac6-488b-9803-73fee496586f:
total 112
-rw-r--r-- 1 rustfs rustfs 112398 Nov 20 00:52 xl.meta
total 128
-rw-r--r-- 1 rustfs rustfs 127190 Nov 20 01:17 xl.meta
attachments table columns (no binary content)
@@ -31,14 +30,3 @@ attachments table columns (no binary content)
updated_at | timestamp with time zone
(12 rows)
anonymous S3 GET without credentials
Connecting to s3:9000 (10.89.5.10:9000)
HTTP/1.1 403 Forbidden
wget: server returned error: HTTP/1.1 403 Forbidden
admin console port (must be closed)
000
console-closed
+29 -16
View File
@@ -1,6 +1,9 @@
2026-11-20T03:54:42+03:00
2026-11-20T04:20:28+03:00
make test-go
? messenger/cmd/reconcile [no test files]
? messenger/cmd/s3-init [no test files]
? messenger/cmd/s3tool [no test files]
? messenger/cmd/server [no test files]
? messenger/internal/config [no test files]
? messenger/internal/domain [no test files]
@@ -11,36 +14,46 @@ make test-go
=== RUN TestWrongPassword
--- PASS: TestWrongPassword (0.10s)
=== RUN TestRedisUnavailable
redis: 2026/11/20 03:54:46 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:46 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:47 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:47 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:47 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:48 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
--- PASS: TestRedisUnavailable (2.81s)
redis: 2026/11/20 04:20:32 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 04:20:32 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 04:20:33 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 04:20:33 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 04:20:34 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 04:20:34 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
--- PASS: TestRedisUnavailable (2.83s)
=== RUN TestDatabaseUnavailable
--- PASS: TestDatabaseUnavailable (0.17s)
=== RUN TestInvalidJSON
--- PASS: TestInvalidJSON (0.01s)
=== RUN TestForeignChat
--- PASS: TestForeignChat (0.18s)
--- PASS: TestForeignChat (0.16s)
=== RUN TestEmptyMessage
redis: 2026/11/20 03:54:48 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 04:20:35 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
--- PASS: TestEmptyMessage (0.08s)
=== RUN TestDuplicateUsername
--- PASS: TestDuplicateUsername (0.09s)
--- PASS: TestDuplicateUsername (0.10s)
=== RUN TestPaginationMultiplePages
--- PASS: TestPaginationMultiplePages (0.09s)
--- PASS: TestPaginationMultiplePages (0.08s)
=== RUN TestConcurrentMessages
--- PASS: TestConcurrentMessages (0.57s)
--- PASS: TestConcurrentMessages (0.59s)
PASS
ok messenger/internal/handler 7.289s
ok messenger/internal/handler 7.325s
=== RUN TestPlanDetectsOrphansAndMissing
--- PASS: TestPlanDetectsOrphansAndMissing (0.00s)
=== RUN TestPlanHonoursGraceAndPrefix
--- PASS: TestPlanHonoursGraceAndPrefix (0.00s)
=== RUN TestPlanNeverDeletesReferencedObjects
--- PASS: TestPlanNeverDeletesReferencedObjects (0.00s)
=== RUN TestPlanOutputIsSorted
--- PASS: TestPlanOutputIsSorted (0.00s)
PASS
ok messenger/internal/reconcile 0.002s
=== RUN TestRegisterUsernameRace
--- PASS: TestRegisterUsernameRace (0.05s)
=== RUN TestCreateChatRollback
--- PASS: TestCreateChatRollback (0.01s)
--- PASS: TestCreateChatRollback (0.02s)
PASS
ok messenger/internal/repository/postgres 0.072s
ok messenger/internal/repository/postgres 0.071s
? messenger/internal/repository/redis [no test files]
? messenger/internal/repository/s3 [no test files]
? messenger/internal/router [no test files]
+32
View File
@@ -0,0 +1,32 @@
{
"concurrency": 10,
"uploads": 10,
"fixture": "large.png",
"fixture_bytes": 126609,
"elapsed_seconds": 1.235,
"successful": 10,
"failed": 0,
"failures": [],
"latency_ms": {
"p50": 663.7,
"p95": 1224.7,
"p99": 1224.7
},
"api_containers": [
"messenger-lab4_api_1",
"messenger-lab4_api_2"
],
"peak_bytes_before": {
"messenger-lab4_api_1": 5050368,
"messenger-lab4_api_2": 4820992
},
"peak_bytes_after": {
"messenger-lab4_api_1": 368119808,
"messenger-lab4_api_2": 368185344
},
"peak_delta_bytes": {
"messenger-lab4_api_1": 363069440,
"messenger-lab4_api_2": 363364352
},
"peak_max_mb": 351.13
}
+21
View File
@@ -5,6 +5,7 @@ go 1.27.1
require (
github.com/go-chi/chi/v5 v5.3.2
github.com/jackc/pgx/v5 v5.11.0
github.com/minio/madmin-go/v3 v3.0.110
github.com/minio/minio-go/v7 v7.4.1
github.com/redis/go-redis/v9 v9.23.0
golang.org/x/crypto v0.58.0
@@ -13,6 +14,9 @@ require (
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
@@ -20,11 +24,27 @@ require (
github.com/klauspost/compress v1.19.2 // indirect
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
github.com/klauspost/crc32 v1.3.0 // indirect
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/minio/crc64nvme v1.1.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.63.0 // indirect
github.com/prometheus/procfs v0.16.0 // indirect
github.com/prometheus/prom2json v1.4.2 // indirect
github.com/prometheus/prometheus v0.303.0 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/safchain/ethtool v0.5.10 // indirect
github.com/secure-io/sio-go v0.3.1 // indirect
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tinylib/msgp v1.6.4 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zeebo/xxh3 v1.1.0 // indirect
go.uber.org/atomic v1.12.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
@@ -32,5 +52,6 @@ require (
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/text v0.43.0 // indirect
google.golang.org/protobuf v1.36.12 // indirect
gopkg.in/ini.v1 v1.67.3 // indirect
)
+62
View File
@@ -6,10 +6,22 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-chi/chi/v5 v5.3.2 h1:5YQkICvTCSZ25hoRsyJazN0scjzKGiu4VAUc7H1o1nY=
github.com/go-chi/chi/v5 v5.3.2/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@@ -27,19 +39,49 @@ github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV91
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 h1:PpXWgLPs+Fqr325bN2FD2ISlRRztXibcX6e8f5FR5Dc=
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
github.com/minio/madmin-go/v3 v3.0.110 h1:FIYekj7YPc430ffpXFWiUtyut3qBt/unIAcDzJn9H5M=
github.com/minio/madmin-go/v3 v3.0.110/go.mod h1:WOe2kYmYl1OIlY2DSRHVQ8j1v4OItARQ6jGyQqcCud8=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w=
github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/common v0.63.0 h1:YR/EIY1o3mEFP/kZCD7iDMnLPlGyuU2Gb3HIcXnA98k=
github.com/prometheus/common v0.63.0/go.mod h1:VVFF/fBIoToEnWRVkYoXEkq3R3paCoxG9PXP74SnV18=
github.com/prometheus/procfs v0.16.0 h1:xh6oHhKwnOJKMYiYBDWmkHqQPyiY40sny36Cmx2bbsM=
github.com/prometheus/procfs v0.16.0/go.mod h1:8veyXUu3nGP7oaCxhX6yeaM5u4stL2FeMXnCqhDthZg=
github.com/prometheus/prom2json v1.4.2 h1:PxCTM+Whqi/eykO1MKsEL0p/zMpxp9ybpsmdFamw6po=
github.com/prometheus/prom2json v1.4.2/go.mod h1:zuvPm7u3epZSbXPWHny6G+o8ETgu6eAK3oPr6yFkRWE=
github.com/prometheus/prometheus v0.303.0 h1:wsNNsbd4EycMCphYnTmNY9JASBVbp7NWwJna857cGpA=
github.com/prometheus/prometheus v0.303.0/go.mod h1:8PMRi+Fk1WzopMDeb0/6hbNs9nV6zgySkU/zds5Lu3o=
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/safchain/ethtool v0.5.10 h1:Im294gZtuf4pSGJRAOGKaASNi3wMeFaGaWuSaomedpc=
github.com/safchain/ethtool v0.5.10/go.mod h1:w9jh2Lx7YBR4UwzLkzCmWl85UY0W2uZdd7/DckVE5+c=
github.com/secure-io/sio-go v0.3.1 h1:dNvY9awjabXTYGsTF1PiCySl9Ltofk9GA3VdWlo7rRc=
github.com/secure-io/sio-go v0.3.1/go.mod h1:+xbkjDzPjwh4Axd07pRKSNriS9SCiYksWnZqdnfpQxs=
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
@@ -54,6 +96,12 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4=
github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso=
github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
@@ -62,16 +110,30 @@ go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
+54 -34
View File
@@ -10,19 +10,21 @@ import (
)
type Config struct {
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKeyID string
S3SecretAccessKey string
MaxUploadBytes int64
MaxImagePixels int64
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKeyID string
S3SecretAccessKey string
MaxUploadBytes int64
MaxImagePixels int64
MaxConcurrentUploads int
MaxConcurrentDecodes int
}
func Load() (Config, error) {
@@ -30,27 +32,35 @@ func Load() (Config, error) {
if err != nil || ttl <= 0 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
}
maxUploadBytes, err := positiveInt("MAX_UPLOAD_BYTES", "10485760")
maxUploadBytes, err := positiveInt64("MAX_UPLOAD_BYTES", "10485760")
if err != nil {
return Config{}, err
}
maxImagePixels, err := positiveInt("MAX_IMAGE_PIXELS", "40000000")
maxImagePixels, err := positiveInt64("MAX_IMAGE_PIXELS", "40000000")
if err != nil {
return Config{}, err
}
databaseURL, err := readSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
maxConcurrentUploads, err := positiveInt("MAX_CONCURRENT_UPLOADS", "8")
if err != nil {
return Config{}, err
}
cacheURL, err := readSecret("CACHE_URL_FILE", "/run/secrets/cache_url")
maxConcurrentDecodes, err := positiveInt("MAX_CONCURRENT_DECODES", "1")
if err != nil {
return Config{}, err
}
accessKeyID, err := readSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
databaseURL, err := ReadSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
if err != nil {
return Config{}, err
}
secretAccessKey, err := readSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
cacheURL, err := ReadSecret("CACHE_URL_FILE", "/run/secrets/cache_url")
if err != nil {
return Config{}, err
}
accessKeyID, err := ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return Config{}, err
}
secretAccessKey, err := ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return Config{}, err
}
@@ -66,25 +76,27 @@ func Load() (Config, error) {
return Config{}, fmt.Errorf("S3_ENDPOINT must not be empty")
}
return Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
S3Endpoint: endpoint,
S3Region: getenv("S3_REGION", "us-east-1"),
S3Bucket: bucket,
S3AccessKeyID: accessKeyID,
S3SecretAccessKey: secretAccessKey,
MaxUploadBytes: maxUploadBytes,
MaxImagePixels: maxImagePixels,
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
S3Endpoint: endpoint,
S3Region: getenv("S3_REGION", "us-east-1"),
S3Bucket: bucket,
S3AccessKeyID: accessKeyID,
S3SecretAccessKey: secretAccessKey,
MaxUploadBytes: maxUploadBytes,
MaxImagePixels: maxImagePixels,
MaxConcurrentUploads: maxConcurrentUploads,
MaxConcurrentDecodes: maxConcurrentDecodes,
}, nil
}
func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) }
func positiveInt(key, def string) (int64, error) {
func positiveInt64(key, def string) (int64, error) {
value, err := strconv.ParseInt(getenv(key, def), 10, 64)
if err != nil || value <= 0 {
return 0, fmt.Errorf("%s must be a positive integer", key)
@@ -92,7 +104,15 @@ func positiveInt(key, def string) (int64, error) {
return value, nil
}
func readSecret(key, def string) (string, error) {
func positiveInt(key, def string) (int, error) {
value, err := strconv.Atoi(getenv(key, def))
if err != nil || value <= 0 {
return 0, fmt.Errorf("%s must be a positive integer", key)
}
return value, nil
}
func ReadSecret(key, def string) (string, error) {
path := getenv(key, def)
b, err := os.ReadFile(path)
if err != nil {
+1 -1
View File
@@ -90,7 +90,7 @@ func newStack(t *testing.T, dbURL, redisURL string, ttl time.Duration) *httptest
auth := service.NewAuthService(store, sessions, ttl)
chats := service.NewChatService(store)
messages := service.NewMessageService(chats, store)
attachments := service.NewAttachmentService(chats, store, newStubObjectStore(), 10485760, 40000000)
attachments := service.NewAttachmentService(chats, store, newStubObjectStore(), 10485760, 40000000, 8, 2)
h := handler.New(users, auth, chats, messages, attachments, []handler.Pinger{store.Ping, sessions.Ping}, slog.New(slog.DiscardHandler))
srv := httptest.NewServer(router.New(h, "test"))
t.Cleanup(srv.Close)
+97
View File
@@ -0,0 +1,97 @@
package reconcile
import (
"sort"
"strings"
"time"
"uuid"
)
type Object struct {
Key string
Size int64
LastModified time.Time
}
type Metadata struct {
ID uuid.UUID
ObjectKey string
ChatID uuid.UUID
CreatedAt time.Time
}
type Orphan struct {
Key string `json:"key"`
SizeBytes int64 `json:"size_bytes"`
LastModified string `json:"last_modified"`
AgeSeconds int64 `json:"age_seconds"`
}
type Missing struct {
ID string `json:"id"`
ObjectKey string `json:"object_key"`
ChatID string `json:"chat_id"`
CreatedAt string `json:"created_at"`
}
type Report struct {
Prefix string `json:"prefix"`
GraceSeconds int64 `json:"grace_seconds"`
DryRun bool `json:"dry_run"`
ScannedObjects int `json:"scanned_objects"`
ScannedRows int `json:"scanned_rows"`
Truncated bool `json:"truncated"`
Orphans []Orphan `json:"orphans"`
Missing []Missing `json:"missing"`
Deleted int `json:"deleted_objects"`
DeleteFailures int `json:"delete_failures"`
DurationMS int64 `json:"duration_ms"`
}
func Plan(objects []Object, rows []Metadata, prefix string, now time.Time, grace time.Duration) ([]Orphan, []Missing) {
referenced := make(map[string]struct{}, len(rows))
existing := make(map[string]struct{}, len(objects))
for _, row := range rows {
referenced[row.ObjectKey] = struct{}{}
}
for _, object := range objects {
existing[object.Key] = struct{}{}
}
orphans := make([]Orphan, 0)
for _, object := range objects {
if !strings.HasPrefix(object.Key, prefix) {
continue
}
if _, ok := referenced[object.Key]; ok {
continue
}
age := now.Sub(object.LastModified)
if age < grace {
continue
}
orphans = append(orphans, Orphan{
Key: object.Key,
SizeBytes: object.Size,
LastModified: object.LastModified.UTC().Format(time.RFC3339),
AgeSeconds: int64(age.Seconds()),
})
}
missing := make([]Missing, 0)
for _, row := range rows {
if _, ok := existing[row.ObjectKey]; ok {
continue
}
missing = append(missing, Missing{
ID: row.ID.String(),
ObjectKey: row.ObjectKey,
ChatID: row.ChatID.String(),
CreatedAt: row.CreatedAt.UTC().Format(time.RFC3339),
})
}
sort.Slice(orphans, func(i, j int) bool { return orphans[i].Key < orphans[j].Key })
sort.Slice(missing, func(i, j int) bool { return missing[i].ObjectKey < missing[j].ObjectKey })
return orphans, missing
}
+75
View File
@@ -0,0 +1,75 @@
package reconcile
import (
"testing"
"time"
"uuid"
)
func TestPlanDetectsOrphansAndMissing(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
usedID, missingID := uuid.New(), uuid.New()
objects := []Object{
{Key: "chats/a/used", Size: 10, LastModified: now.Add(-2 * time.Hour)},
{Key: "chats/a/orphan-old", Size: 20, LastModified: now.Add(-2 * time.Hour)},
{Key: "chats/a/orphan-fresh", Size: 30, LastModified: now.Add(-1 * time.Minute)},
{Key: "other/prefix/ignored", Size: 40, LastModified: now.Add(-2 * time.Hour)},
}
rows := []Metadata{
{ID: usedID, ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
{ID: missingID, ObjectKey: "chats/a/missing", ChatID: uuid.New(), CreatedAt: now.Add(-2 * time.Hour)},
}
orphans, missing := Plan(objects, rows, "chats/", now, time.Hour)
if len(orphans) != 1 || orphans[0].Key != "chats/a/orphan-old" {
t.Fatalf("orphans = %+v, want only chats/a/orphan-old", orphans)
}
if len(missing) != 1 || missing[0].ObjectKey != "chats/a/missing" {
t.Fatalf("missing = %+v, want only chats/a/missing", missing)
}
}
func TestPlanHonoursGraceAndPrefix(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
objects := []Object{
{Key: "chats/a/fresh", Size: 1, LastModified: now.Add(-30 * time.Second)},
{Key: "outside/key", Size: 1, LastModified: now.Add(-48 * time.Hour)},
}
orphans, _ := Plan(objects, nil, "chats/", now, time.Hour)
if len(orphans) != 0 {
t.Fatalf("orphans = %+v, want none", orphans)
}
orphans, _ = Plan(objects, nil, "chats/", now, 0)
if len(orphans) != 1 || orphans[0].Key != "chats/a/fresh" {
t.Fatalf("orphans with zero grace = %+v, want chats/a/fresh", orphans)
}
}
func TestPlanNeverDeletesReferencedObjects(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
objects := []Object{{Key: "chats/a/used", Size: 1, LastModified: now.Add(-100 * time.Hour)}}
rows := []Metadata{{ID: uuid.New(), ObjectKey: "chats/a/used", ChatID: uuid.New(), CreatedAt: now.Add(-100 * time.Hour)}}
orphans, missing := Plan(objects, rows, "chats/", now, 0)
if len(orphans) != 0 {
t.Fatalf("orphans = %+v, want none for referenced object", orphans)
}
if len(missing) != 0 {
t.Fatalf("missing = %+v, want none", missing)
}
}
func TestPlanOutputIsSorted(t *testing.T) {
now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.UTC)
objects := []Object{
{Key: "chats/a/z", LastModified: now.Add(-time.Hour)},
{Key: "chats/a/b", LastModified: now.Add(-time.Hour)},
{Key: "chats/a/m", LastModified: now.Add(-time.Hour)},
}
orphans, _ := Plan(objects, nil, "chats/", now, 0)
if len(orphans) != 3 || orphans[0].Key != "chats/a/b" || orphans[2].Key != "chats/a/z" {
t.Fatalf("orphans are not sorted: %+v", orphans)
}
}
+54 -9
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -14,19 +15,30 @@ import (
"messenger/internal/domain"
)
type ObjectInfo struct {
Key string
Size int64
LastModified time.Time
}
type Store struct {
client *minio.Client
bucket string
region string
}
func New(endpoint, region, accessKeyID, secretAccessKey, bucket string) (*Store, error) {
func EndpointHost(endpoint string) (string, bool) {
host, secure := endpoint, false
if parsed, err := url.Parse(endpoint); err == nil && parsed.Host != "" {
host = parsed.Host
secure = parsed.Scheme == "https"
}
client, err := minio.New(strings.TrimRight(host, "/"), &minio.Options{
return strings.TrimRight(host, "/"), secure
}
func New(endpoint, region, accessKeyID, secretAccessKey, bucket string) (*Store, error) {
host, secure := EndpointHost(endpoint)
client, err := minio.New(host, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Region: region,
Secure: secure,
@@ -56,17 +68,18 @@ func (s *Store) EnsureBucket(ctx context.Context) error {
}
func (s *Store) Ping(ctx context.Context) error {
_, err := s.client.BucketExists(ctx, s.bucket)
return s.wrap(err)
exists, err := s.client.BucketExists(ctx, s.bucket)
if err != nil {
return s.wrap(err)
}
if !exists {
return fmt.Errorf("%w: bucket is missing", domain.ErrUnavailable)
}
return nil
}
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
if err != nil && minio.ToErrorResponse(err).Code == "NoSuchBucket" {
if ensureErr := s.EnsureBucket(ctx); ensureErr == nil {
_, err = s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
}
}
return s.wrap(err)
}
@@ -86,6 +99,38 @@ func (s *Store) Delete(ctx context.Context, key string) error {
return s.wrap(s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}))
}
func (s *Store) List(ctx context.Context, prefix string, limit int) ([]ObjectInfo, bool, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
objects := make([]ObjectInfo, 0, 64)
for info := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
if info.Err != nil {
return nil, false, s.wrap(info.Err)
}
if len(objects) >= limit {
return objects, true, nil
}
objects = append(objects, ObjectInfo{Key: info.Key, Size: info.Size, LastModified: info.LastModified})
}
return objects, false, nil
}
func (s *Store) Buckets(ctx context.Context) ([]string, error) {
buckets, err := s.client.ListBuckets(ctx)
if err != nil {
return nil, s.wrap(err)
}
names := make([]string, len(buckets))
for i, bucket := range buckets {
names[i] = bucket.Name
}
return names, nil
}
func (s *Store) RemoveBucket(ctx context.Context, bucket string) error {
return s.wrap(s.client.RemoveBucket(ctx, bucket))
}
func (s *Store) wrap(err error) error {
if err == nil {
return nil
+37 -3
View File
@@ -31,10 +31,21 @@ type AttachmentService struct {
store ObjectStore
maxBytes int64
maxPixels int64
uploads chan struct{}
decodes chan struct{}
}
func NewAttachmentService(chats *ChatService, repo AttachmentRepository, store ObjectStore, maxBytes, maxPixels int64) *AttachmentService {
return &AttachmentService{chats: chats, repo: repo, store: store, maxBytes: maxBytes, maxPixels: maxPixels}
func NewAttachmentService(chats *ChatService, repo AttachmentRepository, store ObjectStore,
maxBytes, maxPixels int64, maxConcurrentUploads, maxConcurrentDecodes int) *AttachmentService {
return &AttachmentService{
chats: chats,
repo: repo,
store: store,
maxBytes: maxBytes,
maxPixels: maxPixels,
uploads: make(chan struct{}, maxConcurrentUploads),
decodes: make(chan struct{}, maxConcurrentDecodes),
}
}
func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatID uuid.UUID, filename, contentType string, body io.Reader) (domain.Attachment, error) {
@@ -47,6 +58,10 @@ func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatI
if contentType != domain.ContentPNG && contentType != domain.ContentJPEG {
return domain.Attachment{}, fmt.Errorf("%w: content type must be image/png or image/jpeg", domain.ErrUnsupportedMedia)
}
if err := acquire(ctx, s.uploads); err != nil {
return domain.Attachment{}, err
}
defer release(s.uploads)
raw, err := readLimited(body, s.maxBytes)
if err != nil {
return domain.Attachment{}, err
@@ -57,7 +72,7 @@ func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatI
if err := checkPixels(raw, s.maxPixels); err != nil {
return domain.Attachment{}, err
}
if err := checkIntegrity(raw); err != nil {
if err := s.decode(ctx, raw); err != nil {
return domain.Attachment{}, err
}
@@ -124,6 +139,25 @@ func (s *AttachmentService) Open(ctx context.Context, actor domain.User, id uuid
return attachment, content, nil
}
func (s *AttachmentService) decode(ctx context.Context, raw []byte) error {
if err := acquire(ctx, s.decodes); err != nil {
return err
}
defer release(s.decodes)
return checkIntegrity(raw)
}
func acquire(ctx context.Context, sem chan struct{}) error {
select {
case sem <- struct{}{}:
return nil
case <-ctx.Done():
return fmt.Errorf("%w: request cancelled while waiting for capacity", domain.ErrUnavailable)
}
}
func release(sem chan struct{}) { <-sem }
func checkFilename(filename string) error {
if n := utf8.RuneCountInString(filename); n < 1 || n > maxFilenameRunes {
return fmt.Errorf("%w: filename must be 1 to 255 characters", domain.ErrInvalidInput)
+5
View File
@@ -13,11 +13,16 @@ password=$(cat secrets/postgres_password)
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$password" > secrets/database_url
[ -f secrets/cache_url ] ||
printf 'redis://redis:6379/0\n' > secrets/cache_url
[ -f secrets/s3_root_access_key ] ||
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_root_access_key
[ -f secrets/s3_root_secret_key ] ||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_root_secret_key
[ -f secrets/s3_access_key_id ] ||
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_access_key_id
[ -f secrets/s3_secret_access_key ] ||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_secret_access_key
chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url \
secrets/s3_root_access_key secrets/s3_root_secret_key \
secrets/s3_access_key_id secrets/s3_secret_access_key
echo "secrets ready in ./secrets"
+19
View File
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
PROJECT=${PROJECT:-messenger-lab4}
IMAGE=${IMAGE:-localhost/messenger-api:lab4}
$ENGINE run --rm --network "${PROJECT}_backend" \
--entrypoint /usr/local/bin/reconcile \
-v "$PWD/secrets/database_url:/run/secrets/database_url:ro" \
-v "$PWD/secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro" \
-v "$PWD/secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro" \
-e S3_ENDPOINT="${S3_ENDPOINT:-http://s3:9000}" \
-e S3_REGION="${S3_REGION:-us-east-1}" \
-e S3_BUCKET="${S3_BUCKET:-messenger-attachments}" \
-e DATABASE_URL_FILE=/run/secrets/database_url \
-e S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id \
-e S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key \
"$IMAGE" "$@"
+17
View File
@@ -0,0 +1,17 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
PROJECT=${PROJECT:-messenger-lab4}
IMAGE=${IMAGE:-localhost/messenger-api:lab4}
$ENGINE run --rm -i --network "${PROJECT}_backend" \
--entrypoint /usr/local/bin/s3tool \
-v "$PWD/secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro" \
-v "$PWD/secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro" \
-e S3_ENDPOINT="${S3_ENDPOINT:-http://s3:9000}" \
-e S3_REGION="${S3_REGION:-us-east-1}" \
-e S3_BUCKET="${S3_BUCKET:-messenger-attachments}" \
-e S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id \
-e S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key \
"$IMAGE" "$@"
+12
View File
@@ -0,0 +1,12 @@
CREATE OR REPLACE FUNCTION inject_attachment_insert_failure() RETURNS trigger AS $$
BEGIN
RAISE EXCEPTION 'injected attachment insert failure' USING ERRCODE = '57014';
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS inject_attachment_insert_failure ON attachments;
CREATE TRIGGER inject_attachment_insert_failure
BEFORE INSERT ON attachments
FOR EACH ROW
EXECUTE FUNCTION inject_attachment_insert_failure();
+2
View File
@@ -0,0 +1,2 @@
DROP TRIGGER IF EXISTS inject_attachment_insert_failure ON attachments;
DROP FUNCTION IF EXISTS inject_attachment_insert_failure();
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env python3
"""Concurrent upload experiment with API memory measurement; creates synthetic data."""
import json
import math
import shutil
import subprocess
import sys
import threading
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, Failure, arguments, check, run
ROOT = Path(__file__).resolve().parents[1]
def engine():
return 'podman' if shutil.which('podman') else 'docker'
def api_containers(project):
result = subprocess.run(
[engine(), 'ps',
'--filter', 'label=com.docker.compose.project=' + project,
'--filter', 'label=com.docker.compose.service=api',
'--format', '{{.Names}}'],
capture_output=True, text=True, check=False)
check(result.returncode == 0, 'cannot list api containers: ' + result.stderr.strip())
return sorted(line.strip() for line in result.stdout.splitlines() if line.strip())
def peak_bytes(container):
for path in ('/sys/fs/cgroup/memory.peak', '/sys/fs/cgroup/memory/memory.max_usage_in_bytes'):
result = subprocess.run([engine(), 'exec', container, 'cat', path], capture_output=True, text=True, check=False)
if result.returncode == 0 and result.stdout.strip().isdigit():
return int(result.stdout.strip())
return None
def main():
def configure(p):
p.add_argument('--concurrency', type=int, default=10)
p.add_argument('--file', default=str(ROOT / 'scripts/fixtures/large.png'))
p.add_argument('--project', default='messenger-lab4')
p.add_argument('--output', default=None)
args = arguments('Concurrent attachment uploads with peak memory measurement', configure)
check(1 <= args.concurrency <= 64, 'concurrency must be 1..64')
source = Path(args.file)
check(source.is_file(), 'upload fixture not found: ' + str(source))
content = source.read_bytes()
containers = api_containers(args.project)
check(bool(containers), 'no api containers found for project ' + args.project)
baseline = {name: peak_bytes(name) for name in containers}
c = Client()
alice, _, _, chat = c.bootstrap()
actor = {'id': alice['id'], 'token': alice['token']}
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + source.name
barrier = threading.Barrier(args.concurrency)
lock = threading.Lock()
latencies, failures = [], []
def worker(index):
local = Client(timeout=120)
barrier.wait()
started = time.monotonic()
try:
response = local.call('POST', path, 201, actor=actor, raw=content,
headers={'Content-Type': 'image/png'}, schema='Attachment')
with lock:
latencies.append(time.monotonic() - started)
check(response['json']['size_bytes'] == len(content), 'uploaded size mismatch')
except Failure as exc:
with lock:
failures.append(str(exc))
started = time.monotonic()
threads = [threading.Thread(target=worker, args=(i,)) for i in range(args.concurrency)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
elapsed = time.monotonic() - started
peaks = {name: peak_bytes(name) for name in containers}
peak_delta = {}
for name in containers:
before = baseline[name]
after = peaks[name]
if before is None or after is None:
peak_delta[name] = None
else:
peak_delta[name] = after - before
ordered = sorted(latencies)
def percentile(p):
return round(1000 * ordered[max(0, math.ceil(p * len(ordered)) - 1)], 1) if ordered else None
report = {
'concurrency': args.concurrency,
'uploads': args.concurrency,
'fixture': source.name,
'fixture_bytes': len(content),
'elapsed_seconds': round(elapsed, 3),
'successful': len(latencies),
'failed': len(failures),
'failures': failures[:3],
'latency_ms': {'p50': percentile(0.50), 'p95': percentile(0.95), 'p99': percentile(0.99)},
'api_containers': containers,
'peak_bytes_before': baseline,
'peak_bytes_after': peaks,
'peak_delta_bytes': peak_delta,
'peak_max_mb': round(max(v for v in peaks.values() if v is not None) / (1024 * 1024), 2) if any(v is not None for v in peaks.values()) else None,
}
text = json.dumps(report, indent=2)
if args.output:
destination = Path(args.output)
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(text + '\n')
print(text)
check(not failures, 'some uploads failed')
check(len(latencies) == args.concurrency, 'not all uploads succeeded')
if __name__ == '__main__':
run(main)
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Single binary upload for failure drills; asserts the expected status code."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, arguments, check, run
ROOT = Path(__file__).resolve().parents[1]
def main():
def configure(p):
p.add_argument('--expect', type=int, default=201)
p.add_argument('--file', default=str(ROOT / 'scripts/fixtures/sample.jpg'))
p.add_argument('--content-type', default='image/jpeg')
args = arguments('Single upload used by compensation drills; creates synthetic data', configure)
source = Path(args.file)
check(source.is_file(), 'file not found: ' + str(source))
content = source.read_bytes()
c = Client()
alice, _, _, chat = c.bootstrap()
response = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + source.name,
args.expect, actor=alice, raw=content,
headers={'Content-Type': args.content_type}, schema='Attachment')
print(f'status={response["status"]}')
if response['status'] == 201:
attachment = response['json']
print(f"attachment={attachment['id']} size={attachment['size_bytes']}")
if __name__ == '__main__':
run(main)