feat: port verified messenger from lab 6
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
include Makefile
|
||||
|
||||
CLEANUP_ARGS ?= --dry-run
|
||||
SERVICES ?=
|
||||
JOB_ID ?=
|
||||
ATTACHMENT_ID ?=
|
||||
REASON ?=
|
||||
export JOB_ID ATTACHMENT_ID REASON
|
||||
|
||||
COMPOSE = docker compose --project-name highload-lab7 --env-file .env -f compose.yml
|
||||
|
||||
.PHONY: app-init app-build app-up app-start app-stop app-down app-restart app-logs app-status
|
||||
.PHONY: app-migrate app-s3-bootstrap worker-start worker-stop queue-status attachments-cleanup
|
||||
.PHONY: jobs-failed jobs-replay jobs-upgrade jobs-cleanup-staging
|
||||
.PHONY: test-backend test-pipeline frontend-build
|
||||
|
||||
|
||||
app-init:
|
||||
$(PYTHON) scripts/init_env.py
|
||||
|
||||
app-build: app-init
|
||||
$(COMPOSE) build
|
||||
|
||||
app-up: app-init
|
||||
$(COMPOSE) up -d --build --wait
|
||||
|
||||
app-start: app-init
|
||||
$(COMPOSE) up -d --no-build --wait
|
||||
|
||||
app-stop:
|
||||
$(COMPOSE) stop
|
||||
|
||||
app-down:
|
||||
$(COMPOSE) down
|
||||
|
||||
app-restart: app-init
|
||||
./scripts/restart.sh
|
||||
|
||||
app-logs:
|
||||
$(COMPOSE) logs -f --tail=100 $(SERVICES)
|
||||
|
||||
app-status:
|
||||
$(COMPOSE) ps --all
|
||||
|
||||
app-migrate: app-init
|
||||
$(COMPOSE) run --rm migrate
|
||||
|
||||
app-s3-bootstrap: app-init
|
||||
$(COMPOSE) run --rm s3-bootstrap
|
||||
|
||||
worker-start:
|
||||
$(COMPOSE) start worker
|
||||
|
||||
worker-stop:
|
||||
$(COMPOSE) stop worker
|
||||
|
||||
queue-status:
|
||||
$(COMPOSE) exec -T broker rabbitmqctl -q list_queues name messages_ready messages_unacknowledged --formatter json
|
||||
|
||||
attachments-cleanup:
|
||||
$(COMPOSE) run --rm --no-deps upload-cleanup ./messenger attachments-cleanup $(CLEANUP_ARGS)
|
||||
|
||||
jobs-failed:
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs list-failed
|
||||
|
||||
jobs-replay:
|
||||
@test -n "$$JOB_ID" && test -n "$$REASON" || { echo "Set JOB_ID=... REASON='...'"; exit 2; }
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs replay "$$JOB_ID" "$$REASON"
|
||||
|
||||
jobs-upgrade:
|
||||
@test -n "$$ATTACHMENT_ID" && test -n "$$REASON" || { echo "Set ATTACHMENT_ID=... REASON='...'"; exit 2; }
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs upgrade "$$ATTACHMENT_ID" 2 "$$REASON"
|
||||
|
||||
jobs-cleanup-staging:
|
||||
@test -n "$$JOB_ID" || { echo "Set JOB_ID=..."; exit 2; }
|
||||
$(COMPOSE) exec -T dispatcher ./messenger jobs cleanup-staging "$$JOB_ID"
|
||||
|
||||
test-backend: app-init
|
||||
$(COMPOSE) --profile test run --rm --build backend-tests
|
||||
|
||||
test-pipeline: app-init
|
||||
$(COMPOSE) --profile test run --rm --build backend-tests go test -race -count=1 -v ./backend/tests ./backend/workers -run 'TestPipeline|TestVersionedRendering|TestThumbnail|TestOptimized'
|
||||
|
||||
frontend-build:
|
||||
$(COMPOSE) exec -T frontend npm run build
|
||||
+386
@@ -0,0 +1,386 @@
|
||||
name: highload-lab7
|
||||
|
||||
services:
|
||||
redis:
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
image: redis:7.4.11-bookworm
|
||||
command:
|
||||
[
|
||||
"redis-server",
|
||||
"--appendonly",
|
||||
"yes",
|
||||
"--appendfsync",
|
||||
"everysec",
|
||||
"--dir",
|
||||
"/data",
|
||||
]
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
db:
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [data]
|
||||
image: postgres:16.10-bookworm
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
migrate:
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
environment:
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
volumes:
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
command: ["./messenger", "migrate", "up"]
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [edge, data]
|
||||
ports:
|
||||
- "127.0.0.1:9000:9000"
|
||||
build:
|
||||
context: ./infra
|
||||
dockerfile: minio.Dockerfile
|
||||
environment:
|
||||
MINIO_ROOT_USER_FILE: /run/secrets/s3_access_key_id
|
||||
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_secret_access_key
|
||||
MINIO_BROWSER: "off"
|
||||
MINIO_API_CORS_ALLOW_ORIGIN: ${S3_CORS_ORIGINS:-http://127.0.0.1:5173,http://localhost:5173}
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/minio/health/ready"]
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 30
|
||||
s3-bootstrap:
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
environment: &s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: /run/secrets/s3_root_access_key_id
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_root_secret_access_key
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:9000}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}
|
||||
S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}
|
||||
volumes:
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_root_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_root_secret_access_key:ro
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
command: ["./messenger", "s3-bootstrap"]
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
api:
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [proxy, data, telemetry]
|
||||
build: ./src
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
stop_grace_period: 15s
|
||||
environment: &api-environment
|
||||
<<: *s3-environment
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT: ${OTEL_EXPORTER_OTLP_ENDPOINT-http://otel-collector:4318}
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: ""
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: ""
|
||||
BROKER_URL_FILE: /run/secrets/broker_url
|
||||
JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5}
|
||||
JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-60}
|
||||
WORKER_MAX_PIXELS: ${WORKER_MAX_PIXELS:-8000000}
|
||||
UPLOAD_TMP_DIR: /tmp/uploads
|
||||
UPLOAD_CLEANUP_GRACE_SECONDS: ${UPLOAD_CLEANUP_GRACE_SECONDS:-600}
|
||||
# Go 1.22 sees host CPUs; keep parallelism suitable for this 0.5 CPU limit.
|
||||
GOMAXPROCS: ${API_GOMAXPROCS:-1}
|
||||
GOMEMLIMIT: 384MiB
|
||||
APP_HOST: ${APP_HOST:-0.0.0.0}
|
||||
APP_PORT: ${APP_PORT:-8080}
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
REDIS_ADDR: redis:6379
|
||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
tmpfs:
|
||||
- /tmp/uploads:rw,noexec,nosuid,size=16m,mode=1777
|
||||
volumes: &api-volumes
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/broker_url:/run/secrets/broker_url:ro
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
- ./secrets/cache_url:${CACHE_URL_FILE:-/run/secrets/cache_url}:ro
|
||||
depends_on:
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD", "./messenger", "healthcheck"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
broker:
|
||||
image: rabbitmq:4.2.0-management
|
||||
hostname: broker
|
||||
environment:
|
||||
RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: "+S 2:2"
|
||||
RABBITMQ_CTL_ERL_ARGS: "+S 1:1"
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [data]
|
||||
command:
|
||||
- sh
|
||||
- -ec
|
||||
- |
|
||||
export RABBITMQ_DEFAULT_USER="$$(cat /run/secrets/broker_user)"
|
||||
export RABBITMQ_DEFAULT_PASS="$$(cat /run/secrets/broker_password)"
|
||||
exec docker-entrypoint.sh rabbitmq-server
|
||||
volumes:
|
||||
- rabbitmq_data:/var/lib/rabbitmq
|
||||
- ./secrets/broker_user:/run/secrets/broker_user:ro
|
||||
- ./secrets/broker_password:/run/secrets/broker_password:ro
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "rabbitmq-diagnostics -q check_running && rabbitmq-diagnostics -q check_port_connectivity"]
|
||||
interval: 5s
|
||||
timeout: 10s
|
||||
start_period: 20s
|
||||
retries: 30
|
||||
worker:
|
||||
tmpfs:
|
||||
- /tmp:rw,noexec,nosuid,size=64m,mode=1777
|
||||
build: ./src
|
||||
restart: unless-stopped
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [data, telemetry]
|
||||
environment: *api-environment
|
||||
volumes: *api-volumes
|
||||
command: ["./messenger", "worker"]
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
dispatcher:
|
||||
build: ./src
|
||||
restart: unless-stopped
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data, telemetry]
|
||||
environment: *api-environment
|
||||
volumes: *api-volumes
|
||||
command: ["./messenger", "dispatcher"]
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
broker:
|
||||
condition: service_healthy
|
||||
upload-cleanup:
|
||||
restart: unless-stopped
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
command: ["./messenger", "attachments-cleanup", "--loop"]
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: ""
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: ""
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
haproxy:
|
||||
environment:
|
||||
APP_PORT: ${APP_PORT:-8080}
|
||||
API_REPLICAS: ${API_REPLICAS:-2}
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [edge, proxy]
|
||||
image: haproxy:3.2.25-alpine
|
||||
ports:
|
||||
- "127.0.0.1:${API_HTTP_PORT:-8080}:8080"
|
||||
- "127.0.0.1:8404:8404"
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"wget",
|
||||
"-q",
|
||||
"-O",
|
||||
"/dev/null",
|
||||
"http://127.0.0.1:8080/health/ready",
|
||||
]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
backend-tests:
|
||||
cpus: 2.0
|
||||
mem_limit: 1g
|
||||
networks: [data]
|
||||
profiles: ["test"]
|
||||
build:
|
||||
context: ./src
|
||||
target: builder
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_PUBLIC_ENDPOINT: http://s3:9000
|
||||
TEST_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
TEST_REDIS_ADDR: redis:6379
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_root_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_root_secret_access_key:ro
|
||||
depends_on:
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
command: ["go", "test", "-race", "-count=1", "-v", "./backend/..."]
|
||||
frontend:
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [edge]
|
||||
build:
|
||||
context: ./src/frontend
|
||||
environment:
|
||||
API_PROXY_TARGET: http://haproxy:8080
|
||||
ports:
|
||||
- "127.0.0.1:5173:5173"
|
||||
volumes:
|
||||
- ./src/frontend:/app
|
||||
- /app/node_modules
|
||||
depends_on:
|
||||
haproxy:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"node",
|
||||
"-e",
|
||||
"fetch('http://127.0.0.1:5173/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
otel-collector:
|
||||
image: otel/opentelemetry-collector-contrib:0.120.0
|
||||
networks: [telemetry]
|
||||
mem_limit: 192m
|
||||
command: ["--config=/etc/otelcol-contrib/config.yaml"]
|
||||
volumes:
|
||||
- ./infra/otel/collector.yaml:/etc/otelcol-contrib/config.yaml:ro
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:v3.2.1
|
||||
networks: [telemetry]
|
||||
command: ["--config.file=/etc/prometheus/prometheus.yml", "--storage.tsdb.path=/prometheus", "--storage.tsdb.retention.time=7d"]
|
||||
volumes:
|
||||
- ./infra/otel/prometheus.yaml:/etc/prometheus/prometheus.yml:ro
|
||||
- ./infra/otel/alerts.yaml:/etc/prometheus/alerts.yaml:ro
|
||||
- prometheus_data:/prometheus
|
||||
|
||||
loki:
|
||||
image: grafana/loki:3.4.2
|
||||
networks: [telemetry]
|
||||
command: ["-config.file=/etc/loki/lab.yaml"]
|
||||
volumes:
|
||||
- ./infra/otel/loki.yaml:/etc/loki/lab.yaml:ro
|
||||
- loki_data:/loki
|
||||
|
||||
tempo:
|
||||
image: grafana/tempo:2.7.1
|
||||
networks: [telemetry]
|
||||
command: ["-config.file=/etc/tempo/lab.yaml"]
|
||||
volumes:
|
||||
- ./infra/otel/tempo.yaml:/etc/tempo/lab.yaml:ro
|
||||
- tempo_data:/var/tempo
|
||||
|
||||
grafana:
|
||||
image: grafana/grafana:11.5.2
|
||||
networks: [telemetry, edge]
|
||||
environment:
|
||||
GF_AUTH_ANONYMOUS_ENABLED: "true"
|
||||
GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
|
||||
ports:
|
||||
- "127.0.0.1:${GRAFANA_HTTP_PORT:-3000}:3000"
|
||||
volumes:
|
||||
- ./infra/otel/grafana/provisioning:/etc/grafana/provisioning:ro
|
||||
- ./infra/otel/grafana/dashboards:/var/lib/grafana/dashboards:ro
|
||||
- grafana_data:/var/lib/grafana
|
||||
|
||||
volumes:
|
||||
prometheus_data:
|
||||
loki_data:
|
||||
tempo_data:
|
||||
grafana_data:
|
||||
rabbitmq_data:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
|
||||
networks:
|
||||
edge:
|
||||
proxy:
|
||||
internal: true
|
||||
data:
|
||||
internal: true
|
||||
telemetry:
|
||||
internal: true
|
||||
@@ -0,0 +1,49 @@
|
||||
global
|
||||
# Operator access only inside this container; no host port is published.
|
||||
stats socket ipv4@127.0.0.1:9999 level admin
|
||||
log stdout format raw local0
|
||||
maxconn 1024
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode http
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 35s
|
||||
timeout server 35s
|
||||
timeout check 1s
|
||||
# Let the client decide which failed POST is safe to repeat.
|
||||
retries 0
|
||||
|
||||
# Docker returns the current addresses of all containers of the api service.
|
||||
resolvers docker
|
||||
nameserver docker_dns 127.0.0.11:53
|
||||
accepted_payload_size 8192
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
hold nx 1s
|
||||
hold timeout 1s
|
||||
hold refused 1s
|
||||
hold other 1s
|
||||
|
||||
frontend api_entry
|
||||
bind :8080
|
||||
default_backend api_replicas
|
||||
|
||||
backend api_replicas
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
# Separate slots receive distinct Docker DNS addresses. No sticky sessions.
|
||||
server-template api "${API_REPLICAS}" "api:${APP_PORT}" check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr libc,none
|
||||
|
||||
# Local teaching aid: backend addresses, UP/DOWN state and request counters.
|
||||
listen statistics
|
||||
bind :8404
|
||||
stats enable
|
||||
stats show-legends
|
||||
stats uri /stats
|
||||
stats refresh 2s
|
||||
@@ -0,0 +1,19 @@
|
||||
FROM golang:1.24.2-bookworm AS builder
|
||||
WORKDIR /source
|
||||
RUN git init . \
|
||||
&& git remote add origin https://github.com/minio/minio.git \
|
||||
&& git fetch --depth 1 origin 0d7408fc9969caf07de6a8c3a84f9fbb10a6739e \
|
||||
&& git checkout --detach FETCH_HEAD
|
||||
RUN CGO_ENABLED=0 go build -trimpath \
|
||||
-ldflags="-s -w -X github.com/minio/minio/cmd.Version=2025-04-22T22:12:26Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-04-22T22-12-26Z -X github.com/minio/minio/cmd.CommitID=0d7408fc9969caf07de6a8c3a84f9fbb10a6739e" \
|
||||
-o /out/minio .
|
||||
|
||||
FROM debian:12.15-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& groupadd --system storage && useradd --system --gid storage storage \
|
||||
&& mkdir /data && chown storage:storage /data
|
||||
COPY --from=builder /out/minio /usr/local/bin/minio
|
||||
USER storage:storage
|
||||
ENTRYPOINT ["minio"]
|
||||
CMD ["server", "/data", "--console-address", ":9001"]
|
||||
@@ -0,0 +1,20 @@
|
||||
groups:
|
||||
- name: messenger
|
||||
interval: 5s
|
||||
rules:
|
||||
- alert: MessengerHighErrorRate
|
||||
expr: (sum(rate(messenger_http_requests_total{http_status_class="5xx"}[1m])) / sum(rate(messenger_http_requests_total[1m]))) > 0.05
|
||||
for: 15s
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: More than 5% of business requests return 5xx over one minute
|
||||
runbook: Check API error spans and PostgreSQL operations in Tempo.
|
||||
- alert: MessengerQueueLag
|
||||
expr: max(messenger_queue_oldest_seconds) > 20
|
||||
for: 15s
|
||||
labels:
|
||||
severity: warning
|
||||
annotations:
|
||||
summary: Oldest outstanding attachment job is over 20 seconds old
|
||||
runbook: Check outstanding jobs, worker saturation and consumer traces.
|
||||
@@ -0,0 +1,45 @@
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
processors:
|
||||
memory_limiter:
|
||||
check_interval: 1s
|
||||
limit_mib: 128
|
||||
spike_limit_mib: 32
|
||||
batch:
|
||||
timeout: 1s
|
||||
send_batch_size: 256
|
||||
send_batch_max_size: 512
|
||||
exporters:
|
||||
prometheus:
|
||||
endpoint: 0.0.0.0:8889
|
||||
resource_to_telemetry_conversion:
|
||||
enabled: true
|
||||
otlphttp/loki:
|
||||
endpoint: http://loki:3100/otlp
|
||||
sending_queue:
|
||||
queue_size: 64
|
||||
retry_on_failure:
|
||||
max_elapsed_time: 30s
|
||||
otlphttp/tempo:
|
||||
endpoint: http://tempo:4318
|
||||
sending_queue:
|
||||
queue_size: 64
|
||||
retry_on_failure:
|
||||
max_elapsed_time: 30s
|
||||
service:
|
||||
pipelines:
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [prometheus]
|
||||
logs:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlphttp/loki]
|
||||
traces:
|
||||
receivers: [otlp]
|
||||
processors: [memory_limiter, batch]
|
||||
exporters: [otlphttp/tempo]
|
||||
@@ -0,0 +1,513 @@
|
||||
{
|
||||
"uid": "messenger-lab6",
|
||||
"title": "Мессенджер · API и worker",
|
||||
"schemaVersion": 39,
|
||||
"refresh": "5s",
|
||||
"time": {
|
||||
"from": "now-15m",
|
||||
"to": "now"
|
||||
},
|
||||
"panels": [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "API · запросы в секунду",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"expr": "sum(rate(messenger_http_requests_total[1m]))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "reqps"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"title": "API · 5xx в секунду",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"expr": "sum(rate(messenger_http_requests_total{http_status_class=\"5xx\"}[1m])) or 0 * sum(rate(messenger_http_requests_total[1m]))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 0,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "reqps"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"title": "API · p95 длительности",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_http_duration_seconds_bucket[1m])))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 8,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"title": "Worker · задания в минуту",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"expr": "60 * sum by (job_outcome) (rate(messenger_worker_jobs_total[1m]))",
|
||||
"legendFormat": "{{job_outcome}}"
|
||||
}
|
||||
],
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 8,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"title": "Worker · p95 длительности",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"expr": "histogram_quantile(0.95, sum by (le,job_outcome) (rate(messenger_worker_duration_seconds_bucket[1m])))",
|
||||
"legendFormat": "{{job_outcome}}"
|
||||
}
|
||||
],
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 16,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"title": "API и worker · события",
|
||||
"type": "logs",
|
||||
"datasource": {
|
||||
"type": "loki",
|
||||
"uid": "lab6-loki"
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "loki",
|
||||
"uid": "lab6-loki"
|
||||
},
|
||||
"expr": "{service_name=~\"messenger-api|messenger-worker\"}",
|
||||
"legendFormat": "",
|
||||
"queryType": "range"
|
||||
}
|
||||
],
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 16,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": ""
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"title": "API · p50 длительности",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 24,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "histogram_quantile(0.5, sum by (le) (rate(messenger_http_duration_seconds_bucket[1m])))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"title": "API · p99 длительности",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 24,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "histogram_quantile(0.99, sum by (le) (rate(messenger_http_duration_seconds_bucket[1m])))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"title": "API · доля 5xx",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 32,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "(sum(rate(messenger_http_requests_total{http_status_class=\"5xx\"}[1m])) or 0 * sum(rate(messenger_http_requests_total[1m]))) / sum(rate(messenger_http_requests_total[1m]))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "percentunit"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"title": "Очередь attachments.v1 · незавершённые задания",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 32,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "max(messenger_queue_depth)",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"title": "Очередь attachments.v1 · возраст старейшего задания",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 40,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "max(messenger_queue_oldest_seconds)",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"title": "PostgreSQL · занятые соединения",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 40,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "sum by (service_name) (messenger_db_pool_used)",
|
||||
"legendFormat": "{{service_name}}"
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "short"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 13,
|
||||
"title": "PostgreSQL · запросы к пулу, ожидавшие соединение, в секунду",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 48,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "sum by (service_name) (rate(messenger_db_pool_waited_total[1m]))",
|
||||
"legendFormat": "{{service_name}}"
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "ops"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 14,
|
||||
"title": "Worker · загрузка",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 48,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "sum(messenger_worker_busy) / sum(messenger_worker_capacity)",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "percentunit"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 15,
|
||||
"title": "Вложения · p95 до ready",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 56,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_attachment_ready_seconds_bucket[1m])))",
|
||||
"legendFormat": ""
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "s"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 16,
|
||||
"title": "Процессы · загрузка CPU",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 12,
|
||||
"y": 56,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "sum by (service_name) (rate(messenger_process_cpu_seconds_total[1m]))",
|
||||
"legendFormat": "{{service_name}}"
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "cores"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 17,
|
||||
"title": "Процессы · резидентная память",
|
||||
"type": "timeseries",
|
||||
"datasource": {
|
||||
"type": "prometheus",
|
||||
"uid": "lab6-prometheus"
|
||||
},
|
||||
"gridPos": {
|
||||
"x": 0,
|
||||
"y": 64,
|
||||
"w": 12,
|
||||
"h": 8
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"refId": "A",
|
||||
"expr": "sum by (service_name) (messenger_process_rss_bytes)",
|
||||
"legendFormat": "{{service_name}}"
|
||||
}
|
||||
],
|
||||
"fieldConfig": {
|
||||
"defaults": {
|
||||
"unit": "bytes"
|
||||
},
|
||||
"overrides": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
apiVersion: 1
|
||||
providers:
|
||||
- name: Messenger
|
||||
type: file
|
||||
options:
|
||||
path: /var/lib/grafana/dashboards
|
||||
@@ -0,0 +1,32 @@
|
||||
apiVersion: 1
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
uid: lab6-prometheus
|
||||
type: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
isDefault: true
|
||||
- name: Loki
|
||||
uid: lab6-loki
|
||||
type: loki
|
||||
access: proxy
|
||||
url: http://loki:3100
|
||||
jsonData:
|
||||
derivedFields:
|
||||
- name: Trace
|
||||
matcherType: label
|
||||
matcherRegex: trace_id
|
||||
datasourceUid: lab6-tempo
|
||||
url: '$${__value.raw}'
|
||||
- name: Tempo
|
||||
uid: lab6-tempo
|
||||
type: tempo
|
||||
access: proxy
|
||||
url: http://tempo:3200
|
||||
jsonData:
|
||||
tracesToLogsV2:
|
||||
datasourceUid: lab6-loki
|
||||
spanStartTimeShift: '-5s'
|
||||
spanEndTimeShift: '5s'
|
||||
customQuery: true
|
||||
query: '{service_name=~"messenger-api|messenger-worker"} | trace_id="$${__span.traceId}"'
|
||||
@@ -0,0 +1,31 @@
|
||||
auth_enabled: false
|
||||
server:
|
||||
http_listen_port: 3100
|
||||
common:
|
||||
path_prefix: /loki
|
||||
replication_factor: 1
|
||||
ring:
|
||||
instance_addr: 127.0.0.1
|
||||
kvstore:
|
||||
store: inmemory
|
||||
storage:
|
||||
filesystem:
|
||||
chunks_directory: /loki/chunks
|
||||
rules_directory: /loki/rules
|
||||
schema_config:
|
||||
configs:
|
||||
- from: 2024-01-01
|
||||
store: tsdb
|
||||
object_store: filesystem
|
||||
schema: v13
|
||||
index:
|
||||
prefix: index_
|
||||
period: 24h
|
||||
limits_config:
|
||||
allow_structured_metadata: true
|
||||
otlp_config:
|
||||
resource_attributes:
|
||||
ignore_defaults: true
|
||||
attributes_config:
|
||||
- action: index_label
|
||||
attributes: [service.name, deployment.environment.name]
|
||||
@@ -0,0 +1,8 @@
|
||||
global:
|
||||
scrape_interval: 5s
|
||||
rule_files:
|
||||
- /etc/prometheus/alerts.yaml
|
||||
scrape_configs:
|
||||
- job_name: otel-collector
|
||||
static_configs:
|
||||
- targets: [otel-collector:8889]
|
||||
@@ -0,0 +1,27 @@
|
||||
# Диагностика API и обработки вложений
|
||||
|
||||
SLI доступности API — доля бизнес-запросов без 5xx. Цель — ≥99% за 30 минут при наличии трафика. Проверка:
|
||||
|
||||
```promql
|
||||
1 - (sum(increase(messenger_http_requests_total{http_status_class="5xx"}[30m])) or 0 * sum(increase(messenger_http_requests_total[30m]))) / sum(increase(messenger_http_requests_total[30m]))
|
||||
```
|
||||
|
||||
Health исключён из обоих счётчиков. `MessengerHighErrorRate` срабатывает при доле 5xx выше 5% за минуту в течение 15 секунд. В Explore → Tempo найти ошибочный серверный спан и дочерний `postgresql query`. Сравнить длительность дочернего спана с длительностью HTTP, затем сверить занятые соединения и rate запросов, ожидавших пул.
|
||||
|
||||
`MessengerQueueLag` срабатывает, когда старейшее незавершённое задание старше 20 секунд в течение 15 секунд. В dashboard проверить число незавершённых заданий и наличие worker. Если возраст растёт при отсутствии новых consumer spans, проверить процесс worker. После восстановления дождаться перехода заданий в ready и исчезновения активного alert.
|
||||
|
||||
Глубина очереди измеряется по устойчивым заданиям в PostgreSQL. В неё входят работа в RabbitMQ, leases и ожидание retry. Возраст измеряется от создания задания. Время до ready включает ожидание и повторные попытки. Гистограммы времени используют секунды. Загрузка worker — доля занятых слотов, по одному слоту на процесс. Ожидание пула отображается как частота успешных получений соединения после ожидания. Метрики CPU и RSS относятся к основным процессам Go.
|
||||
|
||||
Правила доступны через API Prometheus `/api/v1/alerts` или запрос `ALERTS{alertstate="firing"}` в Grafana. При недоступности Loki или Tempo приложение продолжает работу. Collector ограничивает память и очередь экспорта, повторяет отправку до 30 секунд. Сигналы сверх лимитов или после истечения retry могут быть потеряны.
|
||||
|
||||
Команды локальной проверки после `make app-up`:
|
||||
|
||||
```sh
|
||||
python3 scripts/check_observability.py pipeline
|
||||
python3 scripts/check_observability_incidents.py worker
|
||||
python3 scripts/check_observability_incidents.py database
|
||||
python3 scripts/check_observability_incidents.py retry
|
||||
python3 scripts/check_observability_incidents.py telemetry
|
||||
```
|
||||
|
||||
Сценарии выполняются последовательно на учебном стенде. DB-инцидент держит временную блокировку таблицы чатов. Проверка retry кратко останавливает S3 и проверяет номера попыток в одной трассе исходного upload. Проверка telemetry останавливает Tempo и одновременно отправляет сообщения и обрабатывает вложение.
|
||||
@@ -0,0 +1,18 @@
|
||||
server:
|
||||
http_listen_port: 3200
|
||||
distributor:
|
||||
receivers:
|
||||
otlp:
|
||||
protocols:
|
||||
http:
|
||||
endpoint: 0.0.0.0:4318
|
||||
storage:
|
||||
trace:
|
||||
backend: local
|
||||
wal:
|
||||
path: /var/tempo/wal
|
||||
local:
|
||||
path: /var/tempo/blocks
|
||||
compactor:
|
||||
compaction:
|
||||
block_retention: 48h
|
||||
@@ -0,0 +1,123 @@
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def read_values(path):
|
||||
values = {}
|
||||
if path.exists():
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value.strip().strip("\"'")
|
||||
return values
|
||||
|
||||
|
||||
def main():
|
||||
path = ROOT / ".env"
|
||||
existing = read_values(path)
|
||||
defaults = read_values(ROOT / ".env.example")
|
||||
defaults.update(
|
||||
POSTGRES_DB="messenger",
|
||||
POSTGRES_USER="messenger",
|
||||
POSTGRES_PASSWORD=secrets.token_hex(24),
|
||||
)
|
||||
defaults.update(API_HTTP_PORT="8080", S3_ENDPOINT="http://s3:9000",
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT="http://otel-collector:4318", UPLOAD_CLEANUP_GRACE_SECONDS="600", UPLOAD_TMP_DIR="/tmp/uploads",
|
||||
S3_PUBLIC_ENDPOINT="http://127.0.0.1:9000",
|
||||
S3_CORS_ORIGINS="http://127.0.0.1:5173,http://localhost:5173")
|
||||
values = defaults | existing
|
||||
for name, lower, upper in (
|
||||
("APP_PORT", 1, 65535),
|
||||
("API_REPLICAS", 2, 32),
|
||||
("SESSION_TTL_SECONDS", 1, 31536000),
|
||||
("IDEMPOTENCY_TTL_SECONDS", 1, 31536000),
|
||||
):
|
||||
try:
|
||||
valid = lower <= int(values[name]) <= upper
|
||||
except (ValueError, KeyError):
|
||||
valid = False
|
||||
if not valid:
|
||||
raise SystemExit(f"{name} must be between {lower} and {upper}")
|
||||
for name in (
|
||||
"POSTGRES_DB",
|
||||
"POSTGRES_USER",
|
||||
"POSTGRES_PASSWORD",
|
||||
"APP_HOST",
|
||||
"APP_ENV",
|
||||
):
|
||||
if not values[name]:
|
||||
raise SystemExit(f"{name} must not be empty")
|
||||
# Existing values, including credentials, are never replaced.
|
||||
missing = {key: value for key, value in values.items() if key not in existing}
|
||||
if missing:
|
||||
prefix = "\n" if path.exists() and path.stat().st_size else ""
|
||||
with path.open("a", encoding="utf-8") as file:
|
||||
file.write(
|
||||
prefix + "".join(f"{key}={value}\n" for key, value in missing.items())
|
||||
)
|
||||
os.chmod(path, 0o600)
|
||||
secret_dir = ROOT / "secrets"
|
||||
secret_dir.mkdir(mode=0o700, exist_ok=True)
|
||||
database_url = "postgres://{}:{}@db:5432/{}?sslmode=disable".format(
|
||||
quote(values["POSTGRES_USER"], safe=""),
|
||||
quote(values["POSTGRES_PASSWORD"], safe=""),
|
||||
quote(values["POSTGRES_DB"], safe=""),
|
||||
)
|
||||
for filename,content in (("broker_user","messenger"),("broker_password",secrets.token_hex(32))):
|
||||
target=secret_dir/filename
|
||||
if not target.exists():
|
||||
target.write_text(content+"\n",encoding="utf-8")
|
||||
os.chmod(target,0o644)
|
||||
broker_url="amqp://{}:{}@broker:5672/%2F".format(
|
||||
quote((secret_dir/"broker_user").read_text().strip(),safe=""),
|
||||
quote((secret_dir/"broker_password").read_text().strip(),safe=""))
|
||||
# Migrate only the generated legacy Redis URL; preserve custom AMQP settings.
|
||||
broker_file=secret_dir/"broker_url"
|
||||
if broker_file.exists() and broker_file.read_text().strip()=="redis://broker:6379/0":
|
||||
broker_file.write_text(broker_url+"\n",encoding="utf-8")
|
||||
for name, content in (
|
||||
("DATABASE_URL_FILE", database_url),
|
||||
("CACHE_URL_FILE", "redis://redis:6379/0"),
|
||||
("BROKER_URL_FILE", broker_url),
|
||||
("S3_ACCESS_KEY_ID_FILE", secrets.token_hex(12)),
|
||||
("S3_SECRET_ACCESS_KEY_FILE", secrets.token_hex(32)),
|
||||
):
|
||||
container_path = Path(values[name])
|
||||
if container_path.parent != Path("/run/secrets") or not container_path.name:
|
||||
raise SystemExit(f"{name} must name a file directly inside /run/secrets")
|
||||
target = (
|
||||
secret_dir
|
||||
/ {
|
||||
"DATABASE_URL_FILE": "database_url",
|
||||
"CACHE_URL_FILE": "cache_url",
|
||||
"BROKER_URL_FILE": "broker_url",
|
||||
"S3_ACCESS_KEY_ID_FILE": "s3_access_key_id",
|
||||
"S3_SECRET_ACCESS_KEY_FILE": "s3_secret_access_key",
|
||||
}[name]
|
||||
)
|
||||
if not target.exists():
|
||||
# Containers run as an unprivileged user; the private host directory
|
||||
# protects the files, while read permission lets the API consume them.
|
||||
with target.open("x", encoding="utf-8") as file:
|
||||
file.write(content + "\n")
|
||||
os.chmod(target, 0o644)
|
||||
# Existing S3 files remain the root credentials; API gets a new, separate identity.
|
||||
for filename, content in (
|
||||
("s3_api_access_key_id", secrets.token_hex(12)),
|
||||
("s3_api_secret_access_key", secrets.token_hex(32)),
|
||||
):
|
||||
target = secret_dir / filename
|
||||
if not target.exists():
|
||||
with target.open("x", encoding="utf-8") as file:
|
||||
file.write(content + "\n")
|
||||
os.chmod(target, 0o644)
|
||||
print("Local .env and connection files ready; existing values and files preserved.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --project-name highload-lab7 --env-file .env -f compose.yml restart db redis s3 broker >/dev/null
|
||||
docker compose --project-name highload-lab7 --env-file .env -f compose.yml up -d --wait db redis s3 broker >/dev/null
|
||||
docker compose --project-name highload-lab7 --env-file .env -f compose.yml restart api upload-cleanup worker dispatcher >/dev/null
|
||||
docker compose --project-name highload-lab7 --env-file .env -f compose.yml up -d --wait >/dev/null
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID to the X-Instance-Id of an API replica}"
|
||||
for container in $(docker compose --project-name highload-lab7 --env-file .env -f compose.yml ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
# Only containers from this Compose project's api service are eligible.
|
||||
docker stop --timeout 8 "$container" >/dev/null
|
||||
printf 'Stopped API instance %s\n' "$hostname"
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
printf 'No running API replica matches TARGET_INSTANCE_ID\n' >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,26 @@
|
||||
.git
|
||||
.gitignore
|
||||
.github
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets
|
||||
certs
|
||||
volumes
|
||||
data
|
||||
backups
|
||||
*.key
|
||||
*.pem
|
||||
*.p12
|
||||
*.pfx
|
||||
*.jks
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
README.md
|
||||
REPORT.md
|
||||
COURSE.md
|
||||
CONTRIBUTING.md
|
||||
MAINTAINERS.md
|
||||
@@ -0,0 +1,42 @@
|
||||
FROM golang:1.22.2-bookworm AS builder
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends webp && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY backend ./backend
|
||||
|
||||
RUN CGO_ENABLED=0 \
|
||||
GOOS=linux \
|
||||
go build \
|
||||
-trimpath \
|
||||
-ldflags="-s -w" \
|
||||
-o /out/messenger \
|
||||
./backend
|
||||
|
||||
|
||||
FROM debian:12.15-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends webp && rm -rf /var/lib/apt/lists/* \
|
||||
&& groupadd --system app \
|
||||
&& useradd --system --gid app --no-create-home app
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /out/messenger ./messenger
|
||||
|
||||
ENV APP_HOST=0.0.0.0
|
||||
ENV APP_PORT=8080
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["./messenger", "healthcheck"]
|
||||
|
||||
USER app:app
|
||||
|
||||
CMD ["./messenger"]
|
||||
@@ -0,0 +1,150 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"messenger/src/backend/telemetry"
|
||||
)
|
||||
|
||||
func serverAddress() string {
|
||||
host := os.Getenv("APP_HOST")
|
||||
if host == "" {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
|
||||
port := os.Getenv("APP_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
func RunHTTP() error {
|
||||
if _, err := repositories.UploadCleanupGrace(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
db, err := repositories.OpenDatabase(ctx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return fmt.Errorf("database initialization failed: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
// Opt-in experiment telemetry goes to container logs, never a public port.
|
||||
if os.Getenv("BENCH_POOL_STATS") == "1" {
|
||||
go func() {
|
||||
for range time.Tick(time.Second) {
|
||||
s := db.Stat()
|
||||
data, _ := json.Marshal(map[string]any{
|
||||
"time": time.Now().UTC(), "acquired": s.AcquiredConns(),
|
||||
"total": s.TotalConns(), "max": s.MaxConns(),
|
||||
"gomaxprocs": runtime.GOMAXPROCS(0), "goroutines": runtime.NumGoroutine(),
|
||||
"acquire_count": s.AcquireCount(), "acquire_duration_ns": s.AcquireDuration().Nanoseconds(),
|
||||
"empty_acquire_count": s.EmptyAcquireCount(), "canceled_acquire_count": s.CanceledAcquireCount(),
|
||||
})
|
||||
log.Printf("benchmark_pool %s", data)
|
||||
}
|
||||
}()
|
||||
}
|
||||
redisClient, err := repositories.RedisClientFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer redisClient.Close()
|
||||
sessionStore := repositories.NewSessionStore(redisClient, "messenger:session:")
|
||||
userStore := repositories.NewUserStore(db)
|
||||
chatStore := repositories.NewChatStore(db)
|
||||
idempotencyTTL, err := repositories.IdempotencyTTL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
messageStore := repositories.NewMessageStoreWithTTL(db, idempotencyTTL)
|
||||
ttl, err := services.SessionTTL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userService := services.NewUserService(userStore)
|
||||
sessionService := services.NewSessionService(userService, sessionStore, ttl)
|
||||
chatService := services.NewChatService(chatStore)
|
||||
messageService := services.NewMessageService(messageStore)
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attachmentService := services.NewAttachmentService(repositories.NewAttachmentStore(db), objects)
|
||||
mux := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(db), sessionStore, objects), userService, sessionService, chatService, messageService, attachmentService)
|
||||
|
||||
signals, stopTelemetry, err := telemetry.Start("messenger-api")
|
||||
if err != nil {
|
||||
return fmt.Errorf("telemetry initialization failed: %w", err)
|
||||
}
|
||||
defer stopTelemetry()
|
||||
if err := signals.ObservePool(db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server := &http.Server{
|
||||
Addr: serverAddress(),
|
||||
Handler: signals.HTTP(mux),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
shutdownContext, stop := signal.NotifyContext(
|
||||
context.Background(),
|
||||
os.Interrupt,
|
||||
syscall.SIGTERM,
|
||||
)
|
||||
defer stop()
|
||||
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
log.Printf("server is listening on %s", server.Addr)
|
||||
|
||||
if err := server.ListenAndServe(); err != nil &&
|
||||
!errors.Is(err, http.ErrServerClosed) {
|
||||
serverErrors <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
log.Printf("server error: %v", err)
|
||||
|
||||
case <-shutdownContext.Done():
|
||||
log.Printf("shutdown signal received")
|
||||
handlers.BeginShutdown()
|
||||
|
||||
shutdownDeadline, cancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
10*time.Second,
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(shutdownDeadline); err != nil {
|
||||
log.Printf("graceful shutdown failed: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Printf("server stopped gracefully")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func runUploadCleanup(args []string) error {
|
||||
flags := flag.NewFlagSet("attachments-cleanup", flag.ContinueOnError)
|
||||
loop := flags.Bool("loop", false, "run every 60 seconds")
|
||||
dryRun := flags.Bool("dry-run", false, "report without deleting anything")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
init, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
db, err := repositories.OpenDatabase(init)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := repositories.NewAttachmentStore(db)
|
||||
for {
|
||||
pass, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
result, err := store.CleanupUploads(pass, *dryRun, objects.Delete)
|
||||
direct, directErr := store.CleanupDirectUploads(pass, *dryRun, objects.Delete)
|
||||
jobs, jobsErr := store.CleanupJobs(pass, 24*time.Hour, *dryRun, objects.Delete)
|
||||
cancel()
|
||||
if encodeErr := json.NewEncoder(os.Stdout).Encode(map[string]any{"originals": result, "direct": direct, "job_staging": jobs}); encodeErr != nil {
|
||||
return encodeErr
|
||||
}
|
||||
if !*loop {
|
||||
if err == nil {
|
||||
err = directErr
|
||||
if err == nil {
|
||||
err = jobsErr
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err != nil || directErr != nil || jobsErr != nil {
|
||||
log.Print("upload cleanup pass failed; journal retained for retry")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-time.After(60 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func runJobAdmin(args []string) error {
|
||||
if len(args) < 1 {
|
||||
return errors.New("usage: jobs list-failed | replay JOB_ID REASON | cleanup-staging JOB_ID | upgrade ATTACHMENT_ID 2 REASON")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
db, err := repositories.OpenDatabase(ctx)
|
||||
if err != nil {
|
||||
return errors.New("database unavailable")
|
||||
}
|
||||
defer db.Close()
|
||||
store := repositories.NewAttachmentStore(db)
|
||||
if args[0] == "list-failed" && len(args) == 1 {
|
||||
jobs, e := store.FailedJobs(ctx)
|
||||
if e != nil {
|
||||
return errors.New("cannot read failed registry")
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(jobs)
|
||||
}
|
||||
if len(args) < 2 {
|
||||
return errors.New("job id required")
|
||||
}
|
||||
if _, err = uuid.Parse(args[1]); err != nil {
|
||||
return errors.New("invalid job id")
|
||||
}
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var changed bool
|
||||
switch {
|
||||
case args[0] == "upgrade" && len(args) == 4:
|
||||
version, e := strconv.Atoi(args[2])
|
||||
if e != nil {
|
||||
return errors.New("invalid pipeline version")
|
||||
}
|
||||
changed, err = store.UpgradePipeline(ctx, args[1], version, args[3], func(ctx context.Context, key string) error {
|
||||
r, e := objects.Get(ctx, key)
|
||||
if e == nil {
|
||||
r.Close()
|
||||
}
|
||||
return e
|
||||
})
|
||||
case args[0] == "replay" && len(args) == 3:
|
||||
changed, err = store.ReplayJob(ctx, args[1], args[2], func(ctx context.Context, key string) error {
|
||||
r, e := objects.Get(ctx, key)
|
||||
if e == nil {
|
||||
r.Close()
|
||||
}
|
||||
return e
|
||||
})
|
||||
case args[0] == "cleanup-staging" && len(args) == 2:
|
||||
changed, err = store.CleanupJobStaging(ctx, args[1], 24*time.Hour, false, objects.Delete)
|
||||
default:
|
||||
return errors.New("usage: jobs list-failed | replay JOB_ID REASON | cleanup-staging JOB_ID | upgrade ATTACHMENT_ID 2 REASON")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]bool{"changed": changed})
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/app"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/workers"
|
||||
)
|
||||
|
||||
// Run dispatches the existing executable modes; the binary and CLI stay the same.
|
||||
func Run(args []string) error {
|
||||
if len(args) == 0 {
|
||||
if environment := os.Getenv("APP_ENV"); environment != "" {
|
||||
log.SetPrefix("[" + environment + "] ")
|
||||
}
|
||||
return app.RunHTTP()
|
||||
}
|
||||
switch args[0] {
|
||||
case "image-process":
|
||||
return workers.RunImageChild(args[1:])
|
||||
case "jobs":
|
||||
return runJobAdmin(args[1:])
|
||||
case "worker", "dispatcher", "broker-publish":
|
||||
return workers.RunPipeline(args[0], args[1:])
|
||||
}
|
||||
if environment := os.Getenv("APP_ENV"); environment != "" {
|
||||
log.SetPrefix("[" + environment + "] ")
|
||||
}
|
||||
switch {
|
||||
case args[0] == "s3-bootstrap" && len(args) == 1:
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err := repositories.BootstrapS3(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Print("private S3 bucket ready")
|
||||
return nil
|
||||
case args[0] == "attachments-cleanup":
|
||||
return runUploadCleanup(args[1:])
|
||||
case args[0] == "healthcheck" && len(args) == 1:
|
||||
return healthcheck()
|
||||
case args[0] == "migrate" && len(args) == 2 && (args[1] == "up" || args[1] == "down"):
|
||||
url, err := repositories.DatabaseURL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = repositories.RunMigrations(url, args[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("migration %s complete", args[1])
|
||||
return nil
|
||||
default:
|
||||
return errors.New("usage: messenger migrate up|down")
|
||||
}
|
||||
}
|
||||
|
||||
func healthcheck() error {
|
||||
client := http.Client{Timeout: 2 * time.Second}
|
||||
port := os.Getenv("APP_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
response, err := client.Get("http://" + net.JoinHostPort("127.0.0.1", port) + "/health/ready")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return errors.New("API is not ready")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"io"
|
||||
"log"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func uploadAttachmentHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
raw := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/attachments")
|
||||
chatID, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), userID, chatID.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
query, err := urlQuery(r)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
filenames := query["filename"]
|
||||
if len(filenames) != 1 {
|
||||
writeError(w, 400, "invalid_request", "exactly one filename is required", requestID)
|
||||
return
|
||||
}
|
||||
if err = services.ValidateFilename(filenames[0]); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if r.ContentLength > services.MaxUploadBytes {
|
||||
writeServiceError(w, services.ErrPayloadTooLarge, requestID)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, services.MaxUploadBytes)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
attachment, err := attachments.Upload(ctx, userID, chatID.String(), filenames[0], r.Header.Get("Content-Type"), r.Body)
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
err = services.ErrPayloadTooLarge
|
||||
}
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 202, attachment)
|
||||
}
|
||||
}
|
||||
|
||||
func attachmentHandler(attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/v1/attachments/")
|
||||
parts := strings.Split(path, "/")
|
||||
if len(parts) > 2 || (len(parts) == 2 && parts[1] != "content") {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
id, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid attachment id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.Get(r.Context(), id.String(), userID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
writeJSON(w, 200, a)
|
||||
return
|
||||
}
|
||||
query, err := urlQuery(r)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if a.Status != "ready" {
|
||||
writeServiceError(w, services.ErrConflict, requestID)
|
||||
return
|
||||
}
|
||||
if variants, exists := query["variant"]; exists {
|
||||
if len(variants) != 1 {
|
||||
writeError(w, 400, "invalid_request", "invalid variant", requestID)
|
||||
return
|
||||
}
|
||||
if variants[0] == "thumbnail" || variants[0] == "crop" || variants[0] == "optimized" {
|
||||
found := false
|
||||
for _, raw := range a.Variants {
|
||||
if v, ok := raw.(map[string]any); ok && v["name"] == variants[0] {
|
||||
a.ObjectKey = repositories.VariantObjectKey(a.ID, a.PipelineVersion, variants[0])
|
||||
a.ContentType = v["content_type"].(string)
|
||||
a.SizeBytes = int64(v["size_bytes"].(float64))
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
} else if variants[0] != "original" {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
content, err := attachments.Content(ctx, a)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
defer content.Close()
|
||||
w.Header().Set("Content-Type", a.ContentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(a.SizeBytes, 10))
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.WriteHeader(200)
|
||||
if _, err = io.Copy(w, content); err != nil {
|
||||
log.Printf("attachment download interrupted, request %s", requestID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func urlQuery(r *http.Request) (url.Values, error) {
|
||||
q, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
return nil, services.InvalidInput{Message: "invalid query"}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type createChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
func createChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createChatRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Create(r.Context(), userID, request.Title, request.MemberIDs)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func getChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/")
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func listChatsHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := chats.List(r.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
|
||||
func chatCollectionHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
create := createChatHandler(chats)
|
||||
list := listChatsHandler(chats)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
list(w, r)
|
||||
} else {
|
||||
create(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func chatSubresourceHandler(chats *services.ChatService, messages *services.MessageService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
get := getChatHandler(chats)
|
||||
createMessage := createMessageHandler(chats, messages)
|
||||
listMessages := listMessagesHandler(chats, messages)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if attachments != nil && strings.Contains(r.URL.Path, "/attachment-uploads") {
|
||||
directUploadHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if attachments != nil && strings.HasSuffix(r.URL.Path, "/attachments") {
|
||||
uploadAttachmentHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
if r.Method == http.MethodGet {
|
||||
listMessages(w, r)
|
||||
} else {
|
||||
createMessage(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
get(w, r)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func directUploadHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
user, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/")
|
||||
if (len(parts) != 2 && (len(parts) != 4 || parts[3] != "complete")) || parts[1] != "attachment-uploads" {
|
||||
writeError(w, 404, "not_found", "upload endpoint not found", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), user, chat.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
if len(parts) == 4 {
|
||||
id, err := uuid.Parse(parts[2])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.CompleteDirect(ctx, user, chat.String(), id.String())
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 202, a)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 4096)
|
||||
var input services.DirectUploadInput
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err = decoder.Decode(&input)
|
||||
if err == nil {
|
||||
var extra any
|
||||
if next := decoder.Decode(&extra); !errors.Is(next, io.EOF) {
|
||||
err = errors.New("expected one JSON object")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload JSON (maximum 4 KiB)", requestID)
|
||||
return
|
||||
}
|
||||
result, err := attachments.InitDirect(ctx, user, chat.String(), input)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type healthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
type apiError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
type errorResponse struct {
|
||||
Error apiError `json:"error"`
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "request-id-generation-failed"
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, code, message, requestID string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
response := errorResponse{Error: apiError{Code: code, Message: message, RequestID: requestID}}
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
log.Printf("failed to encode error response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeServiceError(w http.ResponseWriter, err error, requestID string) {
|
||||
var upload repositories.UploadError
|
||||
if errors.As(err, &upload) {
|
||||
writeError(w, upload.Status, upload.Code, upload.Message, requestID)
|
||||
return
|
||||
}
|
||||
var invalid services.InvalidInput
|
||||
switch {
|
||||
case errors.As(err, &invalid):
|
||||
writeError(w, 400, "invalid_request", invalid.Message, requestID)
|
||||
case errors.Is(err, services.ErrPayloadTooLarge):
|
||||
writeError(w, 413, "payload_too_large", "image exceeds upload limits", requestID)
|
||||
case errors.Is(err, services.ErrUnsupportedMedia):
|
||||
writeError(w, 415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required", requestID)
|
||||
case errors.Is(err, services.ErrAttachmentNotFound):
|
||||
writeError(w, 404, "not_found", "attachment not found", requestID)
|
||||
case errors.Is(err, services.ErrConflict):
|
||||
writeError(w, 409, "conflict", "request conflicts with an existing resource", requestID)
|
||||
case errors.Is(err, services.ErrUnknownUser):
|
||||
writeError(w, 401, "unauthorized", "unknown user", requestID)
|
||||
case errors.Is(err, services.ErrChatNotFound):
|
||||
writeError(w, 404, "not_found", "chat not found", requestID)
|
||||
default:
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(value); err != nil {
|
||||
log.Printf("failed to encode response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func requestUserID(w http.ResponseWriter, r *http.Request, requestID string) (string, bool) {
|
||||
id := currentUserID(r)
|
||||
if id == "" {
|
||||
writeError(w, 401, "unauthorized", "valid Bearer session is required", requestID)
|
||||
return "", false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func pageParams(w http.ResponseWriter, r *http.Request, requestID string) (int, string, bool) {
|
||||
limit := 50
|
||||
if values, exists := r.URL.Query()["limit"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
writeError(w, 400, "invalid_request", "invalid limit", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
parsed, err := strconv.Atoi(values[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "limit must be between 1 and 100", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
var cursor string
|
||||
if values, exists := r.URL.Query()["cursor"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
writeError(w, 400, "invalid_request", "invalid cursor", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
cursor = values[0]
|
||||
}
|
||||
return limit, cursor, true
|
||||
}
|
||||
|
||||
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, healthResponse{"ok"})
|
||||
}
|
||||
|
||||
func readyHandler(health *services.HealthService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
// Readiness must finish before HAProxy's one-second check read timeout.
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := health.Ready(ctx); err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, healthResponse{"ok"})
|
||||
}
|
||||
}
|
||||
|
||||
func NewRouter(health *services.HealthService, users *services.UserService, sessions *services.SessionService, chats *services.ChatService, messages *services.MessageService, attachmentServices ...*services.AttachmentService) http.Handler {
|
||||
var attachments *services.AttachmentService
|
||||
if len(attachmentServices) > 0 {
|
||||
attachments = attachmentServices[0]
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/health/live", healthHandler)
|
||||
mux.HandleFunc("/health/ready", readyHandler(health))
|
||||
mux.Handle("/api/v1/users", createUserHandler(users))
|
||||
mux.Handle("/api/v1/auth/sessions", sessionHandler(sessions))
|
||||
mux.Handle("/api/v1/auth/sessions/current", authRequired(sessions, logoutHandler(sessions)))
|
||||
mux.Handle("/api/v1/users/me", authRequired(sessions, currentUserHandler(users)))
|
||||
mux.Handle("/api/v1/chats", authRequired(sessions, chatCollectionHandler(chats)))
|
||||
mux.Handle("/api/v1/chats/", authRequired(sessions, chatSubresourceHandler(chats, messages, attachments)))
|
||||
if attachments != nil {
|
||||
mux.Handle("/api/v1/attachments/", authRequired(sessions, attachmentHandler(attachments)))
|
||||
}
|
||||
return withInstance(mux)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Docker's hostname lets the operator map an API response to its container.
|
||||
var instanceID = func() string {
|
||||
id, err := os.Hostname()
|
||||
if err != nil || id == "" {
|
||||
return uuid.NewString()
|
||||
}
|
||||
return id
|
||||
}()
|
||||
|
||||
var draining atomic.Bool
|
||||
|
||||
func BeginShutdown() { draining.Store(true) }
|
||||
|
||||
func withInstance(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Instance-Id", instanceID)
|
||||
w.Header().Set("X-Request-Id", newRequestID())
|
||||
if draining.Load() && r.URL.Path != "/health/live" {
|
||||
writeError(w, 503, "unavailable", "instance is shutting down", w.Header().Get("X-Request-Id"))
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
func messageChatID(path string) (string, bool) {
|
||||
const prefix = "/api/v1/chats/"
|
||||
const suffix = "/messages"
|
||||
if !strings.HasPrefix(path, prefix) || !strings.HasSuffix(path, suffix) {
|
||||
return "", false
|
||||
}
|
||||
chatID := strings.TrimSuffix(strings.TrimPrefix(path, prefix), suffix)
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
return "", false
|
||||
}
|
||||
return chatID, true
|
||||
}
|
||||
|
||||
func createMessageHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID, ok := messageChatID(r.URL.Path)
|
||||
if !ok {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createMessageRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
ids := []string{}
|
||||
if request.Text == nil {
|
||||
writeError(w, 400, "invalid_request", "text must be a string", requestID)
|
||||
return
|
||||
}
|
||||
if len(request.AttachmentIDs) > 0 {
|
||||
if err := json.Unmarshal(request.AttachmentIDs, &ids); err != nil || ids == nil {
|
||||
writeError(w, 400, "invalid_request", "attachment_ids must be an array of UUIDs", requestID)
|
||||
return
|
||||
}
|
||||
}
|
||||
keys := r.Header.Values("Idempotency-Key")
|
||||
if len(keys) != 1 {
|
||||
writeError(w, 400, "invalid_request", "exactly one Idempotency-Key is required", requestID)
|
||||
return
|
||||
}
|
||||
message, err := messages.CreateIdempotent(r.Context(), userID, chat, *request.Text, keys[0], ids)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, message)
|
||||
}
|
||||
}
|
||||
|
||||
func listMessagesHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID, ok := messageChatID(r.URL.Path)
|
||||
if !ok {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := messages.List(r.Context(), userID, chat, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type authenticatedUserKey struct{}
|
||||
|
||||
func currentUserID(r *http.Request) string {
|
||||
id, _ := r.Context().Value(authenticatedUserKey{}).(string)
|
||||
return id
|
||||
}
|
||||
|
||||
func authRequired(sessions *services.SessionService, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authorization := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(authorization, "Bearer ") || strings.TrimSpace(strings.TrimPrefix(authorization, "Bearer ")) == "" {
|
||||
unauthorized(w, "Bearer", newRequestID())
|
||||
return
|
||||
}
|
||||
user, err := sessions.User(r.Context(), strings.TrimPrefix(authorization, "Bearer "))
|
||||
if errors.Is(err, services.ErrInvalidSession) {
|
||||
unauthorized(w, "Bearer", newRequestID())
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), authenticatedUserKey{}, user.ID)))
|
||||
})
|
||||
}
|
||||
|
||||
func unauthorized(w http.ResponseWriter, scheme, requestID string) {
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if scheme == "Basic" {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
} else {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
}
|
||||
writeError(w, 401, "unauthorized", "invalid credentials", requestID)
|
||||
}
|
||||
|
||||
func sessionHandler(sessions *services.SessionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || r.ContentLength > 0 {
|
||||
unauthorized(w, "Basic", requestID)
|
||||
return
|
||||
}
|
||||
user, token, expires, valid, err := sessions.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
if !valid {
|
||||
unauthorized(w, "Basic", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User any `json:"user"`
|
||||
}{token, "Bearer", expires, user})
|
||||
}
|
||||
}
|
||||
|
||||
func logoutHandler(sessions *services.SessionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodDelete {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
||||
if err := sessions.Revoke(r.Context(), token); err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createUserRequest struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func decodeJSONBody(r *http.Request, destination any) error {
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var body json.RawMessage
|
||||
if err := decoder.Decode(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
var extra any
|
||||
if err := decoder.Decode(&extra); err != io.EOF {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("more than one JSON value")
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(body, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
if fields == nil {
|
||||
return fmt.Errorf("JSON body must be an object")
|
||||
}
|
||||
// encoding/json matches struct fields case-insensitively, even with
|
||||
// DisallowUnknownFields. Check exact contract names before decoding values.
|
||||
typeOfRequest := reflect.TypeOf(destination).Elem()
|
||||
allowed := make(map[string]bool, typeOfRequest.NumField())
|
||||
for i := 0; i < typeOfRequest.NumField(); i++ {
|
||||
name := strings.Split(typeOfRequest.Field(i).Tag.Get("json"), ",")[0]
|
||||
if name != "" && name != "-" {
|
||||
allowed[name] = true
|
||||
}
|
||||
}
|
||||
for name := range fields {
|
||||
if !allowed[name] {
|
||||
return fmt.Errorf("unknown JSON field %q", name)
|
||||
}
|
||||
}
|
||||
return json.Unmarshal(body, destination)
|
||||
}
|
||||
|
||||
func createUserHandler(users *services.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createUserRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
user, err := users.Create(r.Context(), request.Username, request.DisplayName, request.Password)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, user)
|
||||
}
|
||||
}
|
||||
|
||||
func currentUserHandler(users *services.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
user, err := users.Get(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, user)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package faults
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Only an operator-mounted directory enables faults; clients cannot set them.
|
||||
// Rename makes each injected crash one-shot across automatic container restarts.
|
||||
func Take(point, id string) bool {
|
||||
directory := os.Getenv("PIPELINE_FAULT_DIR")
|
||||
if directory == "" {
|
||||
return false
|
||||
}
|
||||
if _, err := uuid.Parse(id); err != nil {
|
||||
return false
|
||||
}
|
||||
name := filepath.Join(directory, point+"."+id)
|
||||
return os.Rename(name, name+".hit") == nil
|
||||
}
|
||||
func Crash(point, id string) {
|
||||
if Take(point, id) {
|
||||
log.Printf("injected pipeline crash: %s, attachment %s", point, id)
|
||||
os.Exit(86)
|
||||
}
|
||||
}
|
||||
|
||||
// Observe records a test-only branch without exposing an API or a lease token.
|
||||
func Observe(point, id string) {
|
||||
directory := os.Getenv("PIPELINE_FAULT_DIR")
|
||||
if directory == "" {
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(id); err != nil {
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile(filepath.Join(directory, point+"."+id+".observed"), []byte("observed\n"), 0644)
|
||||
}
|
||||
|
||||
// Pause is operator-only and ends on release or context expiry.
|
||||
func Pause(ctx context.Context, point, id string) {
|
||||
if !Take(point, id) {
|
||||
return
|
||||
}
|
||||
release := filepath.Join(os.Getenv("PIPELINE_FAULT_DIR"), point+"."+id+".release")
|
||||
ticker := time.NewTicker(50 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if _, err := os.Stat(release); err == nil {
|
||||
_ = os.WriteFile(release+".observed", []byte("released\n"), 0644)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"messenger/src/backend/commands"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := commands.Run(os.Args[1:]); err != nil {
|
||||
if len(os.Args) > 1 && (os.Args[1] == "image-process" || os.Args[1] == "healthcheck") {
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ObjectKey string `json:"-"`
|
||||
PipelineVersion int `json:"-"`
|
||||
}
|
||||
|
||||
type AttachmentStore struct{ db *pgxpool.Pool }
|
||||
|
||||
func NewAttachmentStore(db *pgxpool.Pool) *AttachmentStore { return &AttachmentStore{db: db} }
|
||||
func (s *AttachmentStore) Create(ctx context.Context, a Attachment) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
_, err := s.db.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
func (s *AttachmentStore) GetForMember(ctx context.Context, id, userID string) (Attachment, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
a := Attachment{Variants: []any{}}
|
||||
var variants []byte
|
||||
err := s.db.QueryRow(ctx, `SELECT a.id,a.chat_id,a.owner_id,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.created_at,a.updated_at,a.status,a.variants,a.error_code,a.published_pipeline_version FROM attachments a JOIN chat_members cm ON cm.chat_id=a.chat_id WHERE a.id=$1 AND cm.user_id=$2`, id, userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.CreatedAt, &a.UpdatedAt, &a.Status, &variants, &a.ErrorCode, &a.PipelineVersion)
|
||||
if err == nil {
|
||||
err = json.Unmarshal(variants, &a.Variants)
|
||||
}
|
||||
a.CreatedAt = a.CreatedAt.UTC()
|
||||
a.UpdatedAt = a.UpdatedAt.UTC()
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"errors"
|
||||
"messenger/src/backend/telemetry"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
const jobQueue = "attachments.v1"
|
||||
|
||||
type JobBroker struct {
|
||||
connection *amqp.Connection
|
||||
channel *amqp.Channel
|
||||
returned chan amqp.Return
|
||||
}
|
||||
|
||||
func NewJobBroker(ctx context.Context, address string) (*JobBroker, error) {
|
||||
connection, err := amqp.DialConfig(address, amqp.Config{
|
||||
Heartbeat: 10 * time.Second,
|
||||
Dial: func(network, address string) (net.Conn, error) {
|
||||
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, address)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New("RabbitMQ connection unavailable")
|
||||
}
|
||||
channel, err := connection.Channel()
|
||||
if err != nil {
|
||||
connection.Close()
|
||||
return nil, errors.New("RabbitMQ channel unavailable")
|
||||
}
|
||||
b := &JobBroker{connection: connection, channel: channel, returned: make(chan amqp.Return, 1)}
|
||||
// Queue metadata and message bodies both survive a broker restart.
|
||||
_, err = channel.QueueDeclare(jobQueue, true, false, false, false, nil)
|
||||
if err == nil {
|
||||
err = channel.Confirm(false)
|
||||
}
|
||||
if err == nil {
|
||||
err = channel.Qos(1, 0, false)
|
||||
}
|
||||
if err != nil {
|
||||
b.Close()
|
||||
return nil, errors.New("RabbitMQ queue initialization failed")
|
||||
}
|
||||
channel.NotifyReturn(b.returned)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (b *JobBroker) Close() { b.channel.Close(); b.connection.Close() }
|
||||
|
||||
func (b *JobBroker) Publish(ctx context.Context, body []byte) error {
|
||||
// mandatory catches an unroutable message; confirms alone do not prove routing.
|
||||
headers := amqp.Table{}
|
||||
for key, value := range telemetry.TraceHeaders(ctx) {
|
||||
headers[key] = value
|
||||
}
|
||||
confirmation, err := b.channel.PublishWithDeferredConfirmWithContext(ctx, "", jobQueue, true, false, amqp.Publishing{
|
||||
DeliveryMode: amqp.Persistent, ContentType: "application/json", Body: body, Headers: headers,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.New("RabbitMQ publication failed")
|
||||
}
|
||||
confirmed, err := confirmation.WaitContext(ctx)
|
||||
if err != nil || !confirmed {
|
||||
return errors.New("RabbitMQ publication not confirmed")
|
||||
}
|
||||
// basic.return precedes basic.ack on the channel; NotifyReturn is buffered.
|
||||
select {
|
||||
case <-b.returned:
|
||||
return errors.New("RabbitMQ message was not routed")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *JobBroker) Consume() (<-chan amqp.Delivery, error) {
|
||||
return b.channel.Consume(jobQueue, "", false, false, false, false, nil)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChatStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatStore(db *pgxpool.Pool) *ChatStore {
|
||||
return &ChatStore{db: db}
|
||||
}
|
||||
|
||||
var ErrUnknownMember = errors.New("unknown member")
|
||||
|
||||
func (s *ChatStore) Create(ctx context.Context, title string, memberIDs []string) (Chat, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
membersCopy := append([]string(nil), memberIDs...)
|
||||
chat := Chat{
|
||||
ID: uuid.New().String(),
|
||||
Title: title,
|
||||
MemberIDs: membersCopy,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
for _, id := range membersCopy {
|
||||
var exists bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM users WHERE id=$1)", id).Scan(&exists); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
if !exists {
|
||||
return Chat{}, ErrUnknownMember
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO chats(id,title,created_at) VALUES($1,$2,$3)", chat.ID, title, chat.CreatedAt); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
for _, id := range membersCopy {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)", chat.ID, id); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *ChatStore) GetByID(ctx context.Context, id string) (Chat, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var chat Chat
|
||||
err := s.db.QueryRow(ctx, "SELECT id,title,created_at FROM chats WHERE id=$1", id).Scan(&chat.ID, &chat.Title, &chat.CreatedAt)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
rows, err := s.db.Query(ctx, "SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id", id)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
chat.MemberIDs = []string{}
|
||||
for rows.Next() {
|
||||
var member string
|
||||
if err = rows.Scan(&member); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
chat.MemberIDs = append(chat.MemberIDs, member)
|
||||
}
|
||||
return chat, rows.Err()
|
||||
}
|
||||
|
||||
func (s *ChatStore) ListPageForUser(ctx context.Context, userID string, cursor *ChatCursor, limit int) ([]Chat, bool, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
afterTime := time.Time{}
|
||||
afterID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != nil {
|
||||
afterTime = cursor.CreatedAt
|
||||
afterID = cursor.ChatID
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT c.id,c.title,c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=$1 AND (c.created_at,c.id)>($2,$3::uuid) ORDER BY c.created_at,c.id LIMIT $4`, userID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
chats := []Chat{}
|
||||
for rows.Next() {
|
||||
var c Chat
|
||||
if err = rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
chats = append(chats, c)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
more := len(chats) > limit
|
||||
if more {
|
||||
chats = chats[:limit]
|
||||
}
|
||||
for i := range chats {
|
||||
chat, err := s.GetByID(ctx, chats[i].ID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
chats[i] = chat
|
||||
}
|
||||
return chats, more, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UploadCleanupGrace() (time.Duration, error) {
|
||||
value := os.Getenv("UPLOAD_CLEANUP_GRACE_SECONDS")
|
||||
if value == "" {
|
||||
value = "600"
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil || seconds < 120 || seconds > 86400 {
|
||||
return 0, errors.New("UPLOAD_CLEANUP_GRACE_SECONDS must be between 120 and 86400")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RedisClientFromEnvironment() (*redis.Client, error) {
|
||||
if path := os.Getenv("CACHE_URL_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
options, err := redis.ParseURL(strings.TrimSpace(string(data)))
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid Redis URL in CACHE_URL_FILE")
|
||||
}
|
||||
options.PoolSize = 10
|
||||
options.MaxRetries = -1
|
||||
options.DialTimeout = time.Second
|
||||
options.ReadTimeout = time.Second
|
||||
options.WriteTimeout = time.Second
|
||||
return redis.NewClient(options), nil
|
||||
}
|
||||
address := os.Getenv("REDIS_ADDR")
|
||||
if address == "" {
|
||||
address = "redis:6379"
|
||||
}
|
||||
return NewRedisClient(address), nil
|
||||
}
|
||||
|
||||
func IdempotencyTTL() (time.Duration, error) {
|
||||
value := os.Getenv("IDEMPOTENCY_TTL_SECONDS")
|
||||
if value == "" {
|
||||
value = "86400"
|
||||
}
|
||||
seconds, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || seconds < 1 || seconds > 31536000 {
|
||||
return 0, errors.New("IDEMPOTENCY_TTL_SECONDS must be between 1 and 31536000")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package repositories
|
||||
|
||||
import "time"
|
||||
|
||||
type ChatCursor struct {
|
||||
UserID string `json:"user_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ChatID string `json:"chat_id"`
|
||||
}
|
||||
|
||||
type MessageCursor struct {
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
MessageID string `json:"message_id"`
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/telemetry"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func DatabaseURL() (string, error) {
|
||||
if path := os.Getenv("DATABASE_URL_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(data)), nil
|
||||
}
|
||||
if url := os.Getenv("DATABASE_URL"); url != "" {
|
||||
return url, nil
|
||||
}
|
||||
return "", errors.New("DATABASE_URL_FILE or DATABASE_URL is required")
|
||||
}
|
||||
|
||||
func OpenDatabase(ctx context.Context) (*pgxpool.Pool, error) {
|
||||
url, err := DatabaseURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.ConnConfig.Tracer = telemetry.PostgresTracer{}
|
||||
cfg.MaxConns = 10
|
||||
if value := os.Getenv("DATABASE_MAX_CONNS"); value != "" {
|
||||
maxConns, parseErr := strconv.Atoi(value)
|
||||
if parseErr != nil || maxConns < 1 || maxConns > 100 {
|
||||
return nil, errors.New("DATABASE_MAX_CONNS must be between 1 and 100")
|
||||
}
|
||||
cfg.MaxConns = int32(maxConns)
|
||||
}
|
||||
cfg.ConnConfig.ConnectTimeout = 5 * time.Second
|
||||
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = db.Ping(ctx); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// RunMigrations uses versioned up/down SQL embedded in the API binary.
|
||||
// "down" rolls back exactly one version and is an explicit operator command.
|
||||
func RunMigrations(url, direction string) error {
|
||||
source, err := iofs.New(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sqlDB, err := sql.Open("pgx", url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
driver, err := pgxmigrate.WithInstance(sqlDB, &pgxmigrate.Config{})
|
||||
if err != nil {
|
||||
sqlDB.Close()
|
||||
return err
|
||||
}
|
||||
m, err := migrate.NewWithInstance("iofs", source, "pgx5", driver)
|
||||
if err != nil {
|
||||
sqlDB.Close()
|
||||
return err
|
||||
}
|
||||
defer m.Close()
|
||||
if direction == "down" {
|
||||
err = m.Steps(-1)
|
||||
} else {
|
||||
err = m.Up()
|
||||
}
|
||||
if errors.Is(err, migrate.ErrNoChange) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *AttachmentStore) CleanupDirectUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM direct_uploads WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupDirectUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d direct upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupDirectUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key, state string
|
||||
var barrier, complete, purge, now time.Time
|
||||
err = tx.QueryRow(ctx, `SELECT staging_key,state,complete_before+interval '600 seconds',complete_before,purge_after,clock_timestamp() FROM direct_uploads WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key, &state, &barrier, &complete, &purge, &now)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || key != "staging/direct/"+parsed.String() {
|
||||
return "", errors.New("invalid direct staging key")
|
||||
}
|
||||
if state == "pending" && now.Before(barrier) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if dryRun {
|
||||
return "eligible", nil
|
||||
}
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if now.After(purge) && !now.Before(barrier) {
|
||||
_, err = tx.Exec(ctx, `DELETE FROM direct_uploads WHERE id=$1`, id)
|
||||
} else {
|
||||
if state == "pending" && !now.Before(complete) {
|
||||
state = "expired"
|
||||
}
|
||||
// An early cleanup is not final: a still valid POST can recreate staging.
|
||||
// Keep a second cleanup after the safety barrier, then retain the receipt 24h.
|
||||
next := barrier
|
||||
if !now.Before(barrier) {
|
||||
next = purge
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE direct_uploads SET state=$2,cleanup_after=$3 WHERE id=$1`, id, state, next)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// UploadError is also persisted for terminal validation failures, so retries agree.
|
||||
type UploadError struct {
|
||||
Status int
|
||||
Code, Message string
|
||||
}
|
||||
|
||||
func (e UploadError) Error() string { return e.Message }
|
||||
|
||||
func DirectError(code string) UploadError {
|
||||
switch code {
|
||||
case "upload_not_uploaded":
|
||||
return UploadError{409, code, "file has not been uploaded"}
|
||||
case "upload_expired":
|
||||
return UploadError{409, code, "upload confirmation period has expired"}
|
||||
case "upload_size_mismatch":
|
||||
return UploadError{400, code, "uploaded size differs from declared size"}
|
||||
case "upload_checksum_mismatch":
|
||||
return UploadError{400, code, "uploaded SHA-256 differs from declared checksum"}
|
||||
case "payload_too_large":
|
||||
return UploadError{413, code, "image exceeds upload limits"}
|
||||
default:
|
||||
return UploadError{415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required"}
|
||||
}
|
||||
}
|
||||
|
||||
type DirectUpload struct {
|
||||
ID string `json:"upload_id"`
|
||||
AttachmentID string `json:"-"`
|
||||
OwnerID string `json:"-"`
|
||||
ChatID string `json:"-"`
|
||||
Filename string `json:"-"`
|
||||
ContentType string `json:"-"`
|
||||
SizeBytes int64 `json:"-"`
|
||||
SHA256 string `json:"-"`
|
||||
StagingKey string `json:"-"`
|
||||
FinalKey string `json:"-"`
|
||||
State string `json:"-"`
|
||||
ErrorCode *string `json:"-"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CompleteBefore time.Time `json:"complete_before"`
|
||||
}
|
||||
|
||||
const directColumns = `d.id,d.attachment_id,d.owner_id,d.chat_id,d.filename,d.content_type,d.size_bytes,d.sha256,d.staging_key,d.final_key,d.state,d.error_code,d.expires_at,d.complete_before`
|
||||
|
||||
func scanDirect(row pgx.Row) (DirectUpload, error) {
|
||||
var u DirectUpload
|
||||
err := row.Scan(&u.ID, &u.AttachmentID, &u.OwnerID, &u.ChatID, &u.Filename, &u.ContentType, &u.SizeBytes, &u.SHA256, &u.StagingKey, &u.FinalKey, &u.State, &u.ErrorCode, &u.ExpiresAt, &u.CompleteBefore)
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) CreateDirect(ctx context.Context, u DirectUpload) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `INSERT INTO direct_uploads AS d(id,attachment_id,owner_id,chat_id,filename,content_type,size_bytes,sha256,staging_key,final_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING `+directColumns, u.ID, u.AttachmentID, u.OwnerID, u.ChatID, u.Filename, u.ContentType, u.SizeBytes, u.SHA256, u.StagingKey, u.FinalKey))
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) GetDirect(ctx context.Context, id, chat, user string) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3`, id, chat, user))
|
||||
}
|
||||
|
||||
// The session lock serializes complete across replicas. A separately committed
|
||||
// reservation survives rollback/crash, while its row lock protects the final key.
|
||||
func (s *AttachmentStore) CompleteDirect(ctx context.Context, id, chat, user string, validate func(context.Context, DirectUpload) error) (Attachment, error) {
|
||||
var a Attachment
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
u, err := scanDirect(tx.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3 FOR UPDATE OF d`, id, chat, user))
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
if u.StagingKey != "staging/direct/"+u.ID || u.FinalKey != "originals/"+u.AttachmentID {
|
||||
return a, errors.New("invalid direct upload object keys")
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.GetForMember(ctx, u.AttachmentID, user)
|
||||
}
|
||||
if u.State == "rejected" && u.ErrorCode != nil {
|
||||
return a, DirectError(*u.ErrorCode)
|
||||
}
|
||||
var expired bool
|
||||
if err = tx.QueryRow(ctx, `SELECT clock_timestamp()>=$1`, u.CompleteBefore).Scan(&expired); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if expired || u.State == "expired" {
|
||||
return a, DirectError("upload_expired")
|
||||
}
|
||||
grace, err := UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
// ON CONFLICT handles retry after an interrupted COPY without losing tracking.
|
||||
_, err = s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second') ON CONFLICT(id) DO UPDATE SET cleanup_after=GREATEST(upload_attempts.cleanup_after,EXCLUDED.cleanup_after) WHERE upload_attempts.object_key=EXCLUDED.object_key`, u.AttachmentID, u.FinalKey, grace.Seconds())
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
var key string
|
||||
if err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, u.AttachmentID).Scan(&key); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if key != u.FinalKey {
|
||||
return a, errors.New("direct upload reservation mismatch")
|
||||
}
|
||||
if err = validate(ctx, u); err != nil {
|
||||
var invalid UploadError
|
||||
if errors.As(err, &invalid) && (invalid.Status == 400 || invalid.Status == 413 || invalid.Status == 415) {
|
||||
_, saveErr := tx.Exec(ctx, `UPDATE direct_uploads SET state='rejected',error_code=$2,cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id, invalid.Code)
|
||||
if saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
if saveErr = tx.Commit(ctx); saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a = Attachment{ID: u.AttachmentID, ChatID: u.ChatID, OwnerID: u.OwnerID, Filename: u.Filename, ContentType: u.ContentType, SizeBytes: u.SizeBytes, SHA256: u.SHA256, ObjectKey: u.FinalKey, Status: "queued", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if err = enqueueJob(ctx, tx, a, a.ObjectKey); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE direct_uploads SET state='ready',cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM upload_attempts WHERE id=$1`, a.ID); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func NotFound(err error) bool {
|
||||
return errors.Is(err, pgx.ErrNoRows)
|
||||
}
|
||||
|
||||
func CheckViolation(err error) bool {
|
||||
var pgerr *pgconn.PgError
|
||||
return errors.As(err, &pgerr) && pgerr.Code == "23514"
|
||||
}
|
||||
|
||||
func UniqueViolation(err error) bool {
|
||||
var pgerr *pgconn.PgError
|
||||
return errors.As(err, &pgerr) && pgerr.Code == "23505"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type HealthStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewHealthStore(db *pgxpool.Pool) *HealthStore {
|
||||
return &HealthStore{db: db}
|
||||
}
|
||||
|
||||
func (s *HealthStore) Ping(ctx context.Context) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return s.db.Ping(ctx)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
var ErrAttachmentFailed = errors.New("attachment failed")
|
||||
|
||||
var ErrIdempotencyConflict = errors.New("idempotency key belongs to a different message")
|
||||
|
||||
// CreateIdempotent commits the message and its replay record together.
|
||||
func (s *MessageStore) CreateIdempotent(ctx context.Context, chatID, senderID, text, key string, attachments ...[]string) (Message, error) {
|
||||
ids := []string{}
|
||||
if len(attachments) > 0 && attachments[0] != nil {
|
||||
ids = attachments[0]
|
||||
}
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
|
||||
// The transaction-scoped lock is shared by all API replicas. Hash collisions
|
||||
// only serialize unrelated requests; the full scope is checked by the query.
|
||||
scope := senderID + ":POST:/api/v1/chats/" + chatID + "/messages:" + key
|
||||
if _, err = tx.Exec(ctx, "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", scope); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
// A shared row lock prevents failure publication racing with a new link.
|
||||
hasFailed := false
|
||||
for _, id := range ids {
|
||||
var status string
|
||||
err = tx.QueryRow(ctx, "SELECT status FROM attachments WHERE id=$1 AND chat_id=$2 FOR SHARE", id, chatID).Scan(&status)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
hasFailed = hasFailed || status == "failed"
|
||||
}
|
||||
|
||||
var message Message
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, ARRAY(SELECT attachment_id::text FROM message_attachments ma WHERE ma.message_id=m.id ORDER BY position)
|
||||
FROM message_idempotency i JOIN messages m ON m.id = i.message_id
|
||||
WHERE i.sender_id=$1 AND i.chat_id=$2 AND i.key=$3
|
||||
AND i.expires_at > clock_timestamp()`, senderID, chatID, key).
|
||||
Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs)
|
||||
if err == nil {
|
||||
message.CreatedAt = message.CreatedAt.UTC()
|
||||
if message.Text != text || !reflect.DeepEqual(message.AttachmentIDs, ids) {
|
||||
return Message{}, ErrIdempotencyConflict
|
||||
}
|
||||
return message, tx.Commit(ctx)
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
if hasFailed {
|
||||
return Message{}, ErrAttachmentFailed
|
||||
}
|
||||
|
||||
// Expiry permits key reuse; it never deletes the original message.
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM message_idempotency WHERE sender_id=$1 AND chat_id=$2 AND key=$3", senderID, chatID, key); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message = Message{
|
||||
AttachmentIDs: ids, ID: uuid.NewString(), ChatID: chatID, SenderID: senderID, Text: text,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", message.ID, chatID, senderID, text, message.CreatedAt); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
for position, id := range ids {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO message_attachments(message_id,attachment_id,position) VALUES($1,$2,$3)", message.ID, id, position); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO message_idempotency(sender_id,chat_id,key,message_id,expires_at)
|
||||
VALUES($1,$2,$3,$4,clock_timestamp() + $5 * interval '1 second')`, senderID, chatID, key, message.ID, s.idempotencyTTL.Seconds()); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return message, tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type FailedJob struct {
|
||||
ID string `json:"job_id"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
Code string `json:"error_code"`
|
||||
Attempts int `json:"attempts"`
|
||||
FailedAt time.Time `json:"failed_at"`
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) FailedJobs(ctx context.Context) ([]FailedJob, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT j.id,j.attachment_id,f.error_code,f.attempts,f.failed_at FROM attachment_job_failures f JOIN attachment_jobs j ON j.id=f.job_id JOIN attachments a ON a.id=j.attachment_id WHERE a.status='failed' AND j.pipeline_version=a.pipeline_version AND j.superseded_at IS NULL ORDER BY f.failed_at DESC LIMIT 100`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
result := []FailedJob{}
|
||||
for rows.Next() {
|
||||
var j FailedJob
|
||||
if err = rows.Scan(&j.ID, &j.AttachmentID, &j.Code, &j.Attempts, &j.FailedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, j)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) ReplayJob(ctx context.Context, id, reason string, exists func(context.Context, string) error) (bool, error) {
|
||||
reason = strings.TrimSpace(reason)
|
||||
if reason == "" || utf8.RuneCountInString(reason) > 200 {
|
||||
return false, errors.New("replay reason must contain 1-200 characters")
|
||||
}
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var status, key, attachment string
|
||||
var deleted, obsolete bool
|
||||
err = tx.QueryRow(ctx, `SELECT a.status,j.staging_key,a.id,j.staging_deleted_at IS NOT NULL,j.pipeline_version<>a.pipeline_version OR j.superseded_at IS NOT NULL FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1 FOR UPDATE OF j,a`, id).Scan(&status, &key, &attachment, &deleted, &obsolete)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if obsolete || status != "failed" {
|
||||
return false, nil
|
||||
}
|
||||
if deleted {
|
||||
return false, errors.New("staging has expired; replay unavailable")
|
||||
}
|
||||
if key != "staging/jobs/"+attachment && key != "originals/"+attachment {
|
||||
return false, errors.New("invalid staging key")
|
||||
}
|
||||
if err = exists(ctx, key); err != nil {
|
||||
return false, errors.New("staging unavailable; replay not scheduled")
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_job_replays(job_id,reason) VALUES($1,$2)`, id, reason); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET attempts=0,generation=generation+1,next_attempt_at=now(),lease_until=NULL,lease_token=NULL,published_at=NULL WHERE id=$1`, id); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET status='queued',error_code=NULL,updated_at=now() WHERE id=$1`, attachment); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// Replay and cleanup lock the same rows. A ready source is removed only after
|
||||
// final publication, failed sources expire after retention; queued work is safe.
|
||||
func (s *AttachmentStore) CleanupJobStaging(ctx context.Context, id string, retention time.Duration, dryRun bool, remove func(context.Context, string) error) (bool, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key, attachment, status string
|
||||
var eligible bool
|
||||
err = tx.QueryRow(ctx, `SELECT j.staging_key,a.id,a.status,j.staging_deleted_at IS NULL AND (a.status='ready' AND jsonb_array_length(a.variants)>=3 OR a.status='failed' AND j.pipeline_version=a.pipeline_version AND COALESCE(f.failed_at,a.updated_at)<now()-$2*interval '1 second') FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id LEFT JOIN attachment_job_failures f ON f.job_id=j.id WHERE j.id=$1 FOR UPDATE OF j,a SKIP LOCKED`, id, retention.Seconds()).Scan(&key, &attachment, &status, &eligible)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !eligible || key == "originals/"+attachment {
|
||||
return false, nil
|
||||
}
|
||||
if key != "staging/jobs/"+attachment {
|
||||
return false, errors.New("invalid staging key")
|
||||
}
|
||||
if dryRun {
|
||||
return true, nil
|
||||
}
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET staging_deleted_at=now() WHERE id=$1`, id); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) CleanupJobs(ctx context.Context, retention time.Duration, dryRun bool, remove func(context.Context, string) error) (int, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT j.id FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id LEFT JOIN attachment_job_failures f ON f.job_id=j.id WHERE j.staging_deleted_at IS NULL AND j.staging_key='staging/jobs/'||j.attachment_id::text AND (a.status='ready' AND jsonb_array_length(a.variants)>=3 OR a.status='failed' AND j.pipeline_version=a.pipeline_version AND COALESCE(f.failed_at,a.updated_at)<now()-$1*interval '1 second') ORDER BY j.created_at LIMIT 100`, retention.Seconds())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
count := 0
|
||||
for _, id := range ids {
|
||||
done, e := s.CleanupJobStaging(ctx, id, retention, dryRun, remove)
|
||||
if e != nil {
|
||||
return count, e
|
||||
}
|
||||
if done {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/telemetry"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type Variant struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
type Job struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
ID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Traceparent string `json:"-"`
|
||||
Tracestate string `json:"-"`
|
||||
}
|
||||
|
||||
// QueuedJobs is also the recovery source when publish/confirm is uncertain.
|
||||
// Re-publication is intentional: a durable intent is never removed by publish.
|
||||
func (s *AttachmentStore) QueuedJobs(ctx context.Context) ([]Job, error) {
|
||||
rows, err := s.db.Query(ctx, `SELECT j.id,j.attachment_id,j.pipeline_version,j.attempts+1,j.created_at,j.traceparent,j.tracestate FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.superseded_at IS NULL AND j.pipeline_version=a.pipeline_version AND a.status IN ('queued','processing') AND j.next_attempt_at<=now() AND (j.lease_until IS NULL OR j.lease_until<now()) AND (j.published_at IS NULL OR j.published_at<now()-interval '30 seconds') ORDER BY j.created_at LIMIT 100`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
jobs := []Job{}
|
||||
for rows.Next() {
|
||||
j := Job{SchemaVersion: 1, Type: "attachment.process.v1"}
|
||||
if err = rows.Scan(&j.ID, &j.AttachmentID, &j.PipelineVersion, &j.Attempt, &j.CreatedAt, &j.Traceparent, &j.Tracestate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
j.CreatedAt = j.CreatedAt.UTC()
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
return jobs, rows.Err()
|
||||
}
|
||||
func (s *AttachmentStore) MarkPublished(ctx context.Context, id string) error {
|
||||
_, err := s.db.Exec(ctx, `UPDATE attachment_jobs SET published_at=now() WHERE id=$1`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// StartAttempt commits the lease and attempt before decoding. A token fences a
|
||||
// consumer that was paused between claim and processing past its lease deadline.
|
||||
func (s *AttachmentStore) StartAttempt(ctx context.Context, j Job, timeout time.Duration, maxAttempts int) (string, error) {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var status string
|
||||
var attempts, generation int
|
||||
var available bool
|
||||
err = tx.QueryRow(ctx, `SELECT a.status,j.attempts,j.generation,j.next_attempt_at<=now() AND (j.lease_until IS NULL OR j.lease_until<now()) FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1 AND j.attachment_id=$2 AND j.pipeline_version=$3 AND j.superseded_at IS NULL AND a.pipeline_version=j.pipeline_version FOR UPDATE OF j,a SKIP LOCKED`, j.ID, j.AttachmentID, j.PipelineVersion).Scan(&status, &attempts, &generation, &available)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if status == "ready" || status == "failed" || !available {
|
||||
return "", nil
|
||||
}
|
||||
// A crashed attempt remains visible in the audit trail.
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code='processing_timeout' WHERE job_id=$1 AND finished_at IS NULL`, j.ID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if attempts >= maxAttempts {
|
||||
if err = failJob(ctx, tx, j, "retry_exhausted", attempts); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", tx.Commit(ctx)
|
||||
}
|
||||
token := uuid.NewString()
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_jobs SET attempts=attempts+1,lease_token=$2,lease_until=now()+$3*interval '1 second' WHERE id=$1`, j.ID, token, timeout.Seconds()+5)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET status='processing',updated_at=now() WHERE id=$1`, j.AttachmentID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_job_attempts(job_id,generation,attempt) VALUES($1,$2,$3)`, j.ID, generation, attempts+1); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
var ErrStaleJob = errors.New("stale job lease or version")
|
||||
|
||||
var ErrProcessingTimeout = errors.New("processing timeout")
|
||||
|
||||
// ProcessJob holds the row lock across deterministic object writes. The lock and
|
||||
// token check prevent overlapping/stale attempts from publishing metadata.
|
||||
func (s *AttachmentStore) ProcessJob(ctx context.Context, j Job, token string, maxAttempts int, process func(Attachment, string) ([]Variant, string, error)) (result error) {
|
||||
ctx, finish := telemetry.Operation(ctx, "postgresql finalize attachment", trace.SpanKindClient)
|
||||
defer func() { finish(result) }()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var staging string
|
||||
var attempts, generation int
|
||||
var a Attachment
|
||||
err = tx.QueryRow(ctx, `SELECT a.id,a.object_key,a.content_type,a.size_bytes,a.sha256,a.status,j.staging_key,j.attempts,j.generation FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1 AND j.attachment_id=$2 AND j.pipeline_version=$3 AND j.lease_token=$4 AND j.lease_until>now() AND j.superseded_at IS NULL AND a.pipeline_version=j.pipeline_version FOR UPDATE OF j,a`, j.ID, j.AttachmentID, j.PipelineVersion, token).Scan(&a.ID, &a.ObjectKey, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status, &staging, &attempts, &generation)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrStaleJob
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if a.Status != "processing" {
|
||||
return errors.New("job is not processing")
|
||||
}
|
||||
a.PipelineVersion = j.PipelineVersion
|
||||
variants, code, workErr := process(a, staging)
|
||||
var next *time.Time
|
||||
var auditCode *string
|
||||
if workErr == nil {
|
||||
data, e := json.Marshal(variants)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE attachments SET status='ready',variants=$2,published_pipeline_version=$3,error_code=NULL,updated_at=now() WHERE id=$1 AND pipeline_version=$3`, a.ID, data, j.PipelineVersion)
|
||||
if err == nil {
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachment_publications(attachment_id,job_id,pipeline_version,variants) VALUES($1,$2,$3,$4)`, a.ID, j.ID, j.PipelineVersion, data)
|
||||
}
|
||||
} else {
|
||||
reason := "temporary_failure"
|
||||
if errors.Is(workErr, ErrProcessingTimeout) {
|
||||
reason = "processing_timeout"
|
||||
}
|
||||
if code != "" {
|
||||
reason = code
|
||||
}
|
||||
auditCode = &reason
|
||||
if code != "" || attempts >= maxAttempts {
|
||||
if code == "" {
|
||||
code = "retry_exhausted"
|
||||
}
|
||||
err = failJob(ctx, tx, j, code, attempts)
|
||||
} else {
|
||||
due := time.Now().UTC().Add(RetryDelay(attempts))
|
||||
next = &due
|
||||
_, err = tx.Exec(ctx, `UPDATE attachments SET status='queued',error_code=NULL,updated_at=now() WHERE id=$1`, a.ID)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code=$4,next_attempt_at=$5 WHERE job_id=$1 AND generation=$2 AND attempt=$3`, j.ID, generation, attempts, auditCode, next)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE attachment_jobs SET lease_until=NULL,lease_token=NULL,published_at=NULL,next_attempt_at=COALESCE($2,now()) WHERE id=$1`, j.ID, next)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func failJob(ctx context.Context, tx pgx.Tx, j Job, code string, attempts int) error {
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachments SET status='failed',error_code=$2,updated_at=now() WHERE id=$1`, j.AttachmentID, code); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `UPDATE attachment_jobs SET lease_token=NULL,lease_until=NULL WHERE id=$1`, j.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := tx.Exec(ctx, `INSERT INTO attachment_job_failures(job_id,error_code,attempts) VALUES($1,$2,$3) ON CONFLICT(job_id) DO UPDATE SET error_code=EXCLUDED.error_code,attempts=EXCLUDED.attempts,failed_at=clock_timestamp()`, j.ID, code, attempts)
|
||||
return err
|
||||
}
|
||||
|
||||
// Equal jitter: exponential ceiling 2,4,8,...,30 seconds; delay in [ceiling/2,ceiling).
|
||||
func RetryDelay(attempt int) time.Duration {
|
||||
ceiling := 2 * time.Second
|
||||
for i := 1; i < attempt && ceiling < 30*time.Second; i++ {
|
||||
ceiling *= 2
|
||||
}
|
||||
if ceiling > 30*time.Second {
|
||||
ceiling = 30 * time.Second
|
||||
}
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(ceiling/2)))
|
||||
if err != nil {
|
||||
return ceiling
|
||||
}
|
||||
return ceiling/2 + time.Duration(n.Int64())
|
||||
}
|
||||
|
||||
func enqueueJob(ctx context.Context, tx pgx.Tx, a Attachment, staging string) error {
|
||||
_, err := tx.Exec(ctx, `UPDATE attachments SET status='queued' WHERE id=$1`, a.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
headers := telemetry.TraceHeaders(ctx)
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key,traceparent,tracestate) VALUES($1,$2,$3,$4,$5)`, uuid.NewString(), a.ID, staging, headers["traceparent"], headers["tracestate"])
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) JobTerminal(ctx context.Context, id string) (bool, error) {
|
||||
var done bool
|
||||
err := s.db.QueryRow(ctx, `SELECT j.superseded_at IS NOT NULL OR j.pipeline_version<a.pipeline_version OR a.status IN ('ready','failed') FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE j.id=$1`, id).Scan(&done)
|
||||
return done, err
|
||||
}
|
||||
|
||||
// PendingJobs counts durable outstanding work, including leases and scheduled retries.
|
||||
func (s *AttachmentStore) PendingJobs(ctx context.Context) (int64, float64, error) {
|
||||
var count int64
|
||||
var age float64
|
||||
err := s.db.QueryRow(ctx, `SELECT count(*), COALESCE(EXTRACT(EPOCH FROM now()-min(j.created_at)),0)::double precision
|
||||
FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id
|
||||
WHERE j.superseded_at IS NULL AND j.pipeline_version=a.pipeline_version AND a.status IN ('queued','processing')`).Scan(&count, &age)
|
||||
return count, age, err
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type MessageStore struct {
|
||||
db *pgxpool.Pool
|
||||
idempotencyTTL time.Duration
|
||||
}
|
||||
|
||||
func NewMessageStore(db *pgxpool.Pool) *MessageStore {
|
||||
return NewMessageStoreWithTTL(db, 24*time.Hour)
|
||||
}
|
||||
|
||||
func NewMessageStoreWithTTL(db *pgxpool.Pool, ttl time.Duration) *MessageStore {
|
||||
return &MessageStore{db: db, idempotencyTTL: ttl}
|
||||
}
|
||||
|
||||
func (s *MessageStore) Create(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
senderID string,
|
||||
text string,
|
||||
) (Message, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
message := Message{
|
||||
AttachmentIDs: []string{},
|
||||
ID: uuid.New().String(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", message.ID, message.ChatID, message.SenderID, message.Text, message.CreatedAt)
|
||||
return message, err
|
||||
}
|
||||
|
||||
func (s *MessageStore) ListPageByChat(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
cursor *MessageCursor,
|
||||
limit int,
|
||||
) ([]Message, bool, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
afterTime := time.Time{}
|
||||
afterID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != nil {
|
||||
afterTime = cursor.CreatedAt
|
||||
afterID = cursor.MessageID
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT id,chat_id,sender_id,text,created_at,ARRAY(SELECT attachment_id::text FROM message_attachments ma WHERE ma.message_id=messages.id ORDER BY position) FROM messages WHERE chat_id=$1 AND (created_at,id)>($2,$3::uuid) ORDER BY created_at,id LIMIT $4`, chatID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Message{}
|
||||
for rows.Next() {
|
||||
var m Message
|
||||
if err = rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
items = append(items, m)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
more := len(items) > limit
|
||||
if more {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, more, nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY,
|
||||
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash bytea NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
PRIMARY KEY (chat_id,user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members(user_id,chat_id);
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS messages_history_idx ON messages(chat_id,created_at,id);
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE sessions;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE message_idempotency;
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE message_idempotency (
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
key varchar(128) NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (sender_id, chat_id, key)
|
||||
);
|
||||
@@ -0,0 +1,5 @@
|
||||
-- An operator must first remove attachment-only messages before this rollback.
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) BETWEEN 1 AND 4000);
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
@@ -0,0 +1,22 @@
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX attachments_chat_idx ON attachments(chat_id);
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
position smallint NOT NULL CHECK (position BETWEEN 0 AND 9),
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
UNIQUE(message_id, position)
|
||||
);
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) <= 4000);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE upload_attempts;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE upload_attempts (
|
||||
id uuid PRIMARY KEY,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
cleanup_after timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX upload_attempts_cleanup_idx ON upload_attempts(cleanup_after,id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE direct_uploads;
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE direct_uploads (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL UNIQUE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
staging_key text NOT NULL UNIQUE,
|
||||
final_key text NOT NULL UNIQUE,
|
||||
state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','ready','rejected','expired')),
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL DEFAULT clock_timestamp()+interval '120 seconds',
|
||||
complete_before timestamptz NOT NULL DEFAULT clock_timestamp()+interval '720 seconds',
|
||||
cleanup_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '1320 seconds',
|
||||
purge_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '24 hours'
|
||||
);
|
||||
CREATE INDEX direct_uploads_cleanup_idx ON direct_uploads(cleanup_after,id);
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP TABLE attachment_jobs;
|
||||
ALTER TABLE attachments DROP COLUMN status, DROP COLUMN variants, DROP COLUMN error_code;
|
||||
@@ -0,0 +1,10 @@
|
||||
ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'ready' CHECK(status IN ('queued','processing','ready','failed'));
|
||||
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]';
|
||||
ALTER TABLE attachments ADD COLUMN error_code text;
|
||||
CREATE TABLE attachment_jobs (
|
||||
id uuid PRIMARY KEY, attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
|
||||
staging_key text NOT NULL, pipeline_version integer NOT NULL DEFAULT 1,
|
||||
attempts integer NOT NULL DEFAULT 0, next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
||||
lease_until timestamptz, published_at timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
DROP INDEX job_dispatch_due_idx;
|
||||
DROP TABLE attachment_job_replays,attachment_job_failures,attachment_job_attempts;
|
||||
ALTER TABLE attachment_jobs DROP COLUMN lease_token,DROP COLUMN generation,DROP COLUMN staging_deleted_at;
|
||||
@@ -0,0 +1,22 @@
|
||||
ALTER TABLE attachment_jobs ADD COLUMN lease_token uuid;
|
||||
ALTER TABLE attachment_jobs ADD COLUMN generation integer NOT NULL DEFAULT 1;
|
||||
ALTER TABLE attachment_jobs ADD COLUMN staging_deleted_at timestamptz;
|
||||
CREATE TABLE attachment_job_attempts (
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id), generation integer NOT NULL,
|
||||
attempt integer NOT NULL, started_at timestamptz NOT NULL DEFAULT now(),
|
||||
finished_at timestamptz, error_code text, next_attempt_at timestamptz,
|
||||
PRIMARY KEY(job_id,generation,attempt)
|
||||
);
|
||||
CREATE TABLE attachment_job_failures (
|
||||
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id), error_code text NOT NULL,
|
||||
attempts integer NOT NULL, failed_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE TABLE attachment_job_replays (
|
||||
id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
||||
reason text NOT NULL CHECK(char_length(reason) BETWEEN 1 AND 200),
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX job_dispatch_due_idx ON attachment_jobs(next_attempt_at,published_at);
|
||||
INSERT INTO attachment_job_failures(job_id,error_code,attempts,failed_at)
|
||||
SELECT j.id,COALESCE(a.error_code,'processing_failed'),j.attempts,a.updated_at
|
||||
FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.status='failed';
|
||||
@@ -0,0 +1,11 @@
|
||||
-- Downgrade is intentionally refused if multiple versions exist: never drop jobs.
|
||||
DO $$ BEGIN
|
||||
IF EXISTS(SELECT 1 FROM attachment_jobs WHERE pipeline_version<>1) THEN
|
||||
RAISE EXCEPTION 'cannot downgrade while versioned jobs exist';
|
||||
END IF;
|
||||
END $$;
|
||||
DROP TABLE attachment_publications,attachment_pipeline_upgrades;
|
||||
ALTER TABLE attachment_jobs DROP CONSTRAINT attachment_jobs_version_key;
|
||||
ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_attachment_id_key UNIQUE(attachment_id);
|
||||
ALTER TABLE attachment_jobs DROP COLUMN superseded_at;
|
||||
ALTER TABLE attachments DROP COLUMN pipeline_version,DROP COLUMN published_pipeline_version;
|
||||
@@ -0,0 +1,19 @@
|
||||
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK(pipeline_version BETWEEN 1 AND 2);
|
||||
ALTER TABLE attachments ADD COLUMN published_pipeline_version integer NOT NULL DEFAULT 0 CHECK(published_pipeline_version BETWEEN 0 AND 2);
|
||||
UPDATE attachments SET published_pipeline_version=1 WHERE status='ready';
|
||||
ALTER TABLE attachment_jobs DROP CONSTRAINT attachment_jobs_attachment_id_key;
|
||||
ALTER TABLE attachment_jobs ADD CONSTRAINT attachment_jobs_version_key UNIQUE(attachment_id,pipeline_version);
|
||||
ALTER TABLE attachment_jobs ADD COLUMN superseded_at timestamptz;
|
||||
CREATE TABLE attachment_pipeline_upgrades (
|
||||
id bigserial PRIMARY KEY, attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
from_version integer NOT NULL,to_version integer NOT NULL,reason text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE attachment_publications (
|
||||
id bigserial PRIMARY KEY,attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id),pipeline_version integer NOT NULL,
|
||||
variants jsonb NOT NULL,published_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
INSERT INTO attachment_publications(attachment_id,job_id,pipeline_version,variants)
|
||||
SELECT a.id,j.id,1,a.variants FROM attachments a JOIN attachment_jobs j ON j.attachment_id=a.id
|
||||
WHERE a.status='ready';
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE attachment_jobs DROP COLUMN tracestate, DROP COLUMN traceparent;
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE attachment_jobs
|
||||
ADD COLUMN traceparent text NOT NULL DEFAULT '',
|
||||
ADD COLUMN tracestate text NOT NULL DEFAULT '';
|
||||
@@ -0,0 +1,74 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// VariantObjectKey separates effects of different pipelines, including retries.
|
||||
func VariantObjectKey(id string, version int, name string) string {
|
||||
if version < 1 {
|
||||
version = 1
|
||||
}
|
||||
extension := ".jpg"
|
||||
if name == "optimized" {
|
||||
extension = ".webp"
|
||||
}
|
||||
return fmt.Sprintf("variants/%s/v%d/%s%s", id, version, name, extension)
|
||||
}
|
||||
|
||||
// UpgradePipeline is an explicit operator action. It never downgrades a result.
|
||||
// The row lock serializes this change with publication, replay and cleanup.
|
||||
func (s *AttachmentStore) UpgradePipeline(ctx context.Context, id string, version int, reason string, exists func(context.Context, string) error) (bool, error) {
|
||||
reason = strings.TrimSpace(reason)
|
||||
if reason == "" || utf8.RuneCountInString(reason) > 200 {
|
||||
return false, errors.New("upgrade reason must contain 1-200 characters")
|
||||
}
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var current int
|
||||
var status, original, source string
|
||||
err = tx.QueryRow(ctx, `SELECT a.pipeline_version,a.status,a.object_key,j.staging_key FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id WHERE a.id=$1 AND j.pipeline_version=a.pipeline_version FOR UPDATE OF j,a`, id).Scan(¤t, &status, &original, &source)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if version <= current {
|
||||
return false, nil
|
||||
}
|
||||
if version != current+1 || version > 2 {
|
||||
return false, errors.New("only sequential upgrade to pipeline 2 is supported")
|
||||
}
|
||||
|
||||
if status == "ready" {
|
||||
source = original
|
||||
}
|
||||
if source != "staging/jobs/"+id && source != "originals/"+id {
|
||||
return false, errors.New("invalid pipeline source")
|
||||
}
|
||||
if err = exists(ctx, source); err != nil {
|
||||
return false, errors.New("pipeline source unavailable")
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_jobs SET superseded_at=clock_timestamp(),lease_token=NULL,lease_until=NULL WHERE attachment_id=$1 AND pipeline_version<$2`, id, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachment_job_attempts SET finished_at=clock_timestamp(),error_code='superseded' WHERE job_id IN (SELECT id FROM attachment_jobs WHERE attachment_id=$1 AND pipeline_version<$2) AND finished_at IS NULL`, id, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key,pipeline_version) VALUES($1,$2,$3,$4)`, uuid.NewString(), id, source, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE attachments SET pipeline_version=$2,status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=$1`, id, version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO attachment_pipeline_upgrades(attachment_id,from_version,to_version,reason) VALUES($1,$2,$3,$4)`, id, current, version, reason); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ErrSessionNotFound = errors.New("session not found")
|
||||
|
||||
type SessionStore struct {
|
||||
client *redis.Client
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewRedisClient(address string) *redis.Client {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: address, PoolSize: 10, MaxRetries: -1,
|
||||
DialTimeout: time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second,
|
||||
})
|
||||
}
|
||||
|
||||
func NewSessionStore(client *redis.Client, prefix string) *SessionStore {
|
||||
return &SessionStore{client: client, prefix: prefix}
|
||||
}
|
||||
|
||||
func (s *SessionStore) key(tokenHash []byte) string {
|
||||
return s.prefix + hex.EncodeToString(tokenHash)
|
||||
}
|
||||
|
||||
func (s *SessionStore) Create(ctx context.Context, tokenHash []byte, userID string, expires time.Time) error {
|
||||
ttl := time.Until(expires)
|
||||
if ttl <= 0 {
|
||||
return errors.New("session expiry must be in the future")
|
||||
}
|
||||
return s.client.Set(ctx, s.key(tokenHash), userID, ttl).Err()
|
||||
}
|
||||
|
||||
func (s *SessionStore) UserID(ctx context.Context, tokenHash []byte) (string, error) {
|
||||
id, err := s.client.Get(ctx, s.key(tokenHash)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return "", ErrSessionNotFound
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *SessionStore) Revoke(ctx context.Context, tokenHash []byte) error {
|
||||
return s.client.Del(ctx, s.key(tokenHash)).Err()
|
||||
}
|
||||
|
||||
func (s *SessionStore) Ping(ctx context.Context) error {
|
||||
return s.client.Ping(ctx).Err()
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ObjectStore struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewObjectStoreFromEnvironment() (*ObjectStore, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" {
|
||||
return nil, errors.New("invalid S3_ENDPOINT")
|
||||
}
|
||||
read := func(name string) (string, error) {
|
||||
b, e := os.ReadFile(os.Getenv(name))
|
||||
if e != nil {
|
||||
return "", errors.New("cannot read " + name)
|
||||
}
|
||||
v := strings.TrimSpace(string(b))
|
||||
if v == "" {
|
||||
return "", errors.New("empty " + name)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
access, err := read("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret, err := read("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport.ResponseHeaderTimeout = 5 * time.Second
|
||||
transport.MaxIdleConnsPerHost = 8
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), Transport: transport})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
if bucket == "" {
|
||||
return nil, errors.New("S3_BUCKET is required")
|
||||
}
|
||||
return &ObjectStore{client: client, bucket: bucket}, nil
|
||||
}
|
||||
func (s *ObjectStore) Ping(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return errors.New("bucket missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *ObjectStore) Bootstrap(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if err = s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: os.Getenv("S3_REGION")}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// No anonymous policy: every object operation requires signed credentials.
|
||||
return s.client.SetBucketPolicy(ctx, s.bucket, "")
|
||||
}
|
||||
func (s *ObjectStore) Put(ctx context.Context, key string, body io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, body, size, minio.PutObjectOptions{ContentType: contentType, DisableMultipart: true})
|
||||
return err
|
||||
}
|
||||
func (s *ObjectStore) Delete(ctx context.Context, key string) error {
|
||||
return s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
func (s *ObjectStore) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
object, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// GetObject is lazy: force the request before writing HTTP 200 headers.
|
||||
if _, err = object.Stat(); err != nil {
|
||||
object.Close()
|
||||
return nil, err
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func readS3Secret(name string) (string, error) {
|
||||
data, err := os.ReadFile(os.Getenv(name))
|
||||
if err != nil {
|
||||
return "", errors.New("cannot read " + name)
|
||||
}
|
||||
value := strings.TrimSpace(string(data))
|
||||
if value == "" {
|
||||
return "", errors.New("empty " + name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// BootstrapS3 is the only application command that needs administrative credentials.
|
||||
func BootstrapS3(ctx context.Context) error {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" {
|
||||
return errors.New("invalid S3_ENDPOINT")
|
||||
}
|
||||
rootAccess, err := readS3Secret("S3_ROOT_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rootSecret, err := readS3Secret("S3_ROOT_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if access == rootAccess {
|
||||
return errors.New("API and root credentials must differ")
|
||||
}
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(rootAccess, rootSecret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION")})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := &ObjectStore{client: client, bucket: os.Getenv("S3_BUCKET")}
|
||||
if err = store.Bootstrap(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = bootstrapDirectLifecycle(ctx, client, store.bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := madmin.New(endpoint.Host, rootAccess, rootSecret, endpoint.Scheme == "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
policy := map[string]any{"Version": "2012-10-17", "Statement": []any{
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + store.bucket}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + store.bucket + "/originals/*", "arn:aws:s3:::" + store.bucket + "/staging/direct/*", "arn:aws:s3:::" + store.bucket + "/staging/jobs/*", "arn:aws:s3:::" + store.bucket + "/variants/*"}},
|
||||
}}
|
||||
data, err := json.Marshal(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
const name = "messenger-attachments-api"
|
||||
if err = admin.AddCannedPolicy(ctx, name, data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.SetUserReq(ctx, access, madmin.AddOrUpdateUserReq{SecretKey: secret, Status: madmin.AccountEnabled}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Explicitly assign the managed policy; creating a user alone grants no S3 rights.
|
||||
// This idempotent API is supported by our pinned MinIO server release.
|
||||
return admin.SetPolicy(ctx, name, access, false)
|
||||
}
|
||||
|
||||
func bootstrapDirectLifecycle(ctx context.Context, client *minio.Client, bucket string) error {
|
||||
config, err := client.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchLifecycleConfiguration" {
|
||||
return err
|
||||
}
|
||||
if config == nil {
|
||||
config = lifecycle.NewConfiguration()
|
||||
}
|
||||
const id = "messenger-direct-staging-expiration"
|
||||
rules := make([]lifecycle.Rule, 0, len(config.Rules)+1)
|
||||
for _, rule := range config.Rules {
|
||||
if rule.ID != id {
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
}
|
||||
rules = append(rules, lifecycle.Rule{ID: id, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "staging/direct/"}, Expiration: lifecycle.Expiration{Days: 1}})
|
||||
config.Rules = rules
|
||||
return client.SetBucketLifecycle(ctx, bucket, config)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type PostUpload struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Fields map[string]string `json:"fields"`
|
||||
}
|
||||
|
||||
type StoredObject struct {
|
||||
Size int64
|
||||
ETag string
|
||||
}
|
||||
|
||||
func IsObjectMissing(err error) bool {
|
||||
code := minio.ToErrorResponse(err).Code
|
||||
return code == "NoSuchKey" || code == "NoSuchObject" || code == "NotFound"
|
||||
}
|
||||
|
||||
func (s *ObjectStore) PresignDirect(ctx context.Context, key, mime string, size int64, expires time.Time) (PostUpload, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_PUBLIC_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" || endpoint.RawQuery != "" || endpoint.User != nil {
|
||||
return PostUpload{}, errors.New("invalid S3_PUBLIC_ENDPOINT")
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
signer, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), BucketLookup: minio.BucketLookupPath})
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
policy := minio.NewPostPolicy()
|
||||
for _, err = range []error{policy.SetBucket(s.bucket), policy.SetKey(key), policy.SetExpires(expires), policy.SetContentType(mime), policy.SetContentLengthRange(size, size), policy.SetSuccessStatusAction("204")} {
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
}
|
||||
target, fields, err := signer.PresignedPostPolicy(ctx, policy)
|
||||
if err != nil {
|
||||
return PostUpload{}, errors.New("cannot sign upload policy")
|
||||
}
|
||||
return PostUpload{Method: "POST", URL: target.String(), Fields: fields}, nil
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Stat(ctx context.Context, key string) (StoredObject, error) {
|
||||
object, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
return StoredObject{Size: object.Size, ETag: object.ETag}, err
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Copy(ctx context.Context, source, destination, etag string) error {
|
||||
_, err := s.client.CopyObject(ctx, minio.CopyDestOptions{Bucket: s.bucket, Object: destination}, minio.CopySrcOptions{Bucket: s.bucket, Object: source, MatchETag: etag})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const queryTimeout = 5 * time.Second
|
||||
|
||||
func boundedQuery(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, queryTimeout)
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// ReserveUpload is committed before any S3 PUT, so an interrupted request is tracked.
|
||||
func (s *AttachmentStore) ReserveUpload(ctx context.Context, a Attachment, grace time.Duration) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
_, err := s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second')`, a.ID, a.ObjectKey, grace.Seconds())
|
||||
return err
|
||||
}
|
||||
|
||||
// PublishUpload holds the journal row lock across PUT and metadata COMMIT.
|
||||
// Cleanup skips that row; a crash rolls back metadata but preserves the reservation.
|
||||
func (s *AttachmentStore) PublishUpload(ctx context.Context, a Attachment, put func(context.Context) error) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key string
|
||||
err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, a.ID).Scan(&key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if key != a.ObjectKey {
|
||||
return errors.New("upload reservation mismatch")
|
||||
}
|
||||
if err = put(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, "originals/"+a.ID, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = enqueueJob(ctx, tx, a, a.ObjectKey); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", a.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
type CleanupResult struct {
|
||||
Candidates int `json:"candidates"`
|
||||
Deleted int `json:"deleted"`
|
||||
Eligible int `json:"eligible"`
|
||||
Protected int `json:"protected"`
|
||||
Skipped int `json:"skipped"`
|
||||
Failed int `json:"failed"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
}
|
||||
|
||||
// CleanupUploads only visits journal entries; it never scans or deletes legacy data.
|
||||
func (s *AttachmentStore) CleanupUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM upload_attempts WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "protected":
|
||||
result.Protected++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key string
|
||||
err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Restrict destructive actions to server-generated keys even if a journal row is malformed.
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || (key != "originals/"+parsed.String() && key != "staging/jobs/"+parsed.String()) {
|
||||
return "", errors.New("invalid upload journal key")
|
||||
}
|
||||
var ready bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1 UNION ALL SELECT 1 FROM attachment_jobs WHERE staging_key=$1)", key).Scan(&ready); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if dryRun {
|
||||
if ready {
|
||||
return "protected", nil
|
||||
}
|
||||
return "eligible", nil
|
||||
}
|
||||
if !ready {
|
||||
// On DELETE failure retain the journal entry for the next pass.
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ready {
|
||||
return "protected", nil
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUserStore(db *pgxpool.Pool) *UserStore {
|
||||
return &UserStore{db: db}
|
||||
}
|
||||
|
||||
func (s *UserStore) Create(ctx context.Context, username, displayName string, passwordHash []byte) (User, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
user := User{
|
||||
ID: uuid.New().String(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(ctx, "INSERT INTO users(id,username,display_name,password_hash,created_at) VALUES($1,$2,$3,$4,$5)", user.ID, user.Username, user.DisplayName, passwordHash, user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetByID(ctx context.Context, id string) (User, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var user User
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at FROM users WHERE id=$1", id).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetCredentials(ctx context.Context, username string) (User, []byte, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var user User
|
||||
var packedHash []byte
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=$1", username).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &packedHash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return User{}, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, nil, err
|
||||
}
|
||||
return user, packedHash, nil
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"messenger/src/backend/repositories"
|
||||
"mime"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
const MaxUploadBytes int64 = 10485760
|
||||
const MaxImagePixels int64 = 40000000
|
||||
|
||||
var ErrPayloadTooLarge = errors.New("payload too large")
|
||||
var ErrUnsupportedMedia = errors.New("unsupported image")
|
||||
var ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
|
||||
type AttachmentService struct {
|
||||
metadata *repositories.AttachmentStore
|
||||
objects AttachmentObjects
|
||||
uploadSlot chan struct{}
|
||||
}
|
||||
|
||||
type AttachmentObjects interface {
|
||||
Put(context.Context, string, io.Reader, int64, string) error
|
||||
Get(context.Context, string) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
func NewAttachmentService(metadata *repositories.AttachmentStore, objects AttachmentObjects) *AttachmentService {
|
||||
return &AttachmentService{metadata: metadata, objects: objects, uploadSlot: make(chan struct{}, 1)}
|
||||
}
|
||||
func ValidateFilename(name string) error {
|
||||
if !utf8.ValidString(name) || strings.TrimSpace(name) == "" || utf8.RuneCountInString(name) > 255 {
|
||||
return InvalidInput{"filename must contain 1-255 characters"}
|
||||
}
|
||||
for _, r := range name {
|
||||
if r == '/' || r == '\\' || unicode.IsControl(r) {
|
||||
return InvalidInput{"invalid filename"}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func ValidateImage(data []byte, declared string) (string, error) {
|
||||
return ValidateImageReader(bytes.NewReader(data), declared)
|
||||
}
|
||||
|
||||
func ValidateImageReader(reader io.ReadSeeker, declared string) (string, error) {
|
||||
contentType, _, err := mime.ParseMediaType(declared)
|
||||
if err != nil || (contentType != "image/png" && contentType != "image/jpeg") {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
config, format, err := image.DecodeConfig(reader)
|
||||
if err != nil {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
actual := "image/" + format
|
||||
if actual != contentType {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
if config.Width <= 0 || config.Height <= 0 || int64(config.Width) > MaxImagePixels/int64(config.Height) {
|
||||
return "", ErrPayloadTooLarge
|
||||
}
|
||||
// DecodeConfig alone accepts truncated headers; decode only after bounding pixels.
|
||||
if _, err = reader.Seek(0, io.SeekStart); err != nil {
|
||||
return "", err
|
||||
}
|
||||
decoded, _, err := image.Decode(reader)
|
||||
if err != nil {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
_ = decoded
|
||||
return actual, nil
|
||||
}
|
||||
func (s *AttachmentService) Upload(ctx context.Context, userID, chatID, filename, contentType string, body io.Reader) (repositories.Attachment, error) {
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
grace, err := repositories.UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
directory := os.Getenv("UPLOAD_TMP_DIR")
|
||||
file, err := os.CreateTemp(directory, "upload-*")
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
// Linux keeps the open descriptor usable; no abandoned pathname survives a crash.
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(body, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.Attachment{}, ErrPayloadTooLarge
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
actual, err := ValidateImageReader(file, contentType)
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a := repositories.Attachment{ID: uuid.NewString(), ChatID: chatID, OwnerID: userID, Filename: filename, ContentType: actual, SizeBytes: size, SHA256: hex.EncodeToString(hash.Sum(nil)), Status: "queued", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
a.ObjectKey = "staging/jobs/" + a.ID
|
||||
if err = s.metadata.ReserveUpload(ctx, a, grace); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
err = s.metadata.PublishUpload(ctx, a, func(ctx context.Context) error {
|
||||
return s.objects.Put(ctx, a.ObjectKey, file, a.SizeBytes, a.ContentType)
|
||||
})
|
||||
// On any uncertainty leave the durable reservation to the cleanup worker.
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
func (s *AttachmentService) Get(ctx context.Context, id, userID string) (repositories.Attachment, error) {
|
||||
a, err := s.metadata.GetForMember(ctx, id, userID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
func (s *AttachmentService) Content(ctx context.Context, a repositories.Attachment) (io.ReadCloser, error) {
|
||||
return s.objects.Get(ctx, a.ObjectKey)
|
||||
}
|
||||
|
||||
func NormalizeAttachmentIDs(ids []string) ([]string, error) {
|
||||
if len(ids) > 10 {
|
||||
return nil, InvalidInput{"at most 10 attachments are allowed"}
|
||||
}
|
||||
result := make([]string, 0, len(ids))
|
||||
seen := map[string]bool{}
|
||||
for _, id := range ids {
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, InvalidInput{"invalid attachment id"}
|
||||
}
|
||||
id = strings.ToLower(parsed.String())
|
||||
if seen[id] {
|
||||
return nil, InvalidInput{"duplicate attachment id"}
|
||||
}
|
||||
seen[id] = true
|
||||
result = append(result, id)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ChatPage struct {
|
||||
Items []repositories.Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type ChatService struct {
|
||||
chats *repositories.ChatStore
|
||||
}
|
||||
|
||||
func NewChatService(chats *repositories.ChatStore) *ChatService {
|
||||
return &ChatService{chats: chats}
|
||||
}
|
||||
|
||||
func ChatHasMember(chat repositories.Chat, userID string) bool {
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(ctx context.Context, currentUserID, title string, memberIDs []string) (repositories.Chat, error) {
|
||||
if strings.TrimSpace(title) == "" {
|
||||
return repositories.Chat{}, InvalidInput{"title must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(title) > 100 {
|
||||
return repositories.Chat{}, InvalidInput{"title is too long"}
|
||||
}
|
||||
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must contain from 1 to 100 users"}
|
||||
}
|
||||
seen := make(map[string]struct{}, len(memberIDs))
|
||||
normalizedMembers := make([]string, len(memberIDs))
|
||||
for i, memberID := range memberIDs {
|
||||
parsed, err := uuid.Parse(memberID)
|
||||
if err != nil {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must contain valid UUIDs"}
|
||||
}
|
||||
memberID = parsed.String()
|
||||
if _, exists := seen[memberID]; exists {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must not contain duplicates"}
|
||||
}
|
||||
seen[memberID] = struct{}{}
|
||||
normalizedMembers[i] = memberID
|
||||
}
|
||||
if _, exists := seen[currentUserID]; !exists {
|
||||
return repositories.Chat{}, InvalidInput{"current user must be a chat member"}
|
||||
}
|
||||
chat, err := s.chats.Create(ctx, title, normalizedMembers)
|
||||
if errors.Is(err, repositories.ErrUnknownMember) {
|
||||
return repositories.Chat{}, InvalidInput{"all members must be existing users"}
|
||||
}
|
||||
return chat, err
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(ctx context.Context, currentUserID, chatID string) (repositories.Chat, error) {
|
||||
chat, err := s.chats.GetByID(ctx, chatID)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.Chat{}, ErrChatNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Chat{}, err
|
||||
}
|
||||
if !ChatHasMember(chat, currentUserID) {
|
||||
return repositories.Chat{}, ErrChatNotFound
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func EncodeChatCursor(cursor repositories.ChatCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func DecodeChatCursor(value string) (repositories.ChatCursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
var cursor repositories.ChatCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
if cursor.UserID == "" || cursor.ChatID == "" || cursor.CreatedAt.IsZero() {
|
||||
return repositories.ChatCursor{}, strconv.ErrSyntax
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.UserID); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(ctx context.Context, userID string, limit int, cursorToken string) (ChatPage, error) {
|
||||
if limit < 1 || limit > 100 {
|
||||
return ChatPage{}, InvalidInput{"limit must be between 1 and 100"}
|
||||
}
|
||||
var cursor *repositories.ChatCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := DecodeChatCursor(cursorToken)
|
||||
if err != nil || decoded.UserID != userID {
|
||||
return ChatPage{}, InvalidInput{"invalid cursor"}
|
||||
}
|
||||
cursor = &decoded
|
||||
}
|
||||
items, more, err := s.chats.ListPageForUser(ctx, userID, cursor, limit)
|
||||
if err != nil {
|
||||
return ChatPage{}, err
|
||||
}
|
||||
page := ChatPage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = []repositories.Chat{}
|
||||
}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
next, err := EncodeChatCursor(repositories.ChatCursor{UserID: userID, CreatedAt: last.CreatedAt, ChatID: last.ID})
|
||||
if err != nil {
|
||||
return ChatPage{}, err
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type DirectUploadInput struct {
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type DirectUploadResponse struct {
|
||||
repositories.DirectUpload
|
||||
Upload repositories.PostUpload `json:"upload"`
|
||||
}
|
||||
|
||||
type DirectObjects interface {
|
||||
AttachmentObjects
|
||||
PresignDirect(context.Context, string, string, int64, time.Time) (repositories.PostUpload, error)
|
||||
Stat(context.Context, string) (repositories.StoredObject, error)
|
||||
Copy(context.Context, string, string, string) error
|
||||
}
|
||||
|
||||
var checksumPattern = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
||||
|
||||
func (s *AttachmentService) InitDirect(ctx context.Context, user, chat string, input DirectUploadInput) (DirectUploadResponse, error) {
|
||||
if err := ValidateFilename(input.Filename); err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
if input.ContentType != "image/png" && input.ContentType != "image/jpeg" {
|
||||
return DirectUploadResponse{}, ErrUnsupportedMedia
|
||||
}
|
||||
if input.SizeBytes > MaxUploadBytes {
|
||||
return DirectUploadResponse{}, ErrPayloadTooLarge
|
||||
}
|
||||
if input.SizeBytes < 1 || !checksumPattern.MatchString(input.SHA256) {
|
||||
return DirectUploadResponse{}, InvalidInput{"size must be positive and sha256 must contain 64 lowercase hex characters"}
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return DirectUploadResponse{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
id, attachment := uuid.NewString(), uuid.NewString()
|
||||
u, err := s.metadata.CreateDirect(ctx, repositories.DirectUpload{ID: id, AttachmentID: attachment, OwnerID: user, ChatID: chat, Filename: input.Filename, ContentType: input.ContentType, SizeBytes: input.SizeBytes, SHA256: input.SHA256, StagingKey: "staging/direct/" + id, FinalKey: "originals/" + attachment})
|
||||
if err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
post, err := objects.PresignDirect(ctx, u.StagingKey, u.ContentType, u.SizeBytes, u.ExpiresAt)
|
||||
return DirectUploadResponse{DirectUpload: u, Upload: post}, err
|
||||
}
|
||||
|
||||
func (s *AttachmentService) CompleteDirect(ctx context.Context, user, chat, id string) (repositories.Attachment, error) {
|
||||
u, err := s.metadata.GetDirect(ctx, id, chat, user)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return repositories.Attachment{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.Get(ctx, u.AttachmentID, user)
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return repositories.Attachment{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
a, err := s.metadata.CompleteDirect(ctx, id, chat, user, func(ctx context.Context, u repositories.DirectUpload) error {
|
||||
info, err := objects.Stat(ctx, u.StagingKey)
|
||||
if repositories.IsObjectMissing(err) {
|
||||
return repositories.DirectError("upload_not_uploaded")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if info.Size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
// A signed staging key remains writable until expiry. Validate the isolated
|
||||
// final object, never a staging object that can change after validation.
|
||||
if err = objects.Copy(ctx, u.StagingKey, u.FinalKey, info.ETag); err != nil {
|
||||
return err
|
||||
}
|
||||
reader, err := objects.Get(ctx, u.FinalKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
file, err := os.CreateTemp(os.Getenv("UPLOAD_TMP_DIR"), "direct-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(reader, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
if hex.EncodeToString(hash.Sum(nil)) != u.SHA256 {
|
||||
return repositories.DirectError("upload_checksum_mismatch")
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = ValidateImageReader(file, u.ContentType)
|
||||
if errors.Is(err, ErrPayloadTooLarge) {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if errors.Is(err, ErrUnsupportedMedia) {
|
||||
return repositories.DirectError("unsupported_media_type")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.Err()
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package services
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrUnknownUser = errors.New("unknown user")
|
||||
ErrChatNotFound = errors.New("chat not found")
|
||||
ErrInvalidSession = errors.New("invalid session")
|
||||
)
|
||||
|
||||
type InvalidInput struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e InvalidInput) Error() string { return e.Message }
|
||||
@@ -0,0 +1,34 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type HealthService struct {
|
||||
health *repositories.HealthStore
|
||||
sessions *repositories.SessionStore
|
||||
objects interface{ Ping(context.Context) error }
|
||||
}
|
||||
|
||||
func NewHealthService(health *repositories.HealthStore, sessions *repositories.SessionStore, objects ...interface{ Ping(context.Context) error }) *HealthService {
|
||||
service := &HealthService{health: health, sessions: sessions}
|
||||
if len(objects) > 0 {
|
||||
service.objects = objects[0]
|
||||
}
|
||||
return service
|
||||
}
|
||||
|
||||
func (s *HealthService) Ready(ctx context.Context) error {
|
||||
if err := s.health.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.sessions.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.objects != nil {
|
||||
return s.objects.Ping(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (s *MessageService) CreateIdempotent(ctx context.Context, userID string, chat repositories.Chat, text, key string, attachments ...[]string) (repositories.Message, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return repositories.Message{}, ErrChatNotFound
|
||||
}
|
||||
if !idempotencyKeyPattern.MatchString(key) {
|
||||
return repositories.Message{}, InvalidInput{"Idempotency-Key must contain 1-128 ASCII letters, digits or ._:-"}
|
||||
}
|
||||
var ids []string
|
||||
if len(attachments) > 0 {
|
||||
ids = attachments[0]
|
||||
}
|
||||
ids, err := NormalizeAttachmentIDs(ids)
|
||||
if err != nil {
|
||||
return repositories.Message{}, err
|
||||
}
|
||||
if (strings.TrimSpace(text) == "" && len(ids) == 0) || utf8.RuneCountInString(text) > 4000 {
|
||||
return repositories.Message{}, InvalidInput{"text must be at most 4000 characters; nonblank text or attachments required"}
|
||||
}
|
||||
message, err := s.messages.CreateIdempotent(ctx, chat.ID, userID, text, key, ids)
|
||||
if errors.Is(err, repositories.ErrAttachmentNotFound) {
|
||||
return repositories.Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
if errors.Is(err, repositories.ErrIdempotencyConflict) || errors.Is(err, repositories.ErrAttachmentFailed) {
|
||||
return repositories.Message{}, ErrConflict
|
||||
}
|
||||
return message, err
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type MessagePage struct {
|
||||
Items []repositories.Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type MessageService struct {
|
||||
messages *repositories.MessageStore
|
||||
}
|
||||
|
||||
func NewMessageService(messages *repositories.MessageStore) *MessageService {
|
||||
return &MessageService{messages: messages}
|
||||
}
|
||||
|
||||
func EncodeMessageCursor(cursor repositories.MessageCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func DecodeMessageCursor(value string) (repositories.MessageCursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
var cursor repositories.MessageCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
if cursor.ChatID == "" || cursor.MessageID == "" || cursor.CreatedAt.IsZero() {
|
||||
return repositories.MessageCursor{}, strconv.ErrSyntax
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.MessageID); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (s *MessageService) List(ctx context.Context, userID string, chat repositories.Chat, limit int, cursorToken string) (MessagePage, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return MessagePage{}, ErrChatNotFound
|
||||
}
|
||||
if limit < 1 || limit > 100 {
|
||||
return MessagePage{}, InvalidInput{"limit must be between 1 and 100"}
|
||||
}
|
||||
var cursor *repositories.MessageCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := DecodeMessageCursor(cursorToken)
|
||||
if err != nil || decoded.ChatID != chat.ID {
|
||||
return MessagePage{}, InvalidInput{"invalid cursor"}
|
||||
}
|
||||
cursor = &decoded
|
||||
}
|
||||
items, more, err := s.messages.ListPageByChat(ctx, chat.ID, cursor, limit)
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
page := MessagePage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = []repositories.Message{}
|
||||
}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
next, err := EncodeMessageCursor(repositories.MessageCursor{ChatID: chat.ID, CreatedAt: last.CreatedAt, MessageID: last.ID})
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonMemory uint32 = 64 * 1024
|
||||
argonIterations uint32 = 3
|
||||
argonParallelism uint8 = 2
|
||||
passwordSaltSize = 16
|
||||
passwordHashSize = 32
|
||||
)
|
||||
|
||||
// HashPassword stores the salt and Argon2id parameters in one PHC-encoded value.
|
||||
func HashPassword(password string) ([]byte, error) {
|
||||
salt := make([]byte, passwordSaltSize)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hash := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, passwordHashSize)
|
||||
encoded := fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, argonMemory, argonIterations, argonParallelism,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(hash),
|
||||
)
|
||||
return []byte(encoded), nil
|
||||
}
|
||||
|
||||
func VerifyPassword(password string, encoded []byte) (bool, error) {
|
||||
parts := strings.Split(string(encoded), "$")
|
||||
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" || parts[2] != fmt.Sprintf("v=%d", argon2.Version) {
|
||||
return false, errors.New("invalid password hash format")
|
||||
}
|
||||
var memory, iterations uint32
|
||||
var parallelism uint8
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil ||
|
||||
fmt.Sprintf("m=%d,t=%d,p=%d", memory, iterations, parallelism) != parts[3] ||
|
||||
memory < 8*1024 || memory > 128*1024 || iterations < 1 || iterations > 10 || parallelism < 1 || parallelism > 8 {
|
||||
return false, errors.New("invalid password hash parameters")
|
||||
}
|
||||
salt, err := base64.RawStdEncoding.Strict().DecodeString(parts[4])
|
||||
if err != nil || len(salt) != passwordSaltSize {
|
||||
return false, errors.New("invalid password salt")
|
||||
}
|
||||
expected, err := base64.RawStdEncoding.Strict().DecodeString(parts[5])
|
||||
if err != nil || len(expected) != passwordHashSize {
|
||||
return false, errors.New("invalid password hash")
|
||||
}
|
||||
actual := argon2.IDKey([]byte(password), salt, iterations, memory, parallelism, passwordHashSize)
|
||||
return subtle.ConstantTimeCompare(expected, actual) == 1, nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type SessionService struct {
|
||||
users *UserService
|
||||
sessions *repositories.SessionStore
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func SessionTTL() (time.Duration, error) {
|
||||
value := os.Getenv("SESSION_TTL_SECONDS")
|
||||
if value == "" {
|
||||
return time.Hour, nil
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil || seconds < 1 || seconds > 31536000 {
|
||||
return 0, errors.New("invalid SESSION_TTL_SECONDS")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
|
||||
func NewSessionService(users *UserService, sessions *repositories.SessionStore, ttl time.Duration) *SessionService {
|
||||
return &SessionService{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
func (s *SessionService) Login(ctx context.Context, username, password string) (repositories.User, string, time.Time, bool, error) {
|
||||
user, valid, err := s.users.Authenticate(ctx, username, password)
|
||||
if err != nil || !valid {
|
||||
return repositories.User{}, "", time.Time{}, valid, err
|
||||
}
|
||||
token, expires, err := s.CreateForUser(ctx, user.ID)
|
||||
if err != nil {
|
||||
return repositories.User{}, "", time.Time{}, false, err
|
||||
}
|
||||
return user, token, expires, true, nil
|
||||
}
|
||||
|
||||
func (s *SessionService) CreateForUser(ctx context.Context, userID string) (string, time.Time, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
token := hex.EncodeToString(raw)
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
expires := time.Now().UTC().Add(s.ttl).Truncate(time.Microsecond)
|
||||
if err := s.sessions.Create(ctx, hash[:], userID, expires); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
return token, expires, nil
|
||||
}
|
||||
|
||||
func (s *SessionService) User(ctx context.Context, token string) (repositories.User, error) {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
id, err := s.sessions.UserID(ctx, hash[:])
|
||||
if errors.Is(err, repositories.ErrSessionNotFound) {
|
||||
return repositories.User{}, ErrInvalidSession
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.User{}, err
|
||||
}
|
||||
user, err := s.users.Get(ctx, id)
|
||||
if errors.Is(err, ErrUnknownUser) {
|
||||
return repositories.User{}, ErrInvalidSession
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *SessionService) Revoke(ctx context.Context, token string) error {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
return s.sessions.Revoke(ctx, hash[:])
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type UserService struct {
|
||||
users *repositories.UserStore
|
||||
}
|
||||
|
||||
func NewUserService(users *repositories.UserStore) *UserService {
|
||||
return &UserService{users: users}
|
||||
}
|
||||
|
||||
func (s *UserService) Create(ctx context.Context, username, displayName, password string) (repositories.User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
return repositories.User{}, InvalidInput{"username must contain 3-32 lowercase latin letters, digits or underscores"}
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return repositories.User{}, InvalidInput{"display_name must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(displayName) > 100 {
|
||||
return repositories.User{}, InvalidInput{"display_name is too long"}
|
||||
}
|
||||
if strings.TrimSpace(password) == "" || utf8.RuneCountInString(password) < 12 || utf8.RuneCountInString(password) > 128 {
|
||||
return repositories.User{}, InvalidInput{"password must contain 12-128 characters"}
|
||||
}
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return repositories.User{}, err
|
||||
}
|
||||
user, err := s.users.Create(ctx, username, displayName, hash)
|
||||
if repositories.UniqueViolation(err) {
|
||||
return repositories.User{}, ErrConflict
|
||||
}
|
||||
if repositories.CheckViolation(err) {
|
||||
return repositories.User{}, InvalidInput{"invalid user fields"}
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) Get(ctx context.Context, id string) (repositories.User, error) {
|
||||
user, err := s.users.GetByID(ctx, id)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.User{}, ErrUnknownUser
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) Authenticate(ctx context.Context, username, password string) (repositories.User, bool, error) {
|
||||
if !usernamePattern.MatchString(username) || utf8.RuneCountInString(password) < 12 || utf8.RuneCountInString(password) > 128 {
|
||||
return repositories.User{}, false, nil
|
||||
}
|
||||
user, packedHash, err := s.users.GetCredentials(ctx, username)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.User{}, false, nil
|
||||
}
|
||||
if err != nil || packedHash == nil {
|
||||
return repositories.User{}, false, err
|
||||
}
|
||||
valid, err := VerifyPassword(password, packedHash)
|
||||
if err != nil {
|
||||
return repositories.User{}, false, err
|
||||
}
|
||||
return user, valid, nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
)
|
||||
|
||||
func (s *Signals) ObservePool(pool *pgxpool.Pool) error {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
used, err := s.meter.Int64ObservableGauge("messenger.db.pool.used")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
capacity, err := s.meter.Int64ObservableGauge("messenger.db.pool.capacity")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
waited, err := s.meter.Int64ObservableCounter("messenger.db.pool.waited")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.meter.RegisterCallback(func(_ context.Context, observer metric.Observer) error {
|
||||
stats := pool.Stat()
|
||||
observer.ObserveInt64(used, int64(stats.AcquiredConns()))
|
||||
observer.ObserveInt64(capacity, int64(stats.MaxConns()))
|
||||
observer.ObserveInt64(waited, stats.EmptyAcquireCount())
|
||||
return nil
|
||||
}, used, capacity, waited)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Signals) ObserveBacklog(read func(context.Context) (int64, float64, error)) error {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
depth, err := s.meter.Int64ObservableGauge("messenger.queue.depth")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
age, err := s.meter.Float64ObservableGauge("messenger.queue.oldest", metric.WithUnit("s"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.meter.RegisterCallback(func(ctx context.Context, observer metric.Observer) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
||||
defer cancel()
|
||||
count, seconds, err := read(ctx)
|
||||
if err == nil {
|
||||
queue := metric.WithAttributes(attribute.String("queue.name", "attachments.v1"))
|
||||
observer.ObserveInt64(depth, count, queue)
|
||||
observer.ObserveFloat64(age, seconds, queue)
|
||||
}
|
||||
return nil
|
||||
}, depth, age)
|
||||
return err
|
||||
}
|
||||
|
||||
// ObserveProcess exports Linux process CPU time and resident memory.
|
||||
func (s *Signals) ObserveProcess() error {
|
||||
cpu, err := s.meter.Float64ObservableCounter("messenger.process.cpu", metric.WithUnit("s"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rss, err := s.meter.Int64ObservableGauge("messenger.process.rss", metric.WithUnit("By"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.meter.RegisterCallback(func(_ context.Context, observer metric.Observer) error {
|
||||
var usage syscall.Rusage
|
||||
if syscall.Getrusage(syscall.RUSAGE_SELF, &usage) == nil {
|
||||
observer.ObserveFloat64(cpu, float64(usage.Utime.Nano()+usage.Stime.Nano())/float64(time.Second))
|
||||
}
|
||||
if data, err := os.ReadFile("/proc/self/statm"); err == nil {
|
||||
fields := strings.Fields(string(data))
|
||||
if len(fields) >= 2 {
|
||||
if pages, err := strconv.ParseInt(fields[1], 10, 64); err == nil {
|
||||
observer.ObserveInt64(rss, pages*int64(os.Getpagesize()))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, cpu, rss)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
otellog "go.opentelemetry.io/otel/log"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type responseStatus struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (w *responseStatus) WriteHeader(status int) {
|
||||
if w.status == 0 && status >= 200 {
|
||||
w.status = status
|
||||
}
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (w *responseStatus) Write(body []byte) (int, error) {
|
||||
if w.status == 0 {
|
||||
w.status = http.StatusOK
|
||||
}
|
||||
return w.ResponseWriter.Write(body)
|
||||
}
|
||||
|
||||
func (w *responseStatus) Unwrap() http.ResponseWriter { return w.ResponseWriter }
|
||||
|
||||
func (s *Signals) HTTP(next http.Handler) http.Handler {
|
||||
if s == nil {
|
||||
return next
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
route := routeTemplate(r.URL.Path)
|
||||
if route == "/health/live" || route == "/health/ready" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
method := r.Method
|
||||
switch method {
|
||||
case "GET", "POST", "DELETE", "OPTIONS", "HEAD", "PUT", "PATCH":
|
||||
default:
|
||||
method = "_OTHER"
|
||||
}
|
||||
start := time.Now()
|
||||
ctx, span := s.tracer.Start(r.Context(), method+" "+route, trace.WithSpanKind(trace.SpanKindServer))
|
||||
defer span.End()
|
||||
response := &responseStatus{ResponseWriter: w}
|
||||
next.ServeHTTP(response, r.WithContext(ctx))
|
||||
status := response.status
|
||||
if status == 0 {
|
||||
status = http.StatusOK
|
||||
}
|
||||
failed := status >= 500
|
||||
attrs := []attribute.KeyValue{
|
||||
attribute.String("http.request.method", method),
|
||||
attribute.String("http.route", route),
|
||||
attribute.String("http.status.class", strconv.Itoa(status/100)+"xx"),
|
||||
}
|
||||
span.SetAttributes(attrs...)
|
||||
span.SetAttributes(attribute.Int("http.response.status_code", status))
|
||||
if failed {
|
||||
span.SetStatus(codes.Error, "")
|
||||
}
|
||||
s.requests.Add(ctx, 1, metric.WithAttributes(attrs...))
|
||||
s.httpDuration.Record(ctx, time.Since(start).Seconds(), metric.WithAttributes(attrs...))
|
||||
s.event(ctx, "http.request.completed", failed,
|
||||
otellog.String("http.request.method", method),
|
||||
otellog.String("http.route", route),
|
||||
otellog.Int("http.response.status_code", status),
|
||||
otellog.String("request_id", w.Header().Get("X-Request-Id")))
|
||||
})
|
||||
}
|
||||
|
||||
func routeTemplate(path string) string {
|
||||
switch path {
|
||||
case "/health/live", "/health/ready", "/api/v1/users", "/api/v1/users/me",
|
||||
"/api/v1/auth/sessions", "/api/v1/auth/sessions/current", "/api/v1/chats":
|
||||
return path
|
||||
}
|
||||
parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
||||
if len(parts) < 4 || parts[0] != "api" || parts[1] != "v1" || parts[3] == "" {
|
||||
return "unmatched"
|
||||
}
|
||||
switch parts[2] {
|
||||
case "chats":
|
||||
if len(parts) == 4 {
|
||||
return "/api/v1/chats/{chat_id}"
|
||||
}
|
||||
if len(parts) == 5 && parts[4] == "attachment-uploads" {
|
||||
return "/api/v1/chats/{chat_id}/attachment-uploads"
|
||||
}
|
||||
if len(parts) == 7 && parts[4] == "attachment-uploads" && parts[6] == "complete" {
|
||||
return "/api/v1/chats/{chat_id}/attachment-uploads/{upload_id}/complete"
|
||||
}
|
||||
if len(parts) == 5 && (parts[4] == "messages" || parts[4] == "attachments") {
|
||||
return "/api/v1/chats/{chat_id}/" + parts[4]
|
||||
}
|
||||
case "attachments":
|
||||
if len(parts) == 4 {
|
||||
return "/api/v1/attachments/{attachment_id}"
|
||||
}
|
||||
if len(parts) == 5 && parts[4] == "content" {
|
||||
return "/api/v1/attachments/{attachment_id}/content"
|
||||
}
|
||||
}
|
||||
return "unmatched"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type PostgresTracer struct{}
|
||||
|
||||
type queryFinishKey struct{}
|
||||
|
||||
func (PostgresTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
|
||||
ctx, finish := Operation(ctx, "postgresql query", trace.SpanKindClient)
|
||||
return context.WithValue(ctx, queryFinishKey{}, finish)
|
||||
}
|
||||
|
||||
func (PostgresTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
|
||||
if finish, ok := ctx.Value(queryFinishKey{}).(func(error)); ok {
|
||||
finish(data.Err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func TraceHeaders(ctx context.Context) map[string]string {
|
||||
headers := propagation.MapCarrier{}
|
||||
propagation.TraceContext{}.Inject(ctx, headers)
|
||||
return headers
|
||||
}
|
||||
|
||||
func WithTraceHeaders(ctx context.Context, headers map[string]string) context.Context {
|
||||
return propagation.TraceContext{}.Extract(ctx, propagation.MapCarrier(headers))
|
||||
}
|
||||
|
||||
func (s *Signals) Publish(ctx context.Context, publish func(context.Context) error) error {
|
||||
if s == nil {
|
||||
return publish(ctx)
|
||||
}
|
||||
ctx, span := s.tracer.Start(ctx, "attachments.v1 publish", trace.WithSpanKind(trace.SpanKindProducer))
|
||||
defer span.End()
|
||||
err := publish(ctx)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Operation creates a child of the active span without recording arguments or raw errors.
|
||||
func Operation(ctx context.Context, name string, kind trace.SpanKind) (context.Context, func(error)) {
|
||||
parent := trace.SpanFromContext(ctx)
|
||||
if !parent.IsRecording() {
|
||||
return ctx, func(error) {}
|
||||
}
|
||||
ctx, span := parent.TracerProvider().Tracer("messenger").Start(ctx, name, trace.WithSpanKind(kind))
|
||||
return ctx, func(err error) {
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "")
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
otellog "go.opentelemetry.io/otel/log"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
sdklog "go.opentelemetry.io/otel/sdk/log"
|
||||
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type Signals struct {
|
||||
meter metric.Meter
|
||||
busy metric.Int64UpDownCounter
|
||||
ready metric.Float64Histogram
|
||||
tracer trace.Tracer
|
||||
logger otellog.Logger
|
||||
requests metric.Int64Counter
|
||||
httpDuration metric.Float64Histogram
|
||||
jobs metric.Int64Counter
|
||||
jobDuration metric.Float64Histogram
|
||||
}
|
||||
|
||||
func Start(service string) (*Signals, func(), error) {
|
||||
stop := func() {}
|
||||
if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" {
|
||||
return nil, stop, nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
instance, err := os.Hostname()
|
||||
if err != nil || instance == "" {
|
||||
instance = uuid.NewString()
|
||||
}
|
||||
res := resource.NewSchemaless(
|
||||
attribute.String("service.name", service),
|
||||
attribute.String("service.version", "lab6"),
|
||||
attribute.String("service.instance.id", instance),
|
||||
attribute.String("deployment.environment.name", "lab"),
|
||||
)
|
||||
traces, err := otlptracehttp.New(ctx, otlptracehttp.WithTimeout(2*time.Second))
|
||||
if err != nil {
|
||||
return nil, stop, err
|
||||
}
|
||||
metrics, err := otlpmetrichttp.New(ctx, otlpmetrichttp.WithTimeout(2*time.Second))
|
||||
if err != nil {
|
||||
_ = traces.Shutdown(ctx)
|
||||
return nil, stop, err
|
||||
}
|
||||
logs, err := otlploghttp.New(ctx, otlploghttp.WithTimeout(2*time.Second))
|
||||
if err != nil {
|
||||
_ = traces.Shutdown(ctx)
|
||||
_ = metrics.Shutdown(ctx)
|
||||
return nil, stop, err
|
||||
}
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithResource(res), sdktrace.WithBatcher(traces))
|
||||
mp := sdkmetric.NewMeterProvider(sdkmetric.WithResource(res), sdkmetric.WithReader(
|
||||
sdkmetric.NewPeriodicReader(metrics, sdkmetric.WithInterval(5*time.Second), sdkmetric.WithTimeout(3*time.Second))))
|
||||
lp := sdklog.NewLoggerProvider(sdklog.WithResource(res), sdklog.WithProcessor(sdklog.NewBatchProcessor(logs)))
|
||||
stop = func() {
|
||||
deadline, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
defer cancel()
|
||||
if errors.Join(tp.Shutdown(deadline), mp.Shutdown(deadline), lp.Shutdown(deadline)) != nil {
|
||||
log.Print("telemetry shutdown did not flush all signals")
|
||||
}
|
||||
}
|
||||
meter := mp.Meter("messenger")
|
||||
buckets := metric.WithExplicitBucketBoundaries(.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 30, 60)
|
||||
requests, errRequests := meter.Int64Counter("messenger.http.requests")
|
||||
httpDuration, errHTTP := meter.Float64Histogram("messenger.http.duration", metric.WithUnit("s"), buckets)
|
||||
jobs, errJobs := meter.Int64Counter("messenger.worker.jobs")
|
||||
jobDuration, errJobDuration := meter.Float64Histogram("messenger.worker.duration", metric.WithUnit("s"), buckets)
|
||||
busy, errBusy := meter.Int64UpDownCounter("messenger.worker.busy")
|
||||
capacity, errCapacity := meter.Int64UpDownCounter("messenger.worker.capacity")
|
||||
ready, errReady := meter.Float64Histogram("messenger.attachment.ready", metric.WithUnit("s"), buckets)
|
||||
if err := errors.Join(errRequests, errHTTP, errJobs, errJobDuration, errBusy, errCapacity, errReady); err != nil {
|
||||
stop()
|
||||
return nil, func() {}, err
|
||||
}
|
||||
if service == "messenger-worker" {
|
||||
capacity.Add(ctx, 1)
|
||||
busy.Add(ctx, 0)
|
||||
}
|
||||
signals := &Signals{meter: meter, busy: busy, ready: ready, tracer: tp.Tracer("messenger"),
|
||||
logger: lp.Logger("messenger"), requests: requests, httpDuration: httpDuration,
|
||||
jobs: jobs, jobDuration: jobDuration}
|
||||
if err := signals.ObserveProcess(); err != nil {
|
||||
stop()
|
||||
return nil, func() {}, err
|
||||
}
|
||||
return signals, stop, nil
|
||||
}
|
||||
|
||||
func (s *Signals) event(ctx context.Context, event string, failed bool, attrs ...otellog.KeyValue) {
|
||||
var record otellog.Record
|
||||
record.SetTimestamp(time.Now())
|
||||
record.SetSeverity(otellog.SeverityInfo)
|
||||
record.SetSeverityText("INFO")
|
||||
if failed {
|
||||
record.SetSeverity(otellog.SeverityError)
|
||||
record.SetSeverityText("ERROR")
|
||||
}
|
||||
record.AddAttributes(otellog.String("event", event))
|
||||
record.SetBody(otellog.StringValue(event))
|
||||
record.AddAttributes(attrs...)
|
||||
s.logger.Emit(ctx, record)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
otellog "go.opentelemetry.io/otel/log"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func (s *Signals) Job(ctx context.Context, jobType, attachmentID string, attempt int, accepted time.Time) (context.Context, func(string)) {
|
||||
if s == nil {
|
||||
return ctx, func(string) {}
|
||||
}
|
||||
start := time.Now()
|
||||
s.busy.Add(ctx, 1)
|
||||
ctx, span := s.tracer.Start(ctx, jobType+" process", trace.WithSpanKind(trace.SpanKindConsumer), trace.WithAttributes(attribute.Int("job.attempt", attempt)))
|
||||
return ctx, func(outcome string) {
|
||||
defer span.End()
|
||||
s.busy.Add(ctx, -1)
|
||||
if outcome == "success" {
|
||||
s.ready.Record(ctx, time.Since(accepted).Seconds())
|
||||
}
|
||||
attrs := []attribute.KeyValue{attribute.String("job.type", jobType), attribute.String("job.outcome", outcome)}
|
||||
span.SetAttributes(attrs...)
|
||||
failed := outcome == "error"
|
||||
if failed {
|
||||
span.SetStatus(codes.Error, "")
|
||||
}
|
||||
s.jobs.Add(ctx, 1, metric.WithAttributes(attrs...))
|
||||
s.jobDuration.Record(ctx, time.Since(start).Seconds(), metric.WithAttributes(attrs...))
|
||||
s.event(ctx, "job.processing.completed", failed, otellog.String("job.type", jobType), otellog.String("job.outcome", outcome), otellog.String("attachment_id", attachmentID))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestAttachmentCleanupGraceLocksProtectionAndRetry(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
makeAttempt := func(age bool, ready bool) repositories.Attachment {
|
||||
id := uuid.NewString()
|
||||
now := time.Now().UTC()
|
||||
a := repositories.Attachment{ID: id, ObjectKey: "originals/" + id, ChatID: chat, OwnerID: user, Filename: "cleanup.png", ContentType: "image/png", SizeBytes: int64(len(data)), SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", CreatedAt: now, UpdatedAt: now}
|
||||
if err := objects.Put(ctx, a.ObjectKey, bytes.NewReader(data), a.SizeBytes, a.ContentType); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), a.ObjectKey) })
|
||||
if err := store.ReserveUpload(ctx, a, 600*time.Second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if age {
|
||||
if _, err := f.db.Exec(ctx, "UPDATE upload_attempts SET cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1", id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if ready {
|
||||
if err := store.Create(ctx, a); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
fresh := makeAttempt(false, false)
|
||||
expired := makeAttempt(true, false)
|
||||
ready := makeAttempt(true, true)
|
||||
locked := makeAttempt(true, false)
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, "SELECT id FROM upload_attempts WHERE id=$1 FOR UPDATE", locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Deleted != 0 || dry.Candidates != 3 || dry.Protected != 1 {
|
||||
t.Fatalf("dry run: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 || result.Protected != 1 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
if content, err := objects.Get(ctx, expired.ObjectKey); err == nil {
|
||||
content.Close()
|
||||
t.Fatal("expired object not removed")
|
||||
}
|
||||
for _, a := range []repositories.Attachment{fresh, ready, locked} {
|
||||
content, err := objects.Get(ctx, a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatalf("protected object removed: %v", err)
|
||||
}
|
||||
content.Close()
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
failure, err := store.CleanupUploads(ctx, false, func(context.Context, string) error { return errors.New("injected S3 DELETE failure") })
|
||||
if err == nil || failure.Failed != 1 {
|
||||
t.Fatalf("DELETE failure: %+v %v", failure, err)
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts WHERE id=$1", locked.ID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Fatal("DELETE failure lost journal")
|
||||
}
|
||||
result, err = store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", result, err)
|
||||
}
|
||||
result, err = store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Candidates != 0 {
|
||||
t.Fatalf("repeat not idempotent: %+v %v", result, err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("cancelled DB operation succeeded")
|
||||
}
|
||||
f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts WHERE id=$1", fresh.ID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Fatal("DB failure lost fresh reservation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentCleanupInvalidKeyIsNeverDeleted(t *testing.T) {
|
||||
f, store, _, _, _ := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
if _, err := f.db.Exec(ctx, "INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()-interval '1 second')", uuid.NewString(), "do-not-delete/anything"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
result, err := store.CleanupUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || called || result.Failed != 1 {
|
||||
t.Fatalf("unsafe key: %+v %v delete=%v", result, err, called)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func attachmentRouter(f *testFixture, objects *repositories.ObjectStore) http.Handler {
|
||||
users := services.NewUserService(f.users)
|
||||
return handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(f.db), f.sessionStore), users, f.sessions, services.NewChatService(f.chats), services.NewMessageService(f.messages), services.NewAttachmentService(repositories.NewAttachmentStore(f.db), objects))
|
||||
}
|
||||
func attachmentCall(t *testing.T, f *testFixture, router http.Handler, method, path, user, mime string, body []byte, status int) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, path, bytes.NewReader(body))
|
||||
if mime != "" {
|
||||
r.Header.Set("Content-Type", mime)
|
||||
}
|
||||
if method == "POST" && strings.HasSuffix(path, "/messages") {
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
}
|
||||
if user != "" {
|
||||
authorizeTestRequest(t, f, r, user)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != status {
|
||||
t.Fatalf("%s %s: expected %d, got %d: %s", method, path, status, w.Code, w.Body.String())
|
||||
}
|
||||
if status >= 400 {
|
||||
var error struct {
|
||||
Error struct {
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &error); err != nil || error.Error.RequestID != w.Header().Get("X-Request-Id") {
|
||||
t.Fatal("invalid error envelope")
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
||||
func TestAttachmentValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachments?filename=photo.png"
|
||||
png := uploadPNG(t)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
mime string
|
||||
status int
|
||||
}{{bytes.Repeat([]byte("x"), 10485761), "image/png", 413}, {png[:40], "image/png", 415}, {png, "image/jpeg", 415}, {png, "text/plain", 415}} {
|
||||
attachmentCall(t, f, router, "POST", path, user, sample.mime, sample.body, sample.status)
|
||||
}
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
attachmentCall(t, f, router, "POST", path, user, "image/png", huge, 413)
|
||||
for _, name := range []string{"", " ", "../file.png", `a\b.png`, "a\x00.png", "a\n.png", strings.Repeat("x", 256)} {
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/attachments?"+url.Values{"filename": {name}}.Encode(), user, "image/png", png, 400)
|
||||
}
|
||||
// Exercise the real HTTP chunked path without Content-Length.
|
||||
server := httptest.NewServer(router)
|
||||
defer server.Close()
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader, writer := io.Pipe()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
defer writer.Close()
|
||||
io.Copy(writer, bytes.NewReader(bytes.Repeat([]byte("x"), 10485761)))
|
||||
}()
|
||||
request, err := http.NewRequest("POST", server.URL+path, reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
request.Header.Set("Content-Type", "image/png")
|
||||
response, err := server.Client().Do(request)
|
||||
if err != nil {
|
||||
reader.Close()
|
||||
<-done
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
reader.Close()
|
||||
<-done
|
||||
if response.StatusCode != 413 {
|
||||
t.Fatalf("chunked limit: %d", response.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentAccessHTTP(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
router := attachmentRouter(f, objects)
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
other := createTestChat(t, f.chats, "Other", []string{user})
|
||||
a, err := services.NewAttachmentService(store, objects).Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), a.ObjectKey) })
|
||||
for _, suffix := range []string{"", "/content"} {
|
||||
path := "/api/v1/attachments/" + a.ID + suffix
|
||||
attachmentCall(t, f, router, "GET", path, eve.ID, "", nil, 404)
|
||||
attachmentCall(t, f, router, "GET", path, "", "", nil, 401)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/attachments?filename=photo.png", eve.ID, "image/png", uploadPNG(t), 404)
|
||||
for _, target := range []string{uuid.NewString(), a.ID} {
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+target+`"]}`), 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+a.ID+`","`+a.ID+`"]}`), 400)
|
||||
}
|
||||
|
||||
func TestUploadRecoveryHTTPDisconnect(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
completed := make(chan struct{})
|
||||
router := attachmentRouter(f, objects)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer close(completed); router.ServeHTTP(w, r) }))
|
||||
defer server.Close()
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := strings.TrimPrefix(server.URL, "http://")
|
||||
connection, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw := "POST /api/v1/chats/" + chat + "/attachments?filename=aborted.png HTTP/1.1\r\nHost: " + address + "\r\nAuthorization: Bearer " + token + "\r\nContent-Type: image/png\r\nContent-Length: 10485760\r\n\r\npartial"
|
||||
if _, err = io.WriteString(connection, raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connection.Close()
|
||||
select {
|
||||
case <-completed:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("disconnected upload did not stop")
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(context.Background(), "SELECT count(*) FROM upload_attempts").Scan(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("incomplete HTTP body reserved an object")
|
||||
}
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) > 0 {
|
||||
t.Fatalf("temporary paths remain: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAttachmentImageValidation(t *testing.T) {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 3, 2))
|
||||
var pngData, jpegData bytes.Buffer
|
||||
if err := png.Encode(&pngData, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := jpeg.Encode(&jpegData, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/png"}, {jpegData.Bytes(), "image/jpeg"}} {
|
||||
if actual, err := services.ValidateImage(sample.data, sample.mime); err != nil || actual != sample.mime {
|
||||
t.Fatalf("valid image: %s %v", actual, err)
|
||||
}
|
||||
}
|
||||
huge := append([]byte(nil), pngData.Bytes()...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
if _, err := services.ValidateImage(huge, "image/png"); !errors.Is(err, services.ErrPayloadTooLarge) {
|
||||
t.Fatalf("pixel limit: %v", err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/jpeg"}, {jpegData.Bytes(), "image/png"}, {pngData.Bytes()[:40], "image/png"}, {[]byte("not an image"), "image/png"}, {pngData.Bytes(), "text/plain"}} {
|
||||
if _, err := services.ValidateImage(sample.data, sample.mime); !errors.Is(err, services.ErrUnsupportedMedia) {
|
||||
t.Fatalf("invalid image accepted: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentFilenameValidation(t *testing.T) {
|
||||
for _, name := range []string{"", " ", "../photo.png", `a\b.png`, "photo\x00.png", "photo\n.png"} {
|
||||
if services.ValidateFilename(name) == nil {
|
||||
t.Fatalf("accepted %q", name)
|
||||
}
|
||||
}
|
||||
if err := services.ValidateFilename("Фотография 1.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentMessageRequiresStringText(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Images", []string{user.ID})
|
||||
id := uuid.NewString()
|
||||
// Repository-level fixture: message validation does not fetch S3 bytes.
|
||||
err := repositories.NewAttachmentStore(f.db).Create(context.Background(), repositories.Attachment{
|
||||
ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "photo.png", ObjectKey: "test/" + id,
|
||||
ContentType: "image/png", SizeBytes: 1, SHA256: strings.Repeat("a", 64), CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, body := range []string{`{"attachment_ids":["` + id + `"]}`, `{"text":null,"attachment_ids":["` + id + `"]}`} {
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats/"+chat.ID+"/messages", strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("invalid request saved a message")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"messenger/src/backend/repositories"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConcurrentMessageCreation(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore := fixture.users, fixture.chats
|
||||
|
||||
const clientsCount = 10
|
||||
const messagesPerClient = 10
|
||||
const expectedMessages = clientsCount * messagesPerClient
|
||||
|
||||
memberIDs := make([]string, 0, clientsCount)
|
||||
users := make([]User, 0, clientsCount)
|
||||
|
||||
for i := range clientsCount {
|
||||
user, created := createTestUser(t, userStore,
|
||||
"user"+strconv.Itoa(i),
|
||||
"Пользователь "+strconv.Itoa(i),
|
||||
)
|
||||
|
||||
if !created {
|
||||
t.Fatalf("failed to create test user %d", i)
|
||||
}
|
||||
|
||||
users = append(users, user)
|
||||
memberIDs = append(memberIDs, user.ID)
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Конкурентный чат",
|
||||
memberIDs,
|
||||
)
|
||||
|
||||
server := httptest.NewServer(fixture.router)
|
||||
defer server.Close()
|
||||
|
||||
client := server.Client()
|
||||
tokens := make([]string, clientsCount)
|
||||
for i, user := range users {
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
tokens[i] = request.Header.Get("Authorization")
|
||||
}
|
||||
|
||||
var waitGroup sync.WaitGroup
|
||||
messageIDs := make(chan string, expectedMessages)
|
||||
errors := make(chan error, expectedMessages)
|
||||
|
||||
for clientIndex := range clientsCount {
|
||||
waitGroup.Add(1)
|
||||
|
||||
go func(index int) {
|
||||
defer waitGroup.Done()
|
||||
|
||||
for messageIndex := range messagesPerClient {
|
||||
body := fmt.Appendf(nil,
|
||||
`{"text":"client-%d message-%d"}`,
|
||||
index,
|
||||
messageIndex,
|
||||
)
|
||||
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPost,
|
||||
server.URL+"/api/v1/chats/"+chat.ID+"/messages",
|
||||
bytes.NewReader(body),
|
||||
)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: create request: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Authorization", tokens[index])
|
||||
request.Header.Set("Idempotency-Key", fmt.Sprintf("client-%d-message-%d", index, messageIndex))
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: send request: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
responseBody, readErr := io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
|
||||
if readErr != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: read response: %w",
|
||||
index,
|
||||
readErr,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: expected status %d, got %d; body: %s",
|
||||
index,
|
||||
http.StatusCreated,
|
||||
response.StatusCode,
|
||||
string(responseBody),
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
var message repositories.Message
|
||||
|
||||
if err := json.Unmarshal(responseBody, &message); err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: decode response: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if message.ID == "" {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: response contains empty message ID",
|
||||
index,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
messageIDs <- message.ID
|
||||
}
|
||||
}(clientIndex)
|
||||
}
|
||||
|
||||
waitGroup.Wait()
|
||||
|
||||
close(errors)
|
||||
close(messageIDs)
|
||||
|
||||
for err := range errors {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
createdIDs := make(map[string]bool)
|
||||
|
||||
for messageID := range messageIDs {
|
||||
if createdIDs[messageID] {
|
||||
t.Fatalf("duplicate message ID returned: %s", messageID)
|
||||
}
|
||||
|
||||
createdIDs[messageID] = true
|
||||
}
|
||||
|
||||
if len(createdIDs) != expectedMessages {
|
||||
t.Fatalf(
|
||||
"expected %d unique created messages, got %d",
|
||||
expectedMessages,
|
||||
len(createdIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Дополнительно проверяем историю через HTTP API.
|
||||
request, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
server.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=100",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create history request: %v", err)
|
||||
}
|
||||
|
||||
request.Header.Set("Authorization", tokens[0])
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("request message history: %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(response.Body)
|
||||
|
||||
t.Fatalf(
|
||||
"expected history status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
response.StatusCode,
|
||||
string(body),
|
||||
)
|
||||
}
|
||||
|
||||
var page messagePage
|
||||
|
||||
if err := json.NewDecoder(response.Body).Decode(&page); err != nil {
|
||||
t.Fatalf("decode message history: %v", err)
|
||||
}
|
||||
|
||||
if len(page.Items) != expectedMessages {
|
||||
t.Fatalf(
|
||||
"expected %d messages in history, got %d",
|
||||
expectedMessages,
|
||||
len(page.Items),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestConnectionFilesTakePrecedence(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database := filepath.Join(dir, "database")
|
||||
cache := filepath.Join(dir, "cache")
|
||||
if err := os.WriteFile(database, []byte("postgres://configured/db\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(cache, []byte("redis://:test-password@configured:6380/3\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("DATABASE_URL_FILE", database)
|
||||
t.Setenv("DATABASE_URL", "postgres://ignored/db")
|
||||
t.Setenv("CACHE_URL_FILE", cache)
|
||||
t.Setenv("REDIS_ADDR", "ignored:6379")
|
||||
url, err := repositories.DatabaseURL()
|
||||
if err != nil || url != "postgres://configured/db" {
|
||||
t.Fatalf("database file not used: %v", err)
|
||||
}
|
||||
client, err := repositories.RedisClientFromEnvironment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
options := client.Options()
|
||||
if options.Addr != "configured:6380" || options.DB != 3 || options.Password != "test-password" {
|
||||
t.Fatal("Redis URL settings not applied")
|
||||
}
|
||||
t.Setenv("CACHE_URL_FILE", filepath.Join(dir, "missing"))
|
||||
if _, err := repositories.RedisClientFromEnvironment(); err == nil {
|
||||
t.Fatal("missing file accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfiguredIdempotencyTTL(t *testing.T) {
|
||||
t.Setenv("IDEMPOTENCY_TTL_SECONDS", "2")
|
||||
ttl, err := repositories.IdempotencyTTL()
|
||||
if err != nil || ttl != 2*time.Second {
|
||||
t.Fatalf("TTL not applied: %v", err)
|
||||
}
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{user.ID})
|
||||
store := repositories.NewMessageStoreWithTTL(f.db, ttl)
|
||||
ctx := context.Background()
|
||||
original, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var remaining float64
|
||||
if err := f.db.QueryRow(ctx, "SELECT extract(epoch FROM expires_at-clock_timestamp()) FROM message_idempotency WHERE key='ttl-test'").Scan(&remaining); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if remaining <= 0 || remaining > 2 {
|
||||
t.Fatalf("unexpected remaining TTL: %f", remaining)
|
||||
}
|
||||
replay, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil || replay.ID != original.ID {
|
||||
t.Fatal("replay before expiry changed the message")
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, "UPDATE message_idempotency SET expires_at=clock_timestamp()-interval '1 second' WHERE key='ttl-test'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reused, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil || reused.ID == original.ID {
|
||||
t.Fatal("expired key was not reusable")
|
||||
}
|
||||
if len(listTestMessages(t, store, chat.ID)) != 2 {
|
||||
t.Fatal("expiry removed an original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidIdempotencyTTL(t *testing.T) {
|
||||
for _, value := range []string{"0", "-1", "abc", "31536001"} {
|
||||
t.Setenv("IDEMPOTENCY_TTL_SECONDS", value)
|
||||
if _, err := repositories.IdempotencyTTL(); err == nil {
|
||||
t.Fatalf("accepted TTL %q", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func TestCursorRejectsInvalidUUIDBeforeDatabaseAccess(t *testing.T) {
|
||||
const userID = "11111111-1111-4111-8111-111111111111"
|
||||
const chatID = "22222222-2222-4222-8222-222222222222"
|
||||
const messageID = "33333333-3333-4333-8333-333333333333"
|
||||
createdAt := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC)
|
||||
for _, invalid := range []string{"not-a-uuid", "ffffffff-ffff-ffff-ffff-fffffffffffz"} {
|
||||
for _, field := range []string{"user_id", "chat_id"} {
|
||||
t.Run("chats/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.ChatCursor{UserID: userID, ChatID: chatID, CreatedAt: createdAt}
|
||||
if field == "user_id" {
|
||||
cursor.UserID = invalid
|
||||
} else {
|
||||
cursor.ChatID = invalid
|
||||
}
|
||||
token, err := services.EncodeChatCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeChatCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
// A nil store proves that validation finishes before any SQL call.
|
||||
_, err = services.NewChatService(nil).List(context.Background(), userID, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, field := range []string{"chat_id", "message_id"} {
|
||||
t.Run("messages/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.MessageCursor{ChatID: chatID, MessageID: messageID, CreatedAt: createdAt}
|
||||
if field == "chat_id" {
|
||||
cursor.ChatID = invalid
|
||||
} else {
|
||||
cursor.MessageID = invalid
|
||||
}
|
||||
token, err := services.EncodeMessageCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeMessageCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
chat := repositories.Chat{ID: chatID, MemberIDs: []string{userID}}
|
||||
_, err = services.NewMessageService(nil).List(context.Background(), userID, chat, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCursorPreservesPosition(t *testing.T) {
|
||||
original := repositories.ChatCursor{
|
||||
UserID: "11111111-1111-4111-8111-111111111111",
|
||||
ChatID: "22222222-2222-4222-8222-222222222222",
|
||||
CreatedAt: time.Date(2026, 10, 10, 12, 0, 0, 123000, time.UTC),
|
||||
}
|
||||
token, err := services.EncodeChatCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeChatCursor(token)
|
||||
if err != nil || decoded != original {
|
||||
t.Fatalf("cursor changed: %+v, error: %v", decoded, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestDirectUploadCleanupSafetyAndReplay(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
fresh, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
expired, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
locked, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
ready, service := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
for _, u := range []string{fresh.ID, expired.ID, locked.ID, ready.ID} {
|
||||
record, err := store.GetDirect(ctx, u, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = objects.Put(ctx, record.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=ANY($1::uuid[])`, []string{expired.ID, locked.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `SELECT id FROM direct_uploads WHERE id=$1 FOR UPDATE`, locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupDirectUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Eligible != 2 || dry.Deleted != 0 || dry.Skipped != 1 {
|
||||
t.Fatalf("dry: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 2 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
for _, id := range []string{fresh.ID, locked.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("fresh/locked staging removed", err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{ready.ID, expired.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("eligible staging remains", err)
|
||||
}
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("ready original removed", err)
|
||||
}
|
||||
// A valid form can recreate staging after early cleanup. The receipt persists.
|
||||
if postDirect(t, ready.Upload, data, nil) != 204 {
|
||||
t.Fatal("replay failed")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || again.Deleted != 1 {
|
||||
t.Fatalf("replay cleanup: %+v %v", again, err)
|
||||
}
|
||||
u, _ := store.GetDirect(ctx, ready.ID, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("replayed staging remains")
|
||||
}
|
||||
same, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil || same.ID != a.ID {
|
||||
t.Fatal("cleanup broke idempotent complete", err)
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET purge_after=clock_timestamp()-interval '1 second',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = store.CleanupDirectUploads(ctx, false, objects.Delete); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("receipt purge removed final", err)
|
||||
}
|
||||
if _, err = store.GetForMember(ctx, a.ID, user); err != nil {
|
||||
t.Fatal("receipt purge removed metadata", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadCleanupDependencyFailureAndMalformedKey(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
u, _ := store.GetDirect(ctx, result.ID, chat, user)
|
||||
if err := objects.Put(ctx, u.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed, err := store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { return errors.New("S3 unavailable") })
|
||||
if err == nil || failed.Failed != 1 {
|
||||
t.Fatal("failed DELETE was lost")
|
||||
}
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("staging changed on failed DELETE", err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupDirectUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("DB cancellation ignored")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, "originals/"+u.AttachmentID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
failed, err = store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || failed.Failed != 1 || called {
|
||||
t.Fatal("malformed key reached S3 DELETE")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, u.StagingKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retried, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || retried.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", retried, err)
|
||||
}
|
||||
repeated, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || repeated.Deleted != 0 {
|
||||
t.Fatalf("repeat: %+v %v", repeated, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func TestDirectUploadAccessPermissionsAndLifecycle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
root := s3Client(t, true, false)
|
||||
app := s3Client(t, false, false)
|
||||
anon := s3Client(t, false, true)
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
key := "staging/direct/" + uuid.NewString()
|
||||
t.Cleanup(func() { root.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}) })
|
||||
if _, err := app.PutObject(ctx, bucket, key, bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
object, err := anon.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = object.Stat()
|
||||
object.Close()
|
||||
denied(t, err)
|
||||
_, err = app.PutObject(ctx, bucket, "staging/forbidden/"+uuid.NewString(), bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
config, err := root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Add only our own harmless prefix rule, verify bootstrap merges rather than replaces.
|
||||
ruleID := "direct-test-" + uuid.NewString()
|
||||
config.Rules = append(config.Rules, lifecycle.Rule{ID: ruleID, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "test-lifecycle/" + ruleID + "/"}, Expiration: lifecycle.Expiration{Days: 7}})
|
||||
if err = root.SetBucketLifecycle(ctx, bucket, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
c, e := root.GetBucketLifecycle(ctx, bucket)
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
return
|
||||
}
|
||||
rules := []lifecycle.Rule{}
|
||||
for _, r := range c.Rules {
|
||||
if r.ID != ruleID {
|
||||
rules = append(rules, r)
|
||||
}
|
||||
}
|
||||
c.Rules = rules
|
||||
if e = root.SetBucketLifecycle(ctx, bucket, c); e != nil {
|
||||
t.Error(e)
|
||||
}
|
||||
})
|
||||
for i := 0; i < 2; i++ {
|
||||
if err = repositories.BootstrapS3(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
config, err = root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
managed, preserved := 0, false
|
||||
for _, r := range config.Rules {
|
||||
if r.ID == ruleID {
|
||||
preserved = true
|
||||
}
|
||||
if r.ID == "messenger-direct-staging-expiration" {
|
||||
managed++
|
||||
if r.RuleFilter.Prefix != "staging/direct/" || r.Expiration.Days != 1 || r.Status != "Enabled" {
|
||||
t.Fatal("unsafe staging lifecycle rule")
|
||||
}
|
||||
}
|
||||
}
|
||||
if managed != 1 || !preserved {
|
||||
t.Fatal("bootstrap is not an idempotent lifecycle merge")
|
||||
}
|
||||
denied(t, app.SetBucketLifecycle(ctx, bucket, config))
|
||||
}
|
||||
|
||||
func TestDirectUploadAccessCORS(t *testing.T) {
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration")
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
origin string
|
||||
allowed bool
|
||||
}{{"http://127.0.0.1:5173", true}, {"http://localhost:5173", true}, {"https://untrusted.example", false}} {
|
||||
request, err := http.NewRequest("OPTIONS", os.Getenv("S3_ENDPOINT")+"/"+os.Getenv("S3_BUCKET"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Origin", sample.origin)
|
||||
request.Header.Set("Access-Control-Request-Method", "POST")
|
||||
request.Header.Set("Access-Control-Request-Headers", "content-type")
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
allowed := response.Header.Get("Access-Control-Allow-Origin") == sample.origin
|
||||
if allowed != sample.allowed || response.Header.Get("Access-Control-Allow-Origin") == "*" {
|
||||
t.Fatalf("unexpected CORS allowance for %s", sample.origin)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,478 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func directInput(data []byte, mime string) services.DirectUploadInput {
|
||||
sum := sha256.Sum256(data)
|
||||
return services.DirectUploadInput{Filename: "direct.png", ContentType: mime, SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}
|
||||
}
|
||||
|
||||
func beginDirect(t *testing.T, f *testFixture, store *repositories.AttachmentStore, objects *repositories.ObjectStore, user, chat string, data []byte, mime string) (services.DirectUploadResponse, *services.AttachmentService) {
|
||||
t.Helper()
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
result, err := service.InitDirect(context.Background(), user, chat, directInput(data, mime))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
return result, service
|
||||
}
|
||||
|
||||
func rememberDirect(t *testing.T, store *repositories.AttachmentStore, objects *repositories.ObjectStore, id, chat, user string) {
|
||||
t.Helper()
|
||||
u, err := store.GetDirect(context.Background(), id, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
objects.Delete(context.Background(), u.StagingKey)
|
||||
objects.Delete(context.Background(), u.FinalKey)
|
||||
})
|
||||
}
|
||||
|
||||
func postDirect(t *testing.T, p repositories.PostUpload, data []byte, change func(map[string]string)) int {
|
||||
t.Helper()
|
||||
fields := map[string]string{}
|
||||
for k, v := range p.Fields {
|
||||
fields[k] = v
|
||||
}
|
||||
if change != nil {
|
||||
change(fields)
|
||||
}
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
for k, v := range fields {
|
||||
if err := writer.WriteField(k, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
file, err := writer.CreateFormFile("file", "image.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = file.Write(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req, err := http.NewRequest("POST", p.URL, &body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
client := http.Client{Timeout: 10 * time.Second}
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal("S3 form request failed")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
io.Copy(io.Discard, response.Body)
|
||||
return response.StatusCode
|
||||
}
|
||||
|
||||
func requireDirectError(t *testing.T, err error, status int, code string) {
|
||||
t.Helper()
|
||||
var e repositories.UploadError
|
||||
if !errors.As(err, &e) || e.Status != status || e.Code != code {
|
||||
t.Fatalf("expected %d %s, got %v", status, code, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadFlow(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := append(uploadPNG(t), []byte("aaaa")...)
|
||||
var jpg bytes.Buffer
|
||||
if err := jpeg.Encode(&jpg, image.NewRGBA(image.Rect(0, 0, 8, 8)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{png, "image/png"}, {jpg.Bytes(), "image/jpeg"}} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if result.ExpiresAt.Sub(time.Now()) > 121*time.Second || result.CompleteBefore.Sub(result.ExpiresAt) < 599*time.Second {
|
||||
t.Fatal("wrong upload deadlines")
|
||||
}
|
||||
if status := postDirect(t, result.Upload, sample.data, nil); status != 204 {
|
||||
t.Fatalf("POST: %d", status)
|
||||
}
|
||||
a, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
router := attachmentRouter(f, objects)
|
||||
attachmentCall(t, f, router, "GET", "/api/v1/attachments/"+a.ID+"/content", user, "", nil, 409)
|
||||
if a.Status != "queued" {
|
||||
t.Fatal("direct complete must queue processing")
|
||||
}
|
||||
var jobs int
|
||||
if err = f.db.QueryRow(context.Background(), "SELECT count(*) FROM attachment_jobs WHERE attachment_id=$1", a.ID).Scan(&jobs); err != nil || jobs != 1 {
|
||||
t.Fatal("durable job missing", err)
|
||||
}
|
||||
message := []byte(`{"text":"","attachment_ids":["` + a.ID + `"]}`)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", message, 201)
|
||||
// Lost success response is represented by ignoring a, then asking another service.
|
||||
again, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil || again.ID != a.ID || !again.CreatedAt.Equal(a.CreatedAt) {
|
||||
t.Fatal("complete is not idempotent", err)
|
||||
}
|
||||
replacement := append([]byte(nil), sample.data...)
|
||||
replacement[len(replacement)-1] ^= 1
|
||||
if status := postDirect(t, result.Upload, replacement, nil); status != 204 {
|
||||
t.Fatal("expected valid staging replay")
|
||||
}
|
||||
final, err := objects.Get(context.Background(), a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original, err := io.ReadAll(final)
|
||||
final.Close()
|
||||
if err != nil || !bytes.Equal(original, sample.data) {
|
||||
t.Fatal("staging replay modified final")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
input := directInput(uploadPNG(t), "image/png")
|
||||
valid, _ := json.Marshal(input)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
status int
|
||||
}{
|
||||
{[]byte(`null`), 400}, {[]byte(`{}`), 400}, {append(append([]byte(nil), valid...), []byte(` {}`)...), 400},
|
||||
{[]byte(strings.Repeat("x", 4097)), 400}, {[]byte(`{"filename":"x","size_bytes":1.5}`), 400},
|
||||
} {
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", sample.body, sample.status)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
mutate func(*services.DirectUploadInput)
|
||||
status int
|
||||
}{
|
||||
{func(i *services.DirectUploadInput) { i.Filename = "../image.png" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SHA256 = "invalid" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 0 }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 10485761 }, 413},
|
||||
{func(i *services.DirectUploadInput) { i.ContentType = "text/plain" }, 415},
|
||||
} {
|
||||
i := input
|
||||
sample.mutate(&i)
|
||||
body, _ := json.Marshal(i)
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", body, sample.status)
|
||||
}
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
w := attachmentCall(t, f, router, "POST", path, user, "application/json", valid, 201)
|
||||
var result services.DirectUploadResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
if status := postDirect(t, result.Upload, uploadPNG(t), nil); status != 204 {
|
||||
t.Fatal("HTTP-created form failed")
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", user, "", nil, 202)
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationContents(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime, code string
|
||||
status int
|
||||
}{
|
||||
{png[:40], "image/png", "unsupported_media_type", 415},
|
||||
{png, "image/jpeg", "unsupported_media_type", 415},
|
||||
{huge, "image/png", "payload_too_large", 413},
|
||||
} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if postDirect(t, result.Upload, sample.data, nil) != 204 {
|
||||
t.Fatal("S3 refused matching declared policy")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
}
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
wrong := append([]byte(nil), png...)
|
||||
wrong[len(wrong)-1] ^= 1
|
||||
if postDirect(t, result.Upload, wrong, nil) != 204 {
|
||||
t.Fatal("form POST failed")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_checksum_mismatch")
|
||||
result, service = beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
if postDirect(t, result.Upload, append(png, 1), nil) < 400 {
|
||||
t.Fatal("S3 did not enforce exact policy size")
|
||||
}
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
// Simulate a trusted-side fault that bypassed policy; complete must still check.
|
||||
if err = objects.Put(context.Background(), u.StagingKey, bytes.NewReader(png[:40]), 40, "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_size_mismatch")
|
||||
}
|
||||
|
||||
func TestDirectUploadAccess(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
||||
if _, err := f.db.Exec(context.Background(), `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, chat, bob.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := createTestChat(t, f.chats, "other", []string{user})
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
for _, actor := range []string{eve.ID, bob.ID} {
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", actor, "", nil, 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", "", "", nil, 401)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/attachment-uploads/"+result.ID+"/complete", user, "", nil, 404)
|
||||
body, _ := json.Marshal(directInput(png, "image/png"))
|
||||
attachmentCall(t, f, router, "POST", path, eve.ID, "application/json", body, 404)
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+u.AttachmentID+`"]}`), 404)
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_not_uploaded")
|
||||
for _, change := range []func(map[string]string){
|
||||
func(fields map[string]string) { fields["key"] = "originals/" + uuid.NewString() },
|
||||
func(fields map[string]string) { fields["Content-Type"] = "image/jpeg" },
|
||||
func(fields map[string]string) { fields["x-amz-signature"] = strings.Repeat("0", 64) },
|
||||
} {
|
||||
if postDirect(t, result.Upload, png, change) < 400 {
|
||||
t.Fatal("S3 accepted tampered form")
|
||||
}
|
||||
}
|
||||
expired, err := objects.PresignDirect(context.Background(), u.StagingKey, "image/png", int64(len(png)), time.Now().Add(-time.Minute))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if postDirect(t, expired, png, nil) < 400 {
|
||||
t.Fatal("S3 accepted expired form")
|
||||
}
|
||||
if _, err = f.db.Exec(context.Background(), `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_expired")
|
||||
}
|
||||
|
||||
type directFault struct {
|
||||
*repositories.ObjectStore
|
||||
afterCopy func() error
|
||||
failGet bool
|
||||
}
|
||||
|
||||
func (f directFault) Copy(ctx context.Context, source, dest, etag string) error {
|
||||
if err := f.ObjectStore.Copy(ctx, source, dest, etag); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.afterCopy != nil {
|
||||
return f.afterCopy()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f directFault) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
if f.failGet {
|
||||
return nil, errors.New("injected GET failure")
|
||||
}
|
||||
return f.ObjectStore.Get(ctx, key)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryConcurrentComplete(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
ids := make(chan string, 2)
|
||||
errs := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
a, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
ids <- a.ID
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(ids)
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first := <-ids
|
||||
if second := <-ids; first == "" || first != second {
|
||||
t.Fatal("concurrent complete produced different attachments")
|
||||
}
|
||||
var n int
|
||||
if err := f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatal("unexpected metadata count", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryFailures(t *testing.T) {
|
||||
for _, name := range []string{"copy-result-loss", "get-failure", "metadata-rejection", "database-disconnect"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, normal := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
fault := directFault{ObjectStore: objects}
|
||||
switch name {
|
||||
case "copy-result-loss":
|
||||
fault.afterCopy = func() error { return errors.New("COPY acknowledgement lost") }
|
||||
case "get-failure":
|
||||
fault.failGet = true
|
||||
case "metadata-rejection":
|
||||
if _, err := f.db.Exec(context.Background(), `ALTER TABLE attachments ADD CONSTRAINT reject_direct CHECK (filename <> 'direct.png')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "database-disconnect":
|
||||
fault.afterCopy = func() error {
|
||||
_, err := f.db.Exec(context.Background(), `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name=$1 AND state='idle in transaction' AND pid<>pg_backend_pid()`, f.db.Config().ConnConfig.RuntimeParams["application_name"])
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := services.NewAttachmentService(store, fault).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err == nil {
|
||||
t.Fatal("expected injected failure")
|
||||
}
|
||||
var count int
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM upload_attempts`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("durable journal missing", err)
|
||||
}
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("published incomplete attachment", err)
|
||||
}
|
||||
if name == "metadata-rejection" {
|
||||
if _, err = f.db.Exec(context.Background(), `ALTER TABLE attachments DROP CONSTRAINT reject_direct`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err = normal.CompleteDirect(context.Background(), user, chat, result.ID); err != nil {
|
||||
t.Fatal("retry failed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryProcessExit(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
child := exec.Command(os.Args[0], "-test.run=^TestDirectUploadCrashHelper$")
|
||||
child.Env = append(os.Environ(), "DIRECT_CRASH_HELPER=1", "UPLOAD_TEST_DATABASE="+f.db.Config().ConnString(), "UPLOAD_TEST_SCHEMA="+f.db.Config().ConnConfig.RuntimeParams["search_path"], "UPLOAD_TEST_USER="+user, "UPLOAD_TEST_CHAT="+chat, "DIRECT_UPLOAD_ID="+result.ID)
|
||||
output, err := child.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 23 {
|
||||
t.Fatalf("helper did not crash after COPY: %v %s", err, output)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
|
||||
func TestDirectUploadCrashHelper(t *testing.T) {
|
||||
if os.Getenv("DIRECT_CRASH_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(os.Getenv("UPLOAD_TEST_DATABASE"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
objects := uploadObjects(t)
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error { os.Exit(23); return nil }}
|
||||
_, err = services.NewAttachmentService(repositories.NewAttachmentStore(db), fault).CompleteDirect(context.Background(), os.Getenv("UPLOAD_TEST_USER"), os.Getenv("UPLOAD_TEST_CHAT"), os.Getenv("DIRECT_UPLOAD_ID"))
|
||||
t.Fatalf("helper failed before exit: %v", err)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoverySharedSlotAndCancellation(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error {
|
||||
close(entered)
|
||||
<-release
|
||||
return errors.New("injected operation cancellation")
|
||||
}}
|
||||
service := services.NewAttachmentService(store, fault)
|
||||
done := make(chan error, 1)
|
||||
go func() { _, err := service.CompleteDirect(context.Background(), user, chat, result.ID); done <- err }()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("COPY not reached")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, proxyErr := service.Upload(ctx, user, chat, "waiting.png", "image/png", bytes.NewReader(data))
|
||||
_, directErr := service.CompleteDirect(ctx, user, chat, result.ID)
|
||||
close(release)
|
||||
if err := <-done; err == nil {
|
||||
t.Fatal("injected failure ignored")
|
||||
}
|
||||
if !errors.Is(proxyErr, context.Canceled) || !errors.Is(directErr, context.Canceled) {
|
||||
t.Fatalf("waiting requests ignored cancellation: proxy=%v direct=%v", proxyErr, directErr)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user