From 5c3f2bfad0575ab190de13e4066cb59895a4731b Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 11:54:05 +0300 Subject: [PATCH 01/21] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=201=20=D0=BA=D0=B0=D0=BA=20=D0=BE=D1=81=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D1=83=20=D0=B4=D0=BB=D1=8F=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 1 + .github/workflows/app.yml | 53 +++ Dockerfile | 20 + Makefile | 46 +++ cmd/server/main.go | 48 +++ compose.yaml | 12 + go.mod | 40 ++ go.sum | 91 +++++ internal/app/app.go | 39 ++ internal/app/app_test.go | 421 ++++++++++++++++++++ internal/auth/auth.go | 65 +++ internal/auth/auth_test.go | 91 +++++ internal/chat/chat.go | 73 ++++ internal/chat/chat_test.go | 84 ++++ internal/chat/handler.go | 193 +++++++++ internal/chat/message.go | 40 ++ internal/chat/message_test.go | 47 +++ internal/chat/repository.go | 30 ++ internal/chat/service.go | 101 +++++ internal/chat/service_test.go | 200 ++++++++++ internal/chat/store/inmem.go | 188 +++++++++ internal/chat/store/inmem_test.go | 262 ++++++++++++ internal/health/handler.go | 53 +++ internal/platform/apperr/apperr.go | 54 +++ internal/platform/apperr/apperr_test.go | 49 +++ internal/platform/config/config.go | 79 ++++ internal/platform/config/config_test.go | 71 ++++ internal/platform/httpx/decode.go | 70 ++++ internal/platform/httpx/engine.go | 49 +++ internal/platform/httpx/httpx_test.go | 309 ++++++++++++++ internal/platform/httpx/identity.go | 25 ++ internal/platform/httpx/middleware.go | 105 +++++ internal/platform/httpx/respond.go | 71 ++++ internal/platform/ident/ident.go | 46 +++ internal/platform/ident/ident_test.go | 72 ++++ internal/platform/pagination/cursor.go | 63 +++ internal/platform/pagination/cursor_test.go | 71 ++++ internal/platform/pagination/page.go | 41 ++ internal/platform/pagination/page_test.go | 60 +++ internal/server/server.go | 57 +++ internal/server/server_test.go | 90 +++++ internal/user/handler.go | 70 ++++ internal/user/repository.go | 17 + internal/user/service.go | 55 +++ internal/user/service_test.go | 85 ++++ internal/user/store/inmem.go | 63 +++ internal/user/store/inmem_test.go | 97 +++++ internal/user/user.go | 51 +++ internal/user/user_test.go | 56 +++ student-tests/lifecycle.sh | 62 +++ student-tests/wait-ready.sh | 17 + tools/concurrency/client.go | 67 ++++ tools/concurrency/main.go | 216 ++++++++++ tools/concurrency/main_test.go | 36 ++ 54 files changed, 4472 insertions(+) create mode 100644 .github/workflows/app.yml create mode 100644 Dockerfile create mode 100644 cmd/server/main.go create mode 100644 compose.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/app/app.go create mode 100644 internal/app/app_test.go create mode 100644 internal/auth/auth.go create mode 100644 internal/auth/auth_test.go create mode 100644 internal/chat/chat.go create mode 100644 internal/chat/chat_test.go create mode 100644 internal/chat/handler.go create mode 100644 internal/chat/message.go create mode 100644 internal/chat/message_test.go create mode 100644 internal/chat/repository.go create mode 100644 internal/chat/service.go create mode 100644 internal/chat/service_test.go create mode 100644 internal/chat/store/inmem.go create mode 100644 internal/chat/store/inmem_test.go create mode 100644 internal/health/handler.go create mode 100644 internal/platform/apperr/apperr.go create mode 100644 internal/platform/apperr/apperr_test.go create mode 100644 internal/platform/config/config.go create mode 100644 internal/platform/config/config_test.go create mode 100644 internal/platform/httpx/decode.go create mode 100644 internal/platform/httpx/engine.go create mode 100644 internal/platform/httpx/httpx_test.go create mode 100644 internal/platform/httpx/identity.go create mode 100644 internal/platform/httpx/middleware.go create mode 100644 internal/platform/httpx/respond.go create mode 100644 internal/platform/ident/ident.go create mode 100644 internal/platform/ident/ident_test.go create mode 100644 internal/platform/pagination/cursor.go create mode 100644 internal/platform/pagination/cursor_test.go create mode 100644 internal/platform/pagination/page.go create mode 100644 internal/platform/pagination/page_test.go create mode 100644 internal/server/server.go create mode 100644 internal/server/server_test.go create mode 100644 internal/user/handler.go create mode 100644 internal/user/repository.go create mode 100644 internal/user/service.go create mode 100644 internal/user/service_test.go create mode 100644 internal/user/store/inmem.go create mode 100644 internal/user/store/inmem_test.go create mode 100644 internal/user/user.go create mode 100644 internal/user/user_test.go create mode 100644 student-tests/lifecycle.sh create mode 100644 student-tests/wait-ready.sh create mode 100644 tools/concurrency/client.go create mode 100644 tools/concurrency/main.go create mode 100644 tools/concurrency/main_test.go diff --git a/.dockerignore b/.dockerignore index bc16e556..c872c535 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,4 @@ __pycache__ node_modules evidence .DS_Store +compose*.yaml diff --git a/.github/workflows/app.yml b/.github/workflows/app.yml new file mode 100644 index 00000000..dc5c3033 --- /dev/null +++ b/.github/workflows/app.yml @@ -0,0 +1,53 @@ +name: Application +on: [push, pull_request] +permissions: + contents: read +defaults: + run: + # Explicit bash turns on `-eo pipefail`, so a failing check behind `| tee` fails the job. + shell: bash +env: + BASE_URL: http://localhost:8080 + CONTAINER: mtusi-messenger-lab2 +jobs: + unit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '1.27.1' + - run: go vet ./... + - run: go test -race -count=1 ./... + e2e: + runs-on: ubuntu-latest + needs: unit + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '1.27.1' + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: docker build -t mtusi-messenger:lab2 . + - run: docker run -d --name "$CONTAINER" -p 127.0.0.1:8080:8080 --env-file .env.example mtusi-messenger:lab2 + - run: sh student-tests/wait-ready.sh + - run: mkdir -p evidence-ci + - name: Public smoke + run: make test PYTHON=python BASE_URL="$BASE_URL" | tee evidence-ci/smoke.txt + - name: Concurrency check + run: go run ./tools/concurrency --base-url "$BASE_URL" --output evidence-ci/concurrency.json + - name: SIGTERM and restart + run: sh student-tests/lifecycle.sh | tee evidence-ci/lifecycle.txt + - name: Container logs + if: always() + run: docker logs "$CONTAINER" > evidence-ci/container.log 2>&1 || true + - name: Cleanup + if: always() + run: docker rm -f "$CONTAINER" || true + - uses: actions/upload-artifact@v4 + if: always() + with: + name: evidence + path: evidence-ci diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..42d4357a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Build stage: compile a static binary with the exact toolchain pinned in go.mod. +FROM golang:1.27.1-alpine3.23@sha256:d9e2f2f07b10cc922da3e80e035c3058810b328d5aef82d2c63680967c5e2ec9 AS build +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download && go mod verify +COPY . . +RUN CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server + +# Runtime stage: minimal image, unprivileged user, the server is PID 1. +FROM alpine:3.23@sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40 +RUN adduser -D -H -u 10001 app +COPY --from=build /out/server /usr/local/bin/server +USER app +ENV APP_HOST=0.0.0.0 \ + APP_PORT=8080 \ + APP_ENV=lab +EXPOSE 8080 +HEALTHCHECK --interval=10s --timeout=2s --start-period=3s --retries=3 \ + CMD wget -qO- "http://127.0.0.1:${APP_PORT:-8080}/health/ready" >/dev/null || exit 1 +ENTRYPOINT ["/usr/local/bin/server"] diff --git a/Makefile b/Makefile index 9ecb223e..3cb2500e 100644 --- a/Makefile +++ b/Makefile @@ -24,3 +24,49 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +# --------------------------------------------------------------------------- +# Application targets (Go implementation). The targets above are the skeleton. +# --------------------------------------------------------------------------- +IMAGE ?= mtusi-messenger:lab2 +CONTAINER ?= mtusi-messenger-lab2 +PUBLISH ?= 127.0.0.1:8080 +EVIDENCE_DIR ?= evidence + +.PHONY: build vet unit up down docker-build docker-run docker-stop docker-logs wait-ready concurrency lifecycle + +build: + go build -o bin/server ./cmd/server + +vet: + go vet ./... + +unit: + go test -race -count=1 ./... + +up: + docker compose up -d --build --wait + +down: + docker compose down + +docker-build: + docker build -t "$(IMAGE)" . + +docker-run: + docker run -d --name "$(CONTAINER)" -p "$(PUBLISH):8080" --env-file .env "$(IMAGE)" + +docker-stop: + docker stop -t 10 "$(CONTAINER)" && docker rm "$(CONTAINER)" + +docker-logs: + docker logs "$(CONTAINER)" + +wait-ready: + BASE_URL="$(BASE_URL)" sh student-tests/wait-ready.sh + +concurrency: + go run ./tools/concurrency --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/concurrency.json" + +lifecycle: + BASE_URL="$(BASE_URL)" CONTAINER="$(CONTAINER)" sh student-tests/lifecycle.sh diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 00000000..ea200735 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,48 @@ +// Command server starts the in-memory messenger API. +package main + +import ( + "context" + "fmt" + "log/slog" + "net" + "os" + "os/signal" + "syscall" + + "messenger/internal/app" + "messenger/internal/platform/config" + "messenger/internal/server" +) + +func main() { + cfg, err := config.Load(os.LookupEnv) + if err != nil { + fmt.Fprintln(os.Stderr, "configuration error:", err) + os.Exit(2) + } + log := newLogger(cfg) + + ln, err := net.Listen("tcp", cfg.Addr()) + if err != nil { + log.Error("listen failed", "addr", cfg.Addr(), "error", err.Error()) + os.Exit(1) + } + + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) + defer stop() + + log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String()) + if err := server.Run(ctx, log, ln, app.New(log, cfg.MaxBodyBytes), cfg.ShutdownTimeout); err != nil { + log.Error("server stopped with error", "error", err.Error()) + os.Exit(1) + } +} + +func newLogger(cfg config.Config) *slog.Logger { + opts := &slog.HandlerOptions{Level: cfg.LogLevel} + if cfg.Env == "dev" { + return slog.New(slog.NewTextHandler(os.Stdout, opts)) + } + return slog.New(slog.NewJSONHandler(os.Stdout, opts)) +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..13cdcb58 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,12 @@ +services: + api: + build: . + image: mtusi-messenger:lab2 + # Fixed name so the Makefile targets and student-tests/lifecycle.sh address + # the same container whether it was started by compose or by docker run. + container_name: mtusi-messenger-lab2 + env_file: .env + # Loopback only; override with PUBLISH=127.0.0.1:18080 when 8080 is busy. + ports: + - "${PUBLISH:-127.0.0.1:8080}:8080" + stop_grace_period: 10s diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..00dce21a --- /dev/null +++ b/go.mod @@ -0,0 +1,40 @@ +module messenger + +go 1.27.1 + +require ( + github.com/gin-gonic/gin v1.12.0 + github.com/google/uuid v1.6.0 +) + +require ( + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/crypto v0.48.0 // indirect + golang.org/x/net v0.51.0 // indirect + golang.org/x/sys v0.41.0 // indirect + golang.org/x/text v0.34.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..0b62b839 --- /dev/null +++ b/go.sum @@ -0,0 +1,91 @@ +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= +go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= +golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= +golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= +golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= +golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 00000000..f2e10cbc --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,39 @@ +// Package app is the composition root: it builds every module with its +// in-memory stores and mounts them on one HTTP engine. Both the server +// binary and the integration tests start from here. +package app + +import ( + "log/slog" + + "github.com/gin-gonic/gin" + + "messenger/internal/auth" + "messenger/internal/chat" + chatstore "messenger/internal/chat/store" + "messenger/internal/health" + "messenger/internal/platform/httpx" + "messenger/internal/user" + userstore "messenger/internal/user/store" +) + +type App struct { + Engine *gin.Engine + Health *health.Handler +} + +func New(log *slog.Logger, maxBodyBytes int64) *App { + users := user.NewService(userstore.NewInMemUsers()) + chats := chat.NewService(chatstore.NewInMemChats(), chatstore.NewInMemMessages(), users) + + engine := httpx.NewEngine(log, maxBodyBytes) + probes := health.New() + probes.Register(engine) + + public := engine.Group("/api/v1") + protected := public.Group("", auth.RequireUser(users)) + user.NewHandler(users).Register(public, protected) + chat.NewHandler(chats).Register(protected) + + return &App{Engine: engine, Health: probes} +} diff --git a/internal/app/app_test.go b/internal/app/app_test.go new file mode 100644 index 00000000..b50f48be --- /dev/null +++ b/internal/app/app_test.go @@ -0,0 +1,421 @@ +package app_test + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "net/url" + "sort" + "strings" + "sync" + "testing" + + "messenger/internal/app" + "messenger/internal/chat" + "messenger/internal/platform/pagination" + "messenger/internal/user" +) + +// errorBody mirrors the contract error envelope. +type errorBody struct { + Error struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` + } `json:"error"` +} + +type response struct { + status int + requestID string + raw []byte +} + +// tester drives the assembled application in-process. +type tester struct { + t *testing.T + a *app.App +} + +func newTester(t *testing.T) *tester { + t.Helper() + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + return &tester{t: t, a: app.New(log, 64*1024)} +} + +// do sends one request. A string body is sent verbatim, anything else is +// JSON-encoded; both get a JSON content type. Every response is checked for +// X-Request-Id, and error bodies for the contract envelope. +func (ts *tester) do(method, path, actor string, body any, headers map[string]string) response { + ts.t.Helper() + req := httptest.NewRequest(method, path, encode(ts.t, body)) + if body != nil { + req.Header.Set("Content-Type", "application/json") + } + if actor != "" { + req.Header.Set("X-User-Id", actor) + } + for k, v := range headers { + req.Header.Set(k, v) + } + rec := httptest.NewRecorder() + ts.a.Engine.ServeHTTP(rec, req) + + res := response{status: rec.Code, requestID: rec.Header().Get("X-Request-Id"), raw: rec.Body.Bytes()} + if res.requestID == "" { + ts.t.Fatalf("%s %s: missing X-Request-Id", method, path) + } + if res.status >= 400 { + var eb errorBody + if err := json.Unmarshal(res.raw, &eb); err != nil { + ts.t.Fatalf("%s %s: error body is not the contract envelope: %v: %s", method, path, err, res.raw) + } + if eb.Error.RequestID != res.requestID || eb.Error.Code == "" || eb.Error.Message == "" { + ts.t.Fatalf("%s %s: bad error envelope: %s", method, path, res.raw) + } + } + return res +} + +func encode(t *testing.T, body any) io.Reader { + t.Helper() + switch b := body.(type) { + case nil: + return nil + case string: + return strings.NewReader(b) + default: + raw, err := json.Marshal(b) + if err != nil { + t.Fatal(err) + } + return bytes.NewReader(raw) + } +} + +// expect asserts the status and, for errors, the contract code. +func (ts *tester) expect(res response, status int, code string) { + ts.t.Helper() + if res.status != status { + ts.t.Fatalf("status = %d, want %d; body %s", res.status, status, res.raw) + } + if code != "" { + var eb errorBody + _ = json.Unmarshal(res.raw, &eb) + if eb.Error.Code != code { + ts.t.Fatalf("code = %q, want %q; body %s", eb.Error.Code, code, res.raw) + } + } +} + +// decode parses a success body into a typed DTO and rejects unknown fields, +// which enforces the additionalProperties=false rule of the contract. +func decode[T any](t *testing.T, res response) T { + t.Helper() + var out T + dec := json.NewDecoder(bytes.NewReader(res.raw)) + dec.DisallowUnknownFields() + if err := dec.Decode(&out); err != nil { + t.Fatalf("body does not match %T: %v: %s", out, err, res.raw) + } + return out +} + +func (ts *tester) user(username, display string) user.Response { + ts.t.Helper() + res := ts.do(http.MethodPost, "/api/v1/users", "", map[string]string{"username": username, "display_name": display}, nil) + ts.expect(res, http.StatusCreated, "") + return decode[user.Response](ts.t, res) +} + +func (ts *tester) chat(actor, title string, members ...string) chat.ChatResponse { + ts.t.Helper() + res := ts.do(http.MethodPost, "/api/v1/chats", actor, map[string]any{"title": title, "member_ids": members}, nil) + ts.expect(res, http.StatusCreated, "") + return decode[chat.ChatResponse](ts.t, res) +} + +func (ts *tester) message(actor, chatID, text string) chat.MessageResponse { + ts.t.Helper() + res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", actor, map[string]string{"text": text}, nil) + ts.expect(res, http.StatusCreated, "") + return decode[chat.MessageResponse](ts.t, res) +} + +func (ts *tester) chats(actor, query string) pagination.PageResponse[chat.ChatResponse] { + ts.t.Helper() + res := ts.do(http.MethodGet, "/api/v1/chats"+query, actor, nil, nil) + ts.expect(res, http.StatusOK, "") + return decode[pagination.PageResponse[chat.ChatResponse]](ts.t, res) +} + +func (ts *tester) history(actor, chatID, query string) pagination.PageResponse[chat.MessageResponse] { + ts.t.Helper() + res := ts.do(http.MethodGet, "/api/v1/chats/"+chatID+"/messages"+query, actor, nil, nil) + ts.expect(res, http.StatusOK, "") + return decode[pagination.PageResponse[chat.MessageResponse]](ts.t, res) +} + +func cursorQuery(limit int, cursor *string) string { + q := fmt.Sprintf("?limit=%d", limit) + if cursor != nil { + q += "&cursor=" + url.QueryEscape(*cursor) + } + return q +} + +func TestHealthEndpoints(t *testing.T) { + ts := newTester(t) + for _, path := range []string{"/health/live", "/health/ready"} { + res := ts.do(http.MethodGet, path, "", nil, nil) + ts.expect(res, http.StatusOK, "") + if strings.TrimSpace(string(res.raw)) != `{"status":"ok"}` { + t.Fatalf("%s body = %s", path, res.raw) + } + } + ts.a.Health.SetReady(false) + ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "") +} + +func TestRegisterUserShapeAndConflicts(t *testing.T) { + ts := newTester(t) + alice := ts.user("alice", "Алиса") + if alice.ID == "" || alice.Username != "alice" || alice.DisplayName != "Алиса" { + t.Fatalf("user = %+v", alice) + } + if !strings.HasSuffix(alice.CreatedAt, "Z") || len(alice.CreatedAt) != len("2026-09-01T12:00:00.000Z") { + t.Fatalf("created_at = %q", alice.CreatedAt) + } + post := func(body any) response { return ts.do(http.MethodPost, "/api/v1/users", "", body, nil) } + ts.expect(post(map[string]string{"username": "alice", "display_name": "Other"}), http.StatusConflict, "conflict") + ts.expect(post(map[string]string{"username": "Alice", "display_name": "x"}), http.StatusBadRequest, "invalid_request") + ts.expect(post(map[string]string{"username": "bob", "display_name": " "}), http.StatusBadRequest, "invalid_request") + ts.expect(post(map[string]any{"username": "bob", "display_name": "x", "password": "no"}), http.StatusBadRequest, "invalid_request") + ts.expect(post(`{"username":`), http.StatusBadRequest, "invalid_request") + ts.expect(post(map[string]any{"username": 5, "display_name": "x"}), http.StatusBadRequest, "invalid_request") +} + +func TestCurrentUserRequiresIdentity(t *testing.T) { + ts := newTester(t) + alice := ts.user("alice", "Алиса") + me := func(actor string) response { return ts.do(http.MethodGet, "/api/v1/users/me", actor, nil, nil) } + ts.expect(me(""), http.StatusUnauthorized, "unauthorized") + ts.expect(me("garbage"), http.StatusUnauthorized, "unauthorized") + ts.expect(me("11111111-1111-4111-8111-111111111111"), http.StatusUnauthorized, "unauthorized") + res := me(alice.ID) + ts.expect(res, http.StatusOK, "") + if got := decode[user.Response](t, res); got != alice { + t.Fatalf("me = %+v, want %+v", got, alice) + } +} + +func TestChatCreationRules(t *testing.T) { + ts := newTester(t) + alice, bob := ts.user("alice", "A"), ts.user("bob", "B") + created := ts.chat(alice.ID, "Проект по ВНП", alice.ID, bob.ID) + if created.Title != "Проект по ВНП" || len(created.MemberIDs) != 2 || created.MemberIDs[0] != alice.ID || created.MemberIDs[1] != bob.ID { + t.Fatalf("chat = %+v", created) + } + unknown := "22222222-2222-4222-8222-222222222222" + invalid := map[string]any{ + "creator missing": map[string]any{"title": "t", "member_ids": []string{bob.ID}}, + "duplicate": map[string]any{"title": "t", "member_ids": []string{alice.ID, alice.ID}}, + "unknown member": map[string]any{"title": "t", "member_ids": []string{alice.ID, unknown}}, + "malformed uuid": map[string]any{"title": "t", "member_ids": []string{alice.ID, "not-a-uuid"}}, + "empty members": map[string]any{"title": "t", "member_ids": []string{}}, + "blank title": map[string]any{"title": "", "member_ids": []string{alice.ID}}, + "long title": map[string]any{"title": strings.Repeat("я", 101), "member_ids": []string{alice.ID}}, + "missing members": map[string]any{"title": "t"}, + "extra field": map[string]any{"title": "t", "member_ids": []string{alice.ID}, "owner": alice.ID}, + } + for name, body := range invalid { + res := ts.do(http.MethodPost, "/api/v1/chats", alice.ID, body, nil) + if res.status != http.StatusBadRequest { + t.Errorf("%s: status = %d, want 400; body %s", name, res.status, res.raw) + } + } + ts.expect(ts.do(http.MethodPost, "/api/v1/chats", "", map[string]any{"title": "t", "member_ids": []string{alice.ID}}, nil), http.StatusUnauthorized, "unauthorized") +} + +func TestChatVisibilityAndListing(t *testing.T) { + ts := newTester(t) + alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E") + created := ts.chat(alice.ID, "Контрактный тест", alice.ID, bob.ID) + path := "/api/v1/chats/" + created.ID + + res := ts.do(http.MethodGet, path, bob.ID, nil, nil) + ts.expect(res, http.StatusOK, "") + if got := decode[chat.ChatResponse](t, res); got.ID != created.ID || len(got.MemberIDs) != 2 { + t.Fatalf("GET chat = %+v", got) + } + ts.expect(ts.do(http.MethodGet, path, eve.ID, nil, nil), http.StatusNotFound, "not_found") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/33333333-3333-4333-8333-333333333333", alice.ID, nil, nil), http.StatusNotFound, "not_found") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/not-a-uuid", alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") + ts.expect(ts.do(http.MethodGet, path, "", nil, nil), http.StatusUnauthorized, "unauthorized") + + listed := ts.chats(bob.ID, "?limit=100") + if len(listed.Items) != 1 || listed.Items[0].ID != created.ID || listed.NextCursor != nil { + t.Fatalf("bob list = %+v", listed) + } + hidden := ts.do(http.MethodGet, "/api/v1/chats?limit=100", eve.ID, nil, nil) + ts.expect(hidden, http.StatusOK, "") + if !strings.Contains(string(hidden.raw), `"items":[]`) || !strings.Contains(string(hidden.raw), `"next_cursor":null`) { + t.Fatalf("empty list must serialize items as [] and next_cursor as null: %s", hidden.raw) + } +} + +func TestChatListPagination(t *testing.T) { + ts := newTester(t) + alice := ts.user("alice", "A") + var expected []string + for i := 0; i < 5; i++ { + expected = append(expected, ts.chat(alice.ID, fmt.Sprintf("chat %d", i), alice.ID).ID) + } + var walked []string + var cursor *string + for pages := 0; pages < 10; pages++ { + page := ts.chats(alice.ID, cursorQuery(2, cursor)) + for _, c := range page.Items { + walked = append(walked, c.ID) + } + if page.NextCursor == nil { + break + } + cursor = page.NextCursor + } + if strings.Join(walked, ",") != strings.Join(expected, ",") { + t.Fatalf("walked %v, want %v", walked, expected) + } + + bob := ts.user("bob", "B") + foreign := ts.chats(alice.ID, "?limit=1").NextCursor + ts.expect(ts.do(http.MethodGet, "/api/v1/chats"+cursorQuery(50, foreign), bob.ID, nil, nil), http.StatusBadRequest, "invalid_request") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats?cursor=", alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats?limit=abc", alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") +} + +func TestMessagesEndToEnd(t *testing.T) { + ts := newTester(t) + alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E") + room := ts.chat(alice.ID, "Контрактный тест", alice.ID, bob.ID) + path := "/api/v1/chats/" + room.ID + "/messages" + + first := ts.message(alice.ID, room.ID, "Привет, мир 👋") + second := ts.message(bob.ID, room.ID, "Сообщение Боба") + if first.SenderID != alice.ID || second.SenderID != bob.ID { + t.Fatalf("sender must come from X-User-Id: %+v %+v", first, second) + } + if first.ChatID != room.ID || first.Text != "Привет, мир 👋" { + t.Fatalf("message = %+v", first) + } + + page1 := ts.history(bob.ID, room.ID, "?limit=1") + if len(page1.Items) != 1 || page1.NextCursor == nil || page1.Items[0] != first { + t.Fatalf("page1 = %+v", page1) + } + page2 := ts.history(alice.ID, room.ID, cursorQuery(1, page1.NextCursor)) + if len(page2.Items) != 1 || page2.NextCursor != nil || page2.Items[0] != second { + t.Fatalf("page2 = %+v", page2) + } + + post := func(actor string, body any) response { return ts.do(http.MethodPost, path, actor, body, nil) } + ts.expect(post(alice.ID, map[string]string{"text": ""}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.ID, map[string]string{"text": " "}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.ID, map[string]string{"text": strings.Repeat("я", 4001)}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.ID, map[string]string{"text": "x", "sender_id": bob.ID}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.ID, `{"text":`), http.StatusBadRequest, "invalid_request") + ts.expect(post(eve.ID, map[string]string{"text": "чужое"}), http.StatusNotFound, "not_found") + ts.expect(post(eve.ID, map[string]string{"text": ""}), http.StatusNotFound, "not_found") + ts.expect(post("", map[string]string{"text": "x"}), http.StatusUnauthorized, "unauthorized") + ts.expect(post(alice.ID, map[string]string{"text": strings.Repeat("я", 4000)}), http.StatusCreated, "") + + get := func(actor, query string) response { return ts.do(http.MethodGet, path+query, actor, nil, nil) } + ts.expect(get(eve.ID, ""), http.StatusNotFound, "not_found") + ts.expect(get(alice.ID, "?limit=0"), http.StatusBadRequest, "invalid_request") + ts.expect(get(alice.ID, "?limit=101"), http.StatusBadRequest, "invalid_request") + ts.expect(get(alice.ID, "?cursor=garbage"), http.StatusBadRequest, "invalid_request") + + other := ts.chat(alice.ID, "другой", alice.ID) + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+other.ID+"/messages"+cursorQuery(50, page1.NextCursor), alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") +} + +func TestMessageHistoryWalkAcrossPages(t *testing.T) { + ts := newTester(t) + alice := ts.user("alice", "A") + room := ts.chat(alice.ID, "t", alice.ID) + var expected []string + for i := 0; i < 23; i++ { + expected = append(expected, ts.message(alice.ID, room.ID, fmt.Sprintf("m%d", i)).ID) + } + var walked []string + var cursor *string + for { + page := ts.history(alice.ID, room.ID, cursorQuery(5, cursor)) + for _, m := range page.Items { + walked = append(walked, m.ID) + } + if page.NextCursor == nil { + break + } + cursor = page.NextCursor + } + if strings.Join(walked, ",") != strings.Join(expected, ",") { + t.Fatalf("walk mismatch:\n%v\n%v", walked, expected) + } +} + +func TestConcurrentSendsProduceExactlyUniqueMessages(t *testing.T) { + ts := newTester(t) + const clients, perClient = 10, 10 + members := make([]string, 0, clients) + for i := 0; i < clients; i++ { + members = append(members, ts.user(fmt.Sprintf("client_%d", i), "C").ID) + } + room := ts.chat(members[0], "нагрузка", members...) + + var wg sync.WaitGroup + for n, actor := range members { + wg.Add(1) + go func(n int, actor string) { + defer wg.Done() + for i := 0; i < perClient; i++ { + res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", actor, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, nil) + if res.status != http.StatusCreated { + t.Errorf("client %d: status %d body %s", n, res.status, res.raw) + } + } + }(n, actor) + } + wg.Wait() + + page := ts.history(members[0], room.ID, "?limit=100") + if len(page.Items) != clients*perClient || page.NextCursor != nil { + t.Fatalf("got %d messages, next=%v; want %d and null", len(page.Items), page.NextCursor, clients*perClient) + } + ids := make([]string, 0, len(page.Items)) + unique := make(map[string]struct{}, len(page.Items)) + for _, m := range page.Items { + ids = append(ids, m.ID) + unique[m.ID] = struct{}{} + } + if !sort.StringsAreSorted(ids) { + t.Fatal("history is not ordered by id") + } + if len(unique) != clients*perClient { + t.Fatalf("%d unique ids, want %d", len(unique), clients*perClient) + } +} + +func TestUnknownRouteAndMethodAreContractErrors(t *testing.T) { + ts := newTester(t) + ts.expect(ts.do(http.MethodGet, "/api/v1/nothing", "", nil, nil), http.StatusNotFound, "not_found") + ts.expect(ts.do(http.MethodDelete, "/api/v1/users", "", nil, nil), http.StatusMethodNotAllowed, "invalid_request") + ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", "{}", map[string]string{"Content-Type": "text/plain"}), http.StatusUnsupportedMediaType, "unsupported_media_type") + big := map[string]string{"username": "big", "display_name": strings.Repeat("x", 70*1024)} + ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", big, nil), http.StatusRequestEntityTooLarge, "payload_too_large") +} diff --git a/internal/auth/auth.go b/internal/auth/auth.go new file mode 100644 index 00000000..e9d6fb59 --- /dev/null +++ b/internal/auth/auth.go @@ -0,0 +1,65 @@ +// Package auth identifies the caller. In this lab the identity is the +// X-User-Id header naming a registered user: identification, not +// authentication, by design of the assignment. +package auth + +import ( + "context" + "errors" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/httpx" + "messenger/internal/platform/ident" +) + +type UserLookup interface { + Exists(ctx context.Context, id uuid.UUID) (bool, error) +} + +// RequireUser answers 401 unless X-User-Id names a registered user, and +// records that user for the handlers behind it. +func RequireUser(users UserLookup) gin.HandlerFunc { + return func(c *gin.Context) { + raw := c.GetHeader("X-User-Id") + if raw == "" { + httpx.Error(c, apperr.Unauthorized("X-User-Id header is required")) + return + } + id, err := ident.Parse(raw) + if err != nil { + httpx.Error(c, apperr.Unauthorized("X-User-Id must be the UUID of a registered user")) + return + } + ok, err := users.Exists(c.Request.Context(), id) + if err != nil { + httpx.Error(c, err) + return + } + if !ok { + httpx.Error(c, apperr.Unauthorized("X-User-Id must be the UUID of a registered user")) + return + } + httpx.SetUserID(c, id) + c.Next() + } +} + +// UserHandler is a handler that needs the identified caller. +type UserHandler func(c *gin.Context, actor uuid.UUID) + +// WithUser adapts a UserHandler to gin. Reaching it without RequireUser in +// front is a wiring mistake, so it is reported as an internal error rather +// than blamed on the client with a 401. +func WithUser(h UserHandler) gin.HandlerFunc { + return func(c *gin.Context) { + actor, ok := httpx.UserID(c) + if !ok { + httpx.Error(c, errors.New("route is missing the RequireUser middleware")) + return + } + h(c, actor) + } +} diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go new file mode 100644 index 00000000..aedfb6da --- /dev/null +++ b/internal/auth/auth_test.go @@ -0,0 +1,91 @@ +package auth_test + +import ( + "context" + "encoding/json" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "messenger/internal/auth" + "messenger/internal/platform/httpx" + "messenger/internal/platform/ident" +) + +type lookup map[uuid.UUID]struct{} + +func (l lookup) Exists(_ context.Context, id uuid.UUID) (bool, error) { + _, ok := l[id] + return ok, nil +} + +func echoActor(c *gin.Context, actor uuid.UUID) { + httpx.JSON(c, http.StatusOK, map[string]string{"id": actor.String()}) +} + +func newEngine(users lookup, guarded bool) *gin.Engine { + e := httpx.NewEngine(slog.New(slog.NewTextHandler(io.Discard, nil)), 1024) + if guarded { + e.GET("/me", auth.RequireUser(users), auth.WithUser(echoActor)) + } else { + e.GET("/me", auth.WithUser(echoActor)) + } + return e +} + +func call(e *gin.Engine, header string) (*httptest.ResponseRecorder, string) { + req := httptest.NewRequest(http.MethodGet, "/me", nil) + if header != "" { + req.Header.Set("X-User-Id", header) + } + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + var body struct { + ID string `json:"id"` + Error struct { + Code string `json:"code"` + } `json:"error"` + } + _ = json.Unmarshal(rec.Body.Bytes(), &body) + if body.ID != "" { + return rec, body.ID + } + return rec, body.Error.Code +} + +func TestKnownUserReachesHandlerWithItsID(t *testing.T) { + alice := ident.New() + rec, got := call(newEngine(lookup{alice: {}}, true), alice.String()) + if rec.Code != http.StatusOK || got != alice.String() { + t.Fatalf("status %d, handler saw %q, want %s", rec.Code, got, alice) + } +} + +func TestMiddlewareRejectsMissingMalformedAndUnknownWith401(t *testing.T) { + alice := ident.New() + e := newEngine(lookup{alice: {}}, true) + for name, header := range map[string]string{ + "missing": "", + "malformed": "not-a-uuid", + "braces": "{" + alice.String() + "}", + "unknown": ident.New().String(), + } { + rec, code := call(e, header) + if rec.Code != http.StatusUnauthorized || code != "unauthorized" { + t.Errorf("%s: status = %d code = %q, want 401 unauthorized", name, rec.Code, code) + } + } +} + +func TestWithUserWithoutMiddlewareIsAWiringError(t *testing.T) { + alice := ident.New() + rec, code := call(newEngine(lookup{alice: {}}, false), alice.String()) + if rec.Code != http.StatusInternalServerError || code != "internal_error" { + t.Fatalf("status = %d code = %q, want 500 internal_error", rec.Code, code) + } +} diff --git a/internal/chat/chat.go b/internal/chat/chat.go new file mode 100644 index 00000000..6bde7698 --- /dev/null +++ b/internal/chat/chat.go @@ -0,0 +1,73 @@ +// Package chat is the chats module. Entities mirror future table rows and +// carry no behaviour; membership is a query rather than a field, and +// ChatWithMembers is the projection the API renders. Both entities derive +// CreatedAt from their UUIDv7 id, so the history order (created_at, id) is +// plain id order. +package chat + +import ( + "fmt" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" +) + +const ( + MaxTitleCodePoints = 100 + MaxMembers = 100 +) + +// ErrNotFound covers both a missing chat and a chat the caller is not a +// member of, so outsiders cannot probe for existence. +var ErrNotFound = apperr.NotFound("chat not found") + +type Chat struct { + ID uuid.UUID + Title string + CreatedAt time.Time +} + +type ChatWithMembers struct { + Chat + MemberIDs []uuid.UUID +} + +func NewChat(id uuid.UUID, title string) (Chat, error) { + if strings.TrimSpace(title) == "" { + return Chat{}, apperr.Invalid("title must not be blank") + } + if utf8.RuneCountInString(title) > MaxTitleCodePoints { + return Chat{}, apperr.Invalid(fmt.Sprintf("title must be at most %d characters", MaxTitleCodePoints)) + } + return Chat{ID: id, Title: title, CreatedAt: ident.TimeOf(id)}, nil +} + +// ValidateMembers checks the member list of a new chat and returns its own +// copy in the original order. +func ValidateMembers(memberIDs []uuid.UUID, creator uuid.UUID) ([]uuid.UUID, error) { + if len(memberIDs) == 0 { + return nil, apperr.Invalid("member_ids must not be empty") + } + if len(memberIDs) > MaxMembers { + return nil, apperr.Invalid(fmt.Sprintf("member_ids must have at most %d entries", MaxMembers)) + } + seen := make(map[uuid.UUID]struct{}, len(memberIDs)) + for _, m := range memberIDs { + if m == uuid.Nil { + return nil, apperr.Invalid("member_ids must not contain the nil uuid") + } + if _, dup := seen[m]; dup { + return nil, apperr.Invalid("member_ids must be unique") + } + seen[m] = struct{}{} + } + if _, ok := seen[creator]; !ok { + return nil, apperr.Invalid("member_ids must include the creator") + } + return append([]uuid.UUID(nil), memberIDs...), nil +} diff --git a/internal/chat/chat_test.go b/internal/chat/chat_test.go new file mode 100644 index 00000000..ebfd5a85 --- /dev/null +++ b/internal/chat/chat_test.go @@ -0,0 +1,84 @@ +package chat_test + +import ( + "strings" + "testing" + + "github.com/google/uuid" + + "messenger/internal/chat" + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" +) + +func TestNewChatDerivesCreatedAtFromID(t *testing.T) { + id := ident.New() + c, err := chat.NewChat(id, "Проект") + if err != nil { + t.Fatalf("NewChat error: %v", err) + } + if c.ID != id || c.Title != "Проект" || !c.CreatedAt.Equal(ident.TimeOf(id)) { + t.Fatalf("unexpected chat %+v", c) + } +} + +func TestNewChatValidatesTitle(t *testing.T) { + for _, title := range []string{"", " ", strings.Repeat("я", 101)} { + _, err := chat.NewChat(ident.New(), title) + if apperr.KindOf(err) != apperr.KindInvalid { + t.Errorf("title %q must be rejected as invalid, got %v", title, err) + } + } + if _, err := chat.NewChat(ident.New(), strings.Repeat("я", 100)); err != nil { + t.Errorf("100 code points must be accepted: %v", err) + } +} + +func TestValidateMembersCopiesAndKeepsOrder(t *testing.T) { + alice, bob := ident.New(), ident.New() + input := []uuid.UUID{bob, alice} + members, err := chat.ValidateMembers(input, alice) + if err != nil { + t.Fatalf("ValidateMembers error: %v", err) + } + if len(members) != 2 || members[0] != bob || members[1] != alice { + t.Fatalf("order must be preserved, got %v", members) + } + input[0] = ident.New() + if members[0] != bob { + t.Fatal("result must not alias the input slice") + } +} + +func TestValidateMembersRejectsInvalidLists(t *testing.T) { + alice, bob := ident.New(), ident.New() + cases := map[string][]uuid.UUID{ + "empty": {}, + "nil": nil, + "missing creator": {bob}, + "duplicate": {alice, bob, bob}, + "duplicate self": {alice, alice}, + "too many": append([]uuid.UUID{alice}, many(100)...), + "nil uuid member": {alice, uuid.Nil}, + } + for name, members := range cases { + _, err := chat.ValidateMembers(members, alice) + if apperr.KindOf(err) != apperr.KindInvalid { + t.Errorf("%s: want invalid, got %v", name, err) + } + if err != nil && !strings.Contains(err.Error(), "member_ids") { + t.Errorf("%s: message must name the field: %v", name, err) + } + } + if _, err := chat.ValidateMembers(append([]uuid.UUID{alice}, many(99)...), alice); err != nil { + t.Errorf("100 members must be accepted: %v", err) + } +} + +func many(n int) []uuid.UUID { + out := make([]uuid.UUID, n) + for i := range out { + out[i] = ident.New() + } + return out +} diff --git a/internal/chat/handler.go b/internal/chat/handler.go new file mode 100644 index 00000000..b18504c9 --- /dev/null +++ b/internal/chat/handler.go @@ -0,0 +1,193 @@ +package chat + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "messenger/internal/auth" + "messenger/internal/platform/apperr" + "messenger/internal/platform/httpx" + "messenger/internal/platform/ident" + "messenger/internal/platform/pagination" +) + +type Handler struct { + chats *Service +} + +func NewHandler(chats *Service) *Handler { + return &Handler{chats: chats} +} + +// Register mounts the chat endpoints on a router guarded by RequireUser. +func (h *Handler) Register(protected gin.IRouter) { + protected.POST("/chats", auth.WithUser(h.create)) + protected.GET("/chats", auth.WithUser(h.list)) + protected.GET("/chats/:chat_id", auth.WithUser(h.get)) + protected.POST("/chats/:chat_id/messages", auth.WithUser(h.send)) + protected.GET("/chats/:chat_id/messages", auth.WithUser(h.history)) +} + +type createChatRequest struct { + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` +} + +type createMessageRequest struct { + Text string `json:"text"` +} + +type ChatResponse struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt string `json:"created_at"` +} + +type MessageResponse struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt string `json:"created_at"` +} + +func toChatResponse(c ChatWithMembers) ChatResponse { + members := make([]string, 0, len(c.MemberIDs)) + for _, m := range c.MemberIDs { + members = append(members, m.String()) + } + return ChatResponse{ + ID: c.ID.String(), + Title: c.Title, + MemberIDs: members, + CreatedAt: httpx.Timestamp(c.CreatedAt), + } +} + +func toMessageResponse(m Message) MessageResponse { + return MessageResponse{ + ID: m.ID.String(), + ChatID: m.ChatID.String(), + SenderID: m.SenderID.String(), + Text: m.Text, + CreatedAt: httpx.Timestamp(m.CreatedAt), + } +} + +func (h *Handler) create(c *gin.Context, actor uuid.UUID) { + var req createChatRequest + if err := httpx.DecodeJSON(c, &req); err != nil { + httpx.Error(c, err) + return + } + members := make([]uuid.UUID, 0, len(req.MemberIDs)) + for _, raw := range req.MemberIDs { + id, err := ident.Parse(raw) + if err != nil { + httpx.Error(c, apperr.Invalid("member_ids must contain valid UUIDs")) + return + } + members = append(members, id) + } + chat, err := h.chats.Create(c.Request.Context(), actor, req.Title, members) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusCreated, toChatResponse(chat)) +} + +func (h *Handler) list(c *gin.Context, actor uuid.UUID) { + limit, after, err := listing(c, actor) + if err != nil { + httpx.Error(c, err) + return + } + page, err := h.chats.List(c.Request.Context(), actor, after, limit) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusOK, pagination.ToResponse(page, actor, toChatResponse)) +} + +func (h *Handler) get(c *gin.Context, actor uuid.UUID) { + chatID, err := chatParam(c) + if err != nil { + httpx.Error(c, err) + return + } + chat, err := h.chats.Get(c.Request.Context(), actor, chatID) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusOK, toChatResponse(chat)) +} + +func (h *Handler) send(c *gin.Context, actor uuid.UUID) { + chatID, err := chatParam(c) + if err != nil { + httpx.Error(c, err) + return + } + var req createMessageRequest + if err := httpx.DecodeJSON(c, &req); err != nil { + httpx.Error(c, err) + return + } + message, err := h.chats.Send(c.Request.Context(), actor, chatID, req.Text) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusCreated, toMessageResponse(message)) +} + +func (h *Handler) history(c *gin.Context, actor uuid.UUID) { + chatID, err := chatParam(c) + if err != nil { + httpx.Error(c, err) + return + } + limit, after, err := listing(c, chatID) + if err != nil { + httpx.Error(c, err) + return + } + page, err := h.chats.History(c.Request.Context(), actor, chatID, after, limit) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusOK, pagination.ToResponse(page, chatID, toMessageResponse)) +} + +func chatParam(c *gin.Context) (uuid.UUID, error) { + id, err := ident.Parse(c.Param("chat_id")) + if err != nil { + return uuid.Nil, apperr.Invalid("chat_id must be a UUID") + } + return id, nil +} + +// listing parses limit and cursor; the cursor must belong to scope, and a +// present but empty cursor is rejected like a malformed one. +func listing(c *gin.Context, scope uuid.UUID) (int, *uuid.UUID, error) { + limit, err := pagination.ParseLimit(c.Query("limit")) + if err != nil { + return 0, nil, apperr.Wrap(apperr.KindInvalid, err.Error(), err) + } + token, present := c.GetQuery("cursor") + if !present { + return limit, nil, nil + } + last, err := pagination.DecodeCursor(token, scope) + if err != nil { + return 0, nil, apperr.Wrap(apperr.KindInvalid, "cursor is invalid for this resource", err) + } + return limit, &last, nil +} diff --git a/internal/chat/message.go b/internal/chat/message.go new file mode 100644 index 00000000..a825c2bc --- /dev/null +++ b/internal/chat/message.go @@ -0,0 +1,40 @@ +package chat + +import ( + "fmt" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" +) + +const MaxTextCodePoints = 4000 + +type Message struct { + ID uuid.UUID + ChatID uuid.UUID + SenderID uuid.UUID + Text string + CreatedAt time.Time +} + +// NewMessage keeps the text verbatim; only blank or oversized text is rejected. +func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) { + if strings.TrimSpace(text) == "" { + return Message{}, apperr.Invalid("text must not be blank") + } + if utf8.RuneCountInString(text) > MaxTextCodePoints { + return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints)) + } + return Message{ + ID: id, + ChatID: chatID, + SenderID: senderID, + Text: text, + CreatedAt: ident.TimeOf(id), + }, nil +} diff --git a/internal/chat/message_test.go b/internal/chat/message_test.go new file mode 100644 index 00000000..714b3dad --- /dev/null +++ b/internal/chat/message_test.go @@ -0,0 +1,47 @@ +package chat_test + +import ( + "strings" + "testing" + + "messenger/internal/chat" + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" +) + +func TestNewMessageDerivesCreatedAt(t *testing.T) { + id, chatID, alice := ident.New(), ident.New(), ident.New() + m, err := chat.NewMessage(id, chatID, alice, "Привет, мир 👋") + if err != nil { + t.Fatalf("NewMessage error: %v", err) + } + if m.ID != id || m.ChatID != chatID || m.SenderID != alice || m.Text != "Привет, мир 👋" { + t.Fatalf("unexpected message %+v", m) + } + if !m.CreatedAt.Equal(ident.TimeOf(id)) { + t.Fatalf("CreatedAt = %v, want %v", m.CreatedAt, ident.TimeOf(id)) + } +} + +func TestNewMessageValidatesTextByCodePoints(t *testing.T) { + chatID, alice := ident.New(), ident.New() + if _, err := chat.NewMessage(ident.New(), chatID, alice, strings.Repeat("я", 4000)); err != nil { + t.Errorf("4000 code points must be accepted: %v", err) + } + for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001)} { + _, err := chat.NewMessage(ident.New(), chatID, alice, text) + if apperr.KindOf(err) != apperr.KindInvalid { + t.Errorf("text %q must be rejected as invalid, got %v", text, err) + } + } +} + +func TestNewMessageKeepsSurroundingWhitespace(t *testing.T) { + m, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), " hi ") + if err != nil { + t.Fatal(err) + } + if m.Text != " hi " { + t.Fatalf("text must be stored verbatim, got %q", m.Text) + } +} diff --git a/internal/chat/repository.go b/internal/chat/repository.go new file mode 100644 index 00000000..fb785932 --- /dev/null +++ b/internal/chat/repository.go @@ -0,0 +1,30 @@ +package chat + +import ( + "context" + + "github.com/google/uuid" +) + +// ChatRepository stores chats and their membership. Membership is answered +// by queries; the projection with members is assembled by the store in one +// query, never by loading a member list into the entity. +type ChatRepository interface { + // Create stores the chat row and its members atomically. + Create(ctx context.Context, chat Chat, memberIDs []uuid.UUID) error + // GetForMember returns the chat with members, or ErrNotFound when the + // chat does not exist or the user is not a member. + GetForMember(ctx context.Context, chatID, userID uuid.UUID) (ChatWithMembers, error) + IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) + // ListByMember returns up to limit chats of the user ordered by + // (created_at, id), strictly after the chat identified by after when set. + ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]ChatWithMembers, error) +} + +type MessageRepository interface { + Append(ctx context.Context, message Message) error + // ListByChat returns up to limit messages ordered by (created_at, id), + // strictly after the message identified by after when set. An unknown + // chat yields an empty history. + ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]Message, error) +} diff --git a/internal/chat/service.go b/internal/chat/service.go new file mode 100644 index 00000000..97e09248 --- /dev/null +++ b/internal/chat/service.go @@ -0,0 +1,101 @@ +package chat + +import ( + "context" + "fmt" + + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" + "messenger/internal/platform/pagination" +) + +// UserLookup is what this module needs from the users module: one batched +// existence check. It is declared here, on the consumer side, so this +// package never imports the users package and tests substitute a map. +type UserLookup interface { + ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) +} + +type Service struct { + chats ChatRepository + messages MessageRepository + users UserLookup +} + +func NewService(chats ChatRepository, messages MessageRepository, users UserLookup) *Service { + return &Service{chats: chats, messages: messages, users: users} +} + +func (s *Service) Create(ctx context.Context, creator uuid.UUID, title string, memberIDs []uuid.UUID) (ChatWithMembers, error) { + chat, err := NewChat(ident.New(), title) + if err != nil { + return ChatWithMembers{}, err + } + members, err := ValidateMembers(memberIDs, creator) + if err != nil { + return ChatWithMembers{}, err + } + found, err := s.users.ExistingIDs(ctx, members) + if err != nil { + return ChatWithMembers{}, fmt.Errorf("check members: %w", err) + } + if len(found) != len(members) { + return ChatWithMembers{}, apperr.Invalid("member_ids must reference registered users") + } + if err := s.chats.Create(ctx, chat, members); err != nil { + return ChatWithMembers{}, err + } + return ChatWithMembers{Chat: chat, MemberIDs: members}, nil +} + +func (s *Service) Get(ctx context.Context, actor, chatID uuid.UUID) (ChatWithMembers, error) { + return s.chats.GetForMember(ctx, chatID, actor) +} + +func (s *Service) List(ctx context.Context, actor uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[ChatWithMembers], error) { + items, err := s.chats.ListByMember(ctx, actor, after, limit+1) + if err != nil { + return pagination.Page[ChatWithMembers]{}, err + } + return pagination.Cut(items, limit, func(c ChatWithMembers) uuid.UUID { return c.ID }), nil +} + +func (s *Service) Send(ctx context.Context, actor, chatID uuid.UUID, text string) (Message, error) { + if err := s.requireMember(ctx, actor, chatID); err != nil { + return Message{}, err + } + message, err := NewMessage(ident.New(), chatID, actor, text) + if err != nil { + return Message{}, err + } + if err := s.messages.Append(ctx, message); err != nil { + return Message{}, err + } + return message, nil +} + +func (s *Service) History(ctx context.Context, actor, chatID uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[Message], error) { + if err := s.requireMember(ctx, actor, chatID); err != nil { + return pagination.Page[Message]{}, err + } + items, err := s.messages.ListByChat(ctx, chatID, after, limit+1) + if err != nil { + return pagination.Page[Message]{}, err + } + return pagination.Cut(items, limit, func(m Message) uuid.UUID { return m.ID }), nil +} + +// requireMember runs before any field validation, so an outsider gets the +// same answer for a foreign chat as for a missing one. +func (s *Service) requireMember(ctx context.Context, actor, chatID uuid.UUID) error { + ok, err := s.chats.IsMember(ctx, chatID, actor) + if err != nil { + return fmt.Errorf("check membership: %w", err) + } + if !ok { + return ErrNotFound + } + return nil +} diff --git a/internal/chat/service_test.go b/internal/chat/service_test.go new file mode 100644 index 00000000..ad6dde18 --- /dev/null +++ b/internal/chat/service_test.go @@ -0,0 +1,200 @@ +package chat_test + +import ( + "context" + "errors" + "strings" + "sync/atomic" + "testing" + + "github.com/google/uuid" + + "messenger/internal/chat" + "messenger/internal/chat/store" + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" +) + +// knownUsers doubles chat.UserLookup and counts calls to prove that member +// checks are batched. +type knownUsers struct { + ids map[uuid.UUID]struct{} + calls atomic.Int32 +} + +func (k *knownUsers) ExistingIDs(_ context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { + k.calls.Add(1) + out := make([]uuid.UUID, 0, len(ids)) + for _, id := range ids { + if _, ok := k.ids[id]; ok { + out = append(out, id) + } + } + return out, nil +} + +type fixture struct { + svc *chat.Service + users *knownUsers + alice, bob uuid.UUID + eve uuid.UUID + ctx context.Context +} + +func newFixture() fixture { + alice, bob, eve := ident.New(), ident.New(), ident.New() + users := &knownUsers{ids: map[uuid.UUID]struct{}{alice: {}, bob: {}, eve: {}}} + return fixture{ + svc: chat.NewService(store.NewInMemChats(), store.NewInMemMessages(), users), + users: users, + alice: alice, + bob: bob, + eve: eve, + ctx: context.Background(), + } +} + +func TestCreateChatChecksMembersInOneCall(t *testing.T) { + f := newFixture() + c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.alice, f.bob, f.eve}) + if err != nil { + t.Fatalf("Create error: %v", err) + } + if c.Title != "Проект" || len(c.MemberIDs) != 3 { + t.Fatalf("unexpected chat %+v", c) + } + if got := f.users.calls.Load(); got != 1 { + t.Fatalf("member existence must be checked with one batch call, got %d", got) + } + got, err := f.svc.Get(f.ctx, f.bob, c.ID) + if err != nil || got.ID != c.ID || len(got.MemberIDs) != 3 { + t.Fatalf("member Get = %+v, %v", got, err) + } +} + +func TestCreateChatRejectsUnknownMember(t *testing.T) { + f := newFixture() + _, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, ident.New()}) + if apperr.KindOf(err) != apperr.KindInvalid { + t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err) + } + if !strings.Contains(err.Error(), "member_ids") { + t.Fatalf("message must name the field: %v", err) + } +} + +func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) { + f := newFixture() + cases := map[string]struct { + title string + members []uuid.UUID + }{ + "blank title": {" ", []uuid.UUID{f.alice}}, + "missing creator": {"t", []uuid.UUID{f.bob}}, + "duplicate": {"t", []uuid.UUID{f.alice, f.alice}}, + } + for name, tc := range cases { + _, err := f.svc.Create(f.ctx, f.alice, tc.title, tc.members) + if apperr.KindOf(err) != apperr.KindInvalid { + t.Errorf("%s: kind = %v, want KindInvalid (%v)", name, apperr.KindOf(err), err) + } + } + if got := f.users.calls.Load(); got != 0 { + t.Fatalf("invalid input must be rejected before any user lookup, got %d calls", got) + } +} + +func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) { + f := newFixture() + c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + _, foreign := f.svc.Get(f.ctx, f.eve, c.ID) + _, unknown := f.svc.Get(f.ctx, f.eve, ident.New()) + if !errors.Is(foreign, chat.ErrNotFound) || !errors.Is(unknown, chat.ErrNotFound) { + t.Fatalf("foreign = %v, unknown = %v; want both ErrNotFound", foreign, unknown) + } + if apperr.KindOf(foreign) != apperr.KindNotFound { + t.Fatalf("ErrNotFound must carry KindNotFound, got %v", apperr.KindOf(foreign)) + } +} + +func TestListPagesOnlyOwnChats(t *testing.T) { + f := newFixture() + var mine []uuid.UUID + for i := 0; i < 3; i++ { + c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + if err != nil { + t.Fatal(err) + } + mine = append(mine, c.ID) + if _, err := f.svc.Create(f.ctx, f.eve, "t", []uuid.UUID{f.eve}); err != nil { + t.Fatal(err) + } + } + page, err := f.svc.List(f.ctx, f.alice, nil, 2) + if err != nil { + t.Fatal(err) + } + if len(page.Items) != 2 || page.Next == nil || *page.Next != mine[1] { + t.Fatalf("first page = %+v", page) + } + if len(page.Items[0].MemberIDs) != 2 { + t.Fatalf("listed chats must carry their members: %+v", page.Items[0]) + } + rest, err := f.svc.List(f.ctx, f.alice, page.Next, 2) + if err != nil { + t.Fatal(err) + } + if len(rest.Items) != 1 || rest.Items[0].ID != mine[2] || rest.Next != nil { + t.Fatalf("second page = %+v", rest) + } + all, err := f.svc.List(f.ctx, f.bob, nil, 100) + if err != nil || len(all.Items) != 3 || all.Next != nil { + t.Fatalf("bob page = %+v, %v", all, err) + } +} + +func TestSendChecksAccessBeforeValidation(t *testing.T) { + f := newFixture() + c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + _, err := f.svc.Send(f.ctx, f.eve, c.ID, " ") + if !errors.Is(err, chat.ErrNotFound) { + t.Fatalf("outsider with blank text: err = %v, want ErrNotFound", err) + } + _, err = f.svc.Send(f.ctx, f.bob, c.ID, " ") + if apperr.KindOf(err) != apperr.KindInvalid { + t.Fatalf("member with blank text: kind = %v, want KindInvalid", apperr.KindOf(err)) + } + msg, err := f.svc.Send(f.ctx, f.bob, c.ID, "Привет 👋") + if err != nil || msg.SenderID != f.bob || msg.ChatID != c.ID || msg.Text != "Привет 👋" { + t.Fatalf("Send = %+v, %v", msg, err) + } +} + +func TestHistoryPagesAndHidesForeignChat(t *testing.T) { + f := newFixture() + c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + first, _ := f.svc.Send(f.ctx, f.alice, c.ID, "1") + second, _ := f.svc.Send(f.ctx, f.bob, c.ID, "2") + + page, err := f.svc.History(f.ctx, f.bob, c.ID, nil, 1) + if err != nil { + t.Fatal(err) + } + if len(page.Items) != 1 || page.Items[0].ID != first.ID || page.Next == nil || *page.Next != first.ID { + t.Fatalf("first page = %+v", page) + } + rest, err := f.svc.History(f.ctx, f.alice, c.ID, page.Next, 1) + if err != nil { + t.Fatal(err) + } + if len(rest.Items) != 1 || rest.Items[0].ID != second.ID || rest.Next != nil { + t.Fatalf("second page = %+v", rest) + } + if _, err := f.svc.History(f.ctx, f.eve, c.ID, nil, 10); !errors.Is(err, chat.ErrNotFound) { + t.Fatalf("outsider history: err = %v, want ErrNotFound", err) + } + empty, err := f.svc.History(f.ctx, f.alice, c.ID, &second.ID, 10) + if err != nil || empty.Items == nil || len(empty.Items) != 0 || empty.Next != nil { + t.Fatalf("page after last = %+v, %v; want empty non-nil items", empty, err) + } +} diff --git a/internal/chat/store/inmem.go b/internal/chat/store/inmem.go new file mode 100644 index 00000000..ae433acb --- /dev/null +++ b/internal/chat/store/inmem.go @@ -0,0 +1,188 @@ +// Package store holds the repository implementations of the chats module. +// inmem.go is the in-process one: state lives only in this process and +// disappears with it by design of the lab. +package store + +import ( + "bytes" + "context" + "sort" + "sync" + + "github.com/google/uuid" + + "messenger/internal/chat" +) + +// InMemChats keeps chat rows, a per-chat member list and set, and a +// per-member index of chat ids ordered by (created_at, id), which for UUIDv7 +// ids is plain id order. One read-write mutex guards all four maps. +type InMemChats struct { + mu sync.RWMutex + rows map[uuid.UUID]chat.Chat + members map[uuid.UUID][]uuid.UUID + memberOf map[uuid.UUID]map[uuid.UUID]struct{} + byMember map[uuid.UUID][]uuid.UUID +} + +var _ chat.ChatRepository = (*InMemChats)(nil) + +func NewInMemChats() *InMemChats { + return &InMemChats{ + rows: make(map[uuid.UUID]chat.Chat), + members: make(map[uuid.UUID][]uuid.UUID), + memberOf: make(map[uuid.UUID]map[uuid.UUID]struct{}), + byMember: make(map[uuid.UUID][]uuid.UUID), + } +} + +func (r *InMemChats) Create(_ context.Context, c chat.Chat, memberIDs []uuid.UUID) error { + members := append([]uuid.UUID(nil), memberIDs...) + set := make(map[uuid.UUID]struct{}, len(members)) + for _, m := range members { + set[m] = struct{}{} + } + + r.mu.Lock() + defer r.mu.Unlock() + r.rows[c.ID] = c + r.members[c.ID] = members + r.memberOf[c.ID] = set + for _, m := range members { + r.byMember[m] = insertSorted(r.byMember[m], c.ID) + } + return nil +} + +func (r *InMemChats) GetForMember(_ context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) { + r.mu.RLock() + defer r.mu.RUnlock() + if _, ok := r.memberOf[chatID][userID]; !ok { + return chat.ChatWithMembers{}, chat.ErrNotFound + } + return r.projection(chatID), nil +} + +func (r *InMemChats) IsMember(_ context.Context, chatID, userID uuid.UUID) (bool, error) { + r.mu.RLock() + defer r.mu.RUnlock() + _, ok := r.memberOf[chatID][userID] + return ok, nil +} + +func (r *InMemChats) ListByMember(_ context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) { + r.mu.RLock() + defer r.mu.RUnlock() + ids := r.byMember[userID] + start := firstAfter(ids, after) + end := min(start+limit, len(ids)) + out := make([]chat.ChatWithMembers, 0, end-start) + for _, id := range ids[start:end] { + out = append(out, r.projection(id)) + } + return out, nil +} + +// projection copies the member list so callers never alias the store. +func (r *InMemChats) projection(chatID uuid.UUID) chat.ChatWithMembers { + return chat.ChatWithMembers{ + Chat: r.rows[chatID], + MemberIDs: append([]uuid.UUID(nil), r.members[chatID]...), + } +} + +// InMemMessages keeps one ordered history per chat. The outer lock only +// guards the map of histories; each history has its own lock, so writers to +// different chats never contend. +type InMemMessages struct { + mu sync.RWMutex + byChat map[uuid.UUID]*history +} + +type history struct { + mu sync.RWMutex + items []chat.Message +} + +var _ chat.MessageRepository = (*InMemMessages)(nil) + +func NewInMemMessages() *InMemMessages { + return &InMemMessages{byChat: make(map[uuid.UUID]*history)} +} + +// Append inserts at the sorted position; ids are monotonic, so the usual +// case is an append at the tail. +func (r *InMemMessages) Append(_ context.Context, m chat.Message) error { + h := r.historyFor(m.ChatID, true) + h.mu.Lock() + defer h.mu.Unlock() + n := len(h.items) + if n == 0 || less(h.items[n-1].ID, m.ID) { + h.items = append(h.items, m) + return nil + } + i := sort.Search(n, func(k int) bool { return !less(h.items[k].ID, m.ID) }) + h.items = append(h.items, chat.Message{}) + copy(h.items[i+1:], h.items[i:]) + h.items[i] = m + return nil +} + +func (r *InMemMessages) ListByChat(_ context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]chat.Message, error) { + h := r.historyFor(chatID, false) + if h == nil { + return []chat.Message{}, nil + } + h.mu.RLock() + defer h.mu.RUnlock() + start := 0 + if after != nil { + start = sort.Search(len(h.items), func(k int) bool { return less(*after, h.items[k].ID) }) + } + end := min(start+limit, len(h.items)) + out := make([]chat.Message, end-start) + copy(out, h.items[start:end]) + return out, nil +} + +func (r *InMemMessages) historyFor(chatID uuid.UUID, create bool) *history { + r.mu.RLock() + h := r.byChat[chatID] + r.mu.RUnlock() + if h != nil || !create { + return h + } + r.mu.Lock() + defer r.mu.Unlock() + if h = r.byChat[chatID]; h == nil { + h = &history{} + r.byChat[chatID] = h + } + return h +} + +// less orders ids lexicographically, which for UUIDv7 is creation order. +func less(a, b uuid.UUID) bool { + return bytes.Compare(a[:], b[:]) < 0 +} + +func insertSorted(ids []uuid.UUID, id uuid.UUID) []uuid.UUID { + n := len(ids) + if n == 0 || less(ids[n-1], id) { + return append(ids, id) + } + i := sort.Search(n, func(k int) bool { return !less(ids[k], id) }) + ids = append(ids, uuid.Nil) + copy(ids[i+1:], ids[i:]) + ids[i] = id + return ids +} + +// firstAfter returns the index of the first id strictly greater than after, +// or len(ids) when there is none; nil after means the start. +func firstAfter(ids []uuid.UUID, after *uuid.UUID) int { + if after == nil { + return 0 + } + return sort.Search(len(ids), func(k int) bool { return less(*after, ids[k]) }) +} diff --git a/internal/chat/store/inmem_test.go b/internal/chat/store/inmem_test.go new file mode 100644 index 00000000..43b998a4 --- /dev/null +++ b/internal/chat/store/inmem_test.go @@ -0,0 +1,262 @@ +package store_test + +import ( + "bytes" + "context" + "errors" + "sync" + "testing" + + "github.com/google/uuid" + + "messenger/internal/chat" + "messenger/internal/chat/store" + "messenger/internal/platform/ident" +) + +func mustChat(t *testing.T) chat.Chat { + t.Helper() + c, err := chat.NewChat(ident.New(), "title") + if err != nil { + t.Fatal(err) + } + return c +} + +func mustMessage(t *testing.T, chatID uuid.UUID) chat.Message { + t.Helper() + m, err := chat.NewMessage(ident.New(), chatID, ident.New(), "text") + if err != nil { + t.Fatal(err) + } + return m +} + +func TestChatCreateThenGetForMember(t *testing.T) { + repo := store.NewInMemChats() + ctx := context.Background() + alice, bob := ident.New(), ident.New() + c := mustChat(t) + if err := repo.Create(ctx, c, []uuid.UUID{alice, bob}); err != nil { + t.Fatal(err) + } + got, err := repo.GetForMember(ctx, c.ID, bob) + if err != nil { + t.Fatal(err) + } + if got.Chat != c || len(got.MemberIDs) != 2 || got.MemberIDs[0] != alice || got.MemberIDs[1] != bob { + t.Fatalf("GetForMember = %+v", got) + } + got.MemberIDs[0] = ident.New() + again, _ := repo.GetForMember(ctx, c.ID, alice) + if again.MemberIDs[0] != alice { + t.Fatal("GetForMember must return a copy of the member list") + } + if _, err := repo.GetForMember(ctx, c.ID, ident.New()); !errors.Is(err, chat.ErrNotFound) { + t.Fatalf("outsider must get ErrNotFound, got %v", err) + } + for who, want := range map[uuid.UUID]bool{alice: true, bob: true, ident.New(): false} { + ok, err := repo.IsMember(ctx, c.ID, who) + if err != nil || ok != want { + t.Fatalf("IsMember(%s) = %v, %v; want %v", who, ok, err, want) + } + } +} + +func TestChatUnknownID(t *testing.T) { + repo := store.NewInMemChats() + ctx := context.Background() + if _, err := repo.GetForMember(ctx, ident.New(), ident.New()); !errors.Is(err, chat.ErrNotFound) { + t.Fatalf("GetForMember err = %v, want ErrNotFound", err) + } + if ok, err := repo.IsMember(ctx, ident.New(), ident.New()); err != nil || ok { + t.Fatalf("IsMember on unknown chat = %v, %v; want false, nil", ok, err) + } +} + +func TestListByMemberFiltersOrdersAndPages(t *testing.T) { + repo := store.NewInMemChats() + ctx := context.Background() + alice, bob := ident.New(), ident.New() + var mine []chat.Chat + for i := 0; i < 5; i++ { + mine = append(mine, mustChat(t)) + // A chat without alice must never show up in her listing. + if err := repo.Create(ctx, mustChat(t), []uuid.UUID{bob}); err != nil { + t.Fatal(err) + } + } + // Insert the oldest chat last to prove the store sorts, not appends. + for _, c := range mine[1:] { + if err := repo.Create(ctx, c, []uuid.UUID{alice, bob}); err != nil { + t.Fatal(err) + } + } + if err := repo.Create(ctx, mine[0], []uuid.UUID{alice, bob}); err != nil { + t.Fatal(err) + } + + page, err := repo.ListByMember(ctx, alice, nil, 3) + if err != nil { + t.Fatal(err) + } + if len(page) != 3 || page[0].ID != mine[0].ID || page[2].ID != mine[2].ID { + t.Fatalf("first page mismatch: %v", page) + } + if len(page[0].MemberIDs) != 2 { + t.Fatalf("listing must carry members: %+v", page[0]) + } + last := page[2].ID + rest, err := repo.ListByMember(ctx, alice, &last, 10) + if err != nil { + t.Fatal(err) + } + if len(rest) != 2 || rest[0].ID != mine[3].ID || rest[1].ID != mine[4].ID { + t.Fatalf("second page mismatch: %v", rest) + } + none, err := repo.ListByMember(ctx, ident.New(), nil, 10) + if err != nil { + t.Fatal(err) + } + if none == nil || len(none) != 0 { + t.Fatalf("unknown member must get an empty non-nil slice, got %#v", none) + } +} + +func TestListByChatUnknownChatIsEmpty(t *testing.T) { + got, err := store.NewInMemMessages().ListByChat(context.Background(), ident.New(), nil, 10) + if err != nil { + t.Fatal(err) + } + if got == nil || len(got) != 0 { + t.Fatalf("want empty non-nil slice, got %#v", got) + } +} + +func TestAppendOutOfOrderIsSortedByID(t *testing.T) { + repo := store.NewInMemMessages() + ctx := context.Background() + chatID := ident.New() + first, second, third := mustMessage(t, chatID), mustMessage(t, chatID), mustMessage(t, chatID) + for _, m := range []chat.Message{third, first, second} { + if err := repo.Append(ctx, m); err != nil { + t.Fatal(err) + } + } + got, err := repo.ListByChat(ctx, chatID, nil, 10) + if err != nil { + t.Fatal(err) + } + if len(got) != 3 || got[0].ID != first.ID || got[1].ID != second.ID || got[2].ID != third.ID { + t.Fatalf("history must be ordered by id, got %v", got) + } +} + +func TestPagingWalksWholeHistoryWithoutGapsOrDuplicates(t *testing.T) { + repo := store.NewInMemMessages() + ctx := context.Background() + chatID := ident.New() + const total = 250 + var expected []uuid.UUID + for i := 0; i < total; i++ { + m := mustMessage(t, chatID) + expected = append(expected, m.ID) + if err := repo.Append(ctx, m); err != nil { + t.Fatal(err) + } + } + for _, limit := range []int{1, 7, 100} { + var seen []uuid.UUID + var after *uuid.UUID + for { + page, err := repo.ListByChat(ctx, chatID, after, limit) + if err != nil { + t.Fatal(err) + } + if len(page) == 0 { + break + } + if len(page) > limit { + t.Fatalf("limit %d: page has %d items", limit, len(page)) + } + for _, m := range page { + seen = append(seen, m.ID) + } + last := page[len(page)-1].ID + after = &last + } + if len(seen) != total { + t.Fatalf("limit %d: walked %d messages, want %d", limit, len(seen), total) + } + for i := range seen { + if seen[i] != expected[i] { + t.Fatalf("limit %d: position %d has %s, want %s", limit, i, seen[i], expected[i]) + } + } + } +} + +func TestAfterUnknownIDStartsAtNextGreater(t *testing.T) { + repo := store.NewInMemMessages() + ctx := context.Background() + chatID := ident.New() + a := mustMessage(t, chatID) + gap := ident.New() // never stored, but sorts between a and b + b := mustMessage(t, chatID) + _ = repo.Append(ctx, a) + _ = repo.Append(ctx, b) + got, _ := repo.ListByChat(ctx, chatID, &gap, 10) + if len(got) != 1 || got[0].ID != b.ID { + t.Fatalf("want only b after the gap id, got %v", got) + } +} + +func TestListByChatReturnsCopy(t *testing.T) { + repo := store.NewInMemMessages() + ctx := context.Background() + chatID := ident.New() + _ = repo.Append(ctx, mustMessage(t, chatID)) + page, _ := repo.ListByChat(ctx, chatID, nil, 10) + page[0].Text = "mutated" + again, _ := repo.ListByChat(ctx, chatID, nil, 10) + if again[0].Text != "text" { + t.Fatal("ListByChat must not expose internal storage") + } +} + +func TestConcurrentAppendsKeepEveryMessageOrdered(t *testing.T) { + repo := store.NewInMemMessages() + ctx := context.Background() + chatID := ident.New() + const clients, perClient = 10, 10 + var wg sync.WaitGroup + for c := 0; c < clients; c++ { + wg.Add(1) + go func() { + defer wg.Done() + for i := 0; i < perClient; i++ { + if err := repo.Append(ctx, mustMessage(t, chatID)); err != nil { + t.Error(err) + } + } + }() + } + wg.Wait() + got, err := repo.ListByChat(ctx, chatID, nil, clients*perClient+1) + if err != nil { + t.Fatal(err) + } + if len(got) != clients*perClient { + t.Fatalf("stored %d messages, want %d", len(got), clients*perClient) + } + unique := make(map[uuid.UUID]struct{}, len(got)) + for i, m := range got { + unique[m.ID] = struct{}{} + if i > 0 && bytes.Compare(got[i-1].ID[:], m.ID[:]) >= 0 { + t.Fatalf("history out of order at %d", i) + } + } + if len(unique) != clients*perClient { + t.Fatalf("%d unique ids, want %d", len(unique), clients*perClient) + } +} diff --git a/internal/health/handler.go b/internal/health/handler.go new file mode 100644 index 00000000..41a7afa9 --- /dev/null +++ b/internal/health/handler.go @@ -0,0 +1,53 @@ +// Package health serves the probes. Liveness only proves the process +// answers; readiness is switched off during shutdown so an orchestrator stops +// routing new traffic before the listener closes. +package health + +import ( + "net/http" + "sync/atomic" + + "github.com/gin-gonic/gin" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/httpx" +) + +type Handler struct { + ready atomic.Bool +} + +type status struct { + Status string `json:"status"` +} + +func New() *Handler { + h := &Handler{} + h.ready.Store(true) + return h +} + +func (h *Handler) SetReady(ready bool) { + h.ready.Store(ready) +} + +func (h *Handler) IsReady() bool { + return h.ready.Load() +} + +func (h *Handler) Register(r gin.IRouter) { + r.GET("/health/live", h.live) + r.GET("/health/ready", h.readyz) +} + +func (h *Handler) live(c *gin.Context) { + httpx.JSON(c, http.StatusOK, status{Status: "ok"}) +} + +func (h *Handler) readyz(c *gin.Context) { + if !h.ready.Load() { + httpx.Error(c, apperr.Unavailable("service is shutting down")) + return + } + httpx.JSON(c, http.StatusOK, status{Status: "ok"}) +} diff --git a/internal/platform/apperr/apperr.go b/internal/platform/apperr/apperr.go new file mode 100644 index 00000000..8de0ff3e --- /dev/null +++ b/internal/platform/apperr/apperr.go @@ -0,0 +1,54 @@ +// Package apperr is the application's error vocabulary: every module reports +// failures as a Kind plus a message that is safe to show to clients, and the +// transport layer owns the only mapping from Kind to HTTP status. Causes are +// kept for logs but never rendered. +package apperr + +import "errors" + +type Kind int + +const ( + KindInternal Kind = iota + KindInvalid + KindUnauthorized + KindNotFound + KindConflict + KindPayloadTooLarge + KindUnsupportedMedia + KindUnavailable +) + +type Error struct { + Kind Kind + Message string + Cause error +} + +func (e *Error) Error() string { return e.Message } + +func (e *Error) Unwrap() error { return e.Cause } + +func New(kind Kind, message string) *Error { + return &Error{Kind: kind, Message: message} +} + +func Wrap(kind Kind, message string, cause error) *Error { + return &Error{Kind: kind, Message: message, Cause: cause} +} + +// KindOf classifies any error; foreign errors are internal. +func KindOf(err error) Kind { + if e, ok := errors.AsType[*Error](err); ok { + return e.Kind + } + return KindInternal +} + +func Invalid(message string) *Error { return New(KindInvalid, message) } +func Unauthorized(message string) *Error { return New(KindUnauthorized, message) } +func NotFound(message string) *Error { return New(KindNotFound, message) } +func Conflict(message string) *Error { return New(KindConflict, message) } +func PayloadTooLarge(message string) *Error { return New(KindPayloadTooLarge, message) } +func UnsupportedMedia(message string) *Error { return New(KindUnsupportedMedia, message) } +func Unavailable(message string) *Error { return New(KindUnavailable, message) } diff --git a/internal/platform/apperr/apperr_test.go b/internal/platform/apperr/apperr_test.go new file mode 100644 index 00000000..6de347f7 --- /dev/null +++ b/internal/platform/apperr/apperr_test.go @@ -0,0 +1,49 @@ +package apperr_test + +import ( + "errors" + "fmt" + "testing" + + "messenger/internal/platform/apperr" +) + +func TestKindOfReturnsKindForApplicationError(t *testing.T) { + err := apperr.NotFound("chat not found") + if got := apperr.KindOf(err); got != apperr.KindNotFound { + t.Fatalf("KindOf = %v, want KindNotFound", got) + } + if err.Error() != "chat not found" { + t.Fatalf("Error() = %q", err.Error()) + } +} + +func TestKindOfSeesThroughWrapping(t *testing.T) { + err := fmt.Errorf("service: %w", apperr.Conflict("username taken")) + if got := apperr.KindOf(err); got != apperr.KindConflict { + t.Fatalf("KindOf = %v, want KindConflict", got) + } +} + +func TestKindOfDefaultsToInternal(t *testing.T) { + if got := apperr.KindOf(errors.New("boom")); got != apperr.KindInternal { + t.Fatalf("KindOf = %v, want KindInternal", got) + } + if got := apperr.KindOf(nil); got != apperr.KindInternal { + t.Fatalf("KindOf(nil) = %v, want KindInternal", got) + } +} + +func TestWrapKeepsCause(t *testing.T) { + cause := errors.New("cause") + err := apperr.Wrap(apperr.KindInvalid, "bad input", cause) + if !errors.Is(err, cause) { + t.Fatal("wrapped error must unwrap to cause") + } + if apperr.KindOf(err) != apperr.KindInvalid { + t.Fatal("wrapped error must keep its kind") + } + if err.Error() != "bad input" { + t.Fatalf("Error() = %q, want public message only", err.Error()) + } +} diff --git a/internal/platform/config/config.go b/internal/platform/config/config.go new file mode 100644 index 00000000..cd04dc3c --- /dev/null +++ b/internal/platform/config/config.go @@ -0,0 +1,79 @@ +// Package config reads the process configuration from the environment. +// Every setting has a default suited to the lab stand; an invalid value stops +// the process at start-up with a message naming the variable. +package config + +import ( + "fmt" + "log/slog" + "net" + "strconv" + "strings" + "time" +) + +type Config struct { + Host string + Port int + Env string + LogLevel slog.Level + ShutdownTimeout time.Duration + MaxBodyBytes int64 +} + +// Lookup reads one variable, reporting whether it was set; os.LookupEnv +// in production, a map in tests. +type Lookup func(key string) (string, bool) + +func Load(lookup Lookup) (Config, error) { + cfg := Config{ + Host: "0.0.0.0", + Port: 8080, + Env: "lab", + LogLevel: slog.LevelInfo, + ShutdownTimeout: 8 * time.Second, + MaxBodyBytes: 64 * 1024, + } + if v, ok := lookup("APP_HOST"); ok { + if strings.TrimSpace(v) == "" { + return Config{}, fmt.Errorf("APP_HOST must not be empty") + } + cfg.Host = v + } + if v, ok := lookup("APP_PORT"); ok { + port, err := strconv.Atoi(v) + if err != nil || port < 1 || port > 65535 { + return Config{}, fmt.Errorf("APP_PORT must be an integer between 1 and 65535, got %q", v) + } + cfg.Port = port + } + if v, ok := lookup("APP_ENV"); ok && strings.TrimSpace(v) != "" { + cfg.Env = v + } + if v, ok := lookup("APP_LOG_LEVEL"); ok { + var level slog.Level + if err := level.UnmarshalText([]byte(v)); err != nil { + return Config{}, fmt.Errorf("APP_LOG_LEVEL must be debug, info, warn or error, got %q", v) + } + cfg.LogLevel = level + } + if v, ok := lookup("APP_SHUTDOWN_TIMEOUT"); ok { + d, err := time.ParseDuration(v) + if err != nil || d <= 0 { + return Config{}, fmt.Errorf("APP_SHUTDOWN_TIMEOUT must be a positive duration such as 8s, got %q", v) + } + cfg.ShutdownTimeout = d + } + if v, ok := lookup("APP_MAX_BODY_BYTES"); ok { + n, err := strconv.ParseInt(v, 10, 64) + if err != nil || n <= 0 { + return Config{}, fmt.Errorf("APP_MAX_BODY_BYTES must be a positive integer, got %q", v) + } + cfg.MaxBodyBytes = n + } + return cfg, nil +} + +func (c Config) Addr() string { + return net.JoinHostPort(c.Host, strconv.Itoa(c.Port)) +} diff --git a/internal/platform/config/config_test.go b/internal/platform/config/config_test.go new file mode 100644 index 00000000..efc496af --- /dev/null +++ b/internal/platform/config/config_test.go @@ -0,0 +1,71 @@ +package config_test + +import ( + "log/slog" + "testing" + "time" + + "messenger/internal/platform/config" +) + +func env(values map[string]string) func(string) (string, bool) { + return func(key string) (string, bool) { + v, ok := values[key] + return v, ok + } +} + +func TestDefaults(t *testing.T) { + cfg, err := config.Load(env(nil)) + if err != nil { + t.Fatalf("Load error: %v", err) + } + if cfg.Addr() != "0.0.0.0:8080" { + t.Fatalf("Addr = %q", cfg.Addr()) + } + if cfg.Env != "lab" || cfg.LogLevel != slog.LevelInfo { + t.Fatalf("cfg = %+v", cfg) + } + if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 { + t.Fatalf("cfg = %+v", cfg) + } +} + +func TestOverrides(t *testing.T) { + cfg, err := config.Load(env(map[string]string{ + "APP_HOST": "127.0.0.1", + "APP_PORT": "9090", + "APP_ENV": "dev", + "APP_LOG_LEVEL": "debug", + "APP_SHUTDOWN_TIMEOUT": "3s", + "APP_MAX_BODY_BYTES": "1024", + })) + if err != nil { + t.Fatalf("Load error: %v", err) + } + if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.LogLevel != slog.LevelDebug { + t.Fatalf("cfg = %+v", cfg) + } + if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 { + t.Fatalf("cfg = %+v", cfg) + } +} + +func TestInvalidValuesAreReported(t *testing.T) { + cases := map[string]map[string]string{ + "port text": {"APP_PORT": "abc"}, + "port zero": {"APP_PORT": "0"}, + "port too big": {"APP_PORT": "70000"}, + "level": {"APP_LOG_LEVEL": "loud"}, + "timeout": {"APP_SHUTDOWN_TIMEOUT": "soon"}, + "timeout negative": {"APP_SHUTDOWN_TIMEOUT": "-1s"}, + "body zero": {"APP_MAX_BODY_BYTES": "0"}, + "body text": {"APP_MAX_BODY_BYTES": "big"}, + "empty host": {"APP_HOST": ""}, + } + for name, values := range cases { + if _, err := config.Load(env(values)); err == nil { + t.Errorf("%s: want error", name) + } + } +} diff --git a/internal/platform/httpx/decode.go b/internal/platform/httpx/decode.go new file mode 100644 index 00000000..ce8eeae7 --- /dev/null +++ b/internal/platform/httpx/decode.go @@ -0,0 +1,70 @@ +package httpx + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "mime" + "net/http" + "strings" + "unicode/utf8" + + "github.com/gin-gonic/gin" + + "messenger/internal/platform/apperr" +) + +// DecodeJSON reads exactly one JSON object into dst and enforces what gin's +// binding does not: the media type must be application/json (415), invalid +// UTF-8, unknown fields, wrong types, malformed syntax and trailing data are +// 400, an oversized body is 413, and messages never expose Go type names. +func DecodeJSON(c *gin.Context, dst any) error { + mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type")) + if err != nil || mediaType != "application/json" { + return apperr.UnsupportedMedia("Content-Type must be application/json") + } + // The body is already capped by the bodyLimit middleware, so reading it + // whole is bounded. + body, err := io.ReadAll(c.Request.Body) + if err != nil { + return decodeError(err) + } + // encoding/json silently replaces invalid UTF-8 with U+FFFD; the contract + // and RFC 8259 require UTF-8, so such a body is rejected, not repaired. + if !utf8.Valid(body) { + return apperr.Invalid("request body must be valid UTF-8") + } + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + if err := dec.Decode(dst); err != nil { + return decodeError(err) + } + if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) { + return apperr.Invalid("unexpected data after JSON body") + } + return nil +} + +func decodeError(err error) error { + if tooLarge, ok := errors.AsType[*http.MaxBytesError](err); ok { + return apperr.Wrap(apperr.KindPayloadTooLarge, fmt.Sprintf("request body must be at most %d bytes", tooLarge.Limit), err) + } + if errors.Is(err, io.EOF) { + return apperr.Wrap(apperr.KindInvalid, "request body is required", err) + } + if wrongType, ok := errors.AsType[*json.UnmarshalTypeError](err); ok { + if wrongType.Field == "" { + return apperr.Wrap(apperr.KindInvalid, "request body must be a JSON object", err) + } + return apperr.Wrap(apperr.KindInvalid, fmt.Sprintf("field %q has wrong type", wrongType.Field), err) + } + // encoding/json reports unknown fields as a plain error without a type. + const unknownPrefix = "json: unknown field " + if msg := err.Error(); strings.HasPrefix(msg, unknownPrefix) { + field := strings.Trim(strings.TrimPrefix(msg, unknownPrefix), `"`) + return apperr.Wrap(apperr.KindInvalid, fmt.Sprintf("unknown field %q", field), err) + } + return apperr.Wrap(apperr.KindInvalid, "malformed JSON body", err) +} diff --git a/internal/platform/httpx/engine.go b/internal/platform/httpx/engine.go new file mode 100644 index 00000000..ba56a897 --- /dev/null +++ b/internal/platform/httpx/engine.go @@ -0,0 +1,49 @@ +// Package httpx is the transport layer over gin: request ids, access +// logging, panic recovery, body limits, strict JSON decoding and the single +// error format of the API contract. gin is used for routing and the +// middleware chain; everything here exists because the contract demands a +// specific behaviour that gin's own helpers do not provide. +package httpx + +import ( + "log/slog" + "net/http" + + "github.com/gin-gonic/gin" + + "messenger/internal/platform/apperr" +) + +const ( + keyRequestID = "messenger.request_id" + keyLogger = "messenger.logger" +) + +// NewEngine builds an engine whose every response, including routing +// failures and panics, carries X-Request-Id and the contract error body. +func NewEngine(log *slog.Logger, maxBodyBytes int64) *gin.Engine { + gin.SetMode(gin.ReleaseMode) + e := gin.New() + // Redirects would answer with text/plain and without a request id. + e.RedirectTrailingSlash = false + e.RedirectFixedPath = false + e.HandleMethodNotAllowed = true + + e.Use(requestID(log), accessLog(), recovery(), bodyLimit(maxBodyBytes)) + e.NoRoute(func(c *gin.Context) { Error(c, apperr.NotFound("route not found")) }) + e.NoMethod(func(c *gin.Context) { Fail(c, http.StatusMethodNotAllowed, "invalid_request", "method not allowed") }) + return e +} + +func RequestID(c *gin.Context) string { + return c.GetString(keyRequestID) +} + +func logger(c *gin.Context) *slog.Logger { + if l, ok := c.Get(keyLogger); ok { + if log, ok := l.(*slog.Logger); ok { + return log + } + } + return slog.Default() +} diff --git a/internal/platform/httpx/httpx_test.go b/internal/platform/httpx/httpx_test.go new file mode 100644 index 00000000..2683bea9 --- /dev/null +++ b/internal/platform/httpx/httpx_test.go @@ -0,0 +1,309 @@ +package httpx_test + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/httpx" +) + +type errorBody struct { + Error struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` + } `json:"error"` +} + +type payload struct { + Text string `json:"text"` + Count int `json:"count"` +} + +func newEngine(t *testing.T, logs *bytes.Buffer) *gin.Engine { + t.Helper() + return newEngineAt(t, logs, slog.LevelDebug) +} + +func newEngineAt(t *testing.T, logs *bytes.Buffer, level slog.Level) *gin.Engine { + t.Helper() + if logs == nil { + logs = &bytes.Buffer{} + } + log := slog.New(slog.NewJSONHandler(logs, &slog.HandlerOptions{Level: level})) + e := httpx.NewEngine(log, 64) + e.GET("/ok", func(c *gin.Context) { httpx.JSON(c, http.StatusOK, gin.H{"text": ""}) }) + e.GET("/panic", func(c *gin.Context) { panic("secret detail") }) + e.GET("/fail", func(c *gin.Context) { httpx.Error(c, errors.New("db password leaked")) }) + e.GET("/notfound", func(c *gin.Context) { httpx.Error(c, apperr.NotFound("chat not found")) }) + e.GET("/who", func(c *gin.Context) { + httpx.SetUserID(c, uuid.MustParse("11111111-1111-4111-8111-111111111111")) + id, ok := httpx.UserID(c) + httpx.JSON(c, http.StatusOK, gin.H{"id": id.String(), "ok": ok}) + }) + e.POST("/decode", func(c *gin.Context) { + var p payload + if err := httpx.DecodeJSON(c, &p); err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusOK, p) + }) + return e +} + +func do(t *testing.T, e *gin.Engine, method, path string, body string, headers map[string]string) (*httptest.ResponseRecorder, errorBody) { + t.Helper() + var reader io.Reader + if body != "" { + reader = strings.NewReader(body) + } + req := httptest.NewRequest(method, path, reader) + for k, v := range headers { + req.Header.Set(k, v) + } + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + var eb errorBody + if rec.Code >= 400 { + if err := json.Unmarshal(rec.Body.Bytes(), &eb); err != nil { + t.Fatalf("%s %s: error body is not JSON: %v: %s", method, path, err, rec.Body.String()) + } + } + return rec, eb +} + +func assertError(t *testing.T, rec *httptest.ResponseRecorder, eb errorBody, status int, code string) { + t.Helper() + if rec.Code != status { + t.Fatalf("status = %d, want %d (body %s)", rec.Code, status, rec.Body.String()) + } + if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { + t.Fatalf("Content-Type = %q", ct) + } + rid := rec.Header().Get("X-Request-Id") + if rid == "" { + t.Fatal("missing X-Request-Id header") + } + if eb.Error.RequestID != rid { + t.Fatalf("body request_id %q != header %q", eb.Error.RequestID, rid) + } + if eb.Error.Code != code || eb.Error.Message == "" { + t.Fatalf("error = %+v, want code %q with a message", eb.Error, code) + } +} + +func TestUnknownRouteIsJSON404(t *testing.T) { + rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/nope", "", nil) + assertError(t, rec, eb, http.StatusNotFound, "not_found") +} + +func TestTrailingSlashIsNotRedirected(t *testing.T) { + rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/ok/", "", nil) + assertError(t, rec, eb, http.StatusNotFound, "not_found") +} + +func TestWrongMethodIsJSON405WithAllow(t *testing.T) { + rec, eb := do(t, newEngine(t, nil), http.MethodDelete, "/ok", "", nil) + assertError(t, rec, eb, http.StatusMethodNotAllowed, "invalid_request") + if !strings.Contains(rec.Header().Get("Allow"), "GET") { + t.Fatalf("Allow = %q, want GET", rec.Header().Get("Allow")) + } +} + +func TestPanicBecomesJSON500WithoutDetails(t *testing.T) { + logs := &bytes.Buffer{} + rec, eb := do(t, newEngine(t, logs), http.MethodGet, "/panic", "", nil) + assertError(t, rec, eb, http.StatusInternalServerError, "internal_error") + if strings.Contains(rec.Body.String(), "secret detail") { + t.Fatal("panic value leaked to the client") + } + if !strings.Contains(logs.String(), "secret detail") { + t.Fatal("panic must be logged for operators") + } +} + +func TestForeignErrorBecomesJSON500WithoutDetails(t *testing.T) { + logs := &bytes.Buffer{} + rec, eb := do(t, newEngine(t, logs), http.MethodGet, "/fail", "", nil) + assertError(t, rec, eb, http.StatusInternalServerError, "internal_error") + if strings.Contains(rec.Body.String(), "password") { + t.Fatal("internal error text leaked to the client") + } + if !strings.Contains(logs.String(), "db password leaked") { + t.Fatal("cause must be logged for operators") + } +} + +func TestApplicationErrorMapping(t *testing.T) { + rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/notfound", "", nil) + assertError(t, rec, eb, http.StatusNotFound, "not_found") + if eb.Error.Message != "chat not found" { + t.Fatalf("message = %q", eb.Error.Message) + } +} + +func TestSuccessResponseHeadersAndEscaping(t *testing.T) { + rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", nil) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d", rec.Code) + } + if ct := rec.Header().Get("Content-Type"); ct != "application/json; charset=utf-8" { + t.Fatalf("Content-Type = %q", ct) + } + if rec.Header().Get("X-Request-Id") == "" { + t.Fatal("missing X-Request-Id on success") + } + if !strings.Contains(rec.Body.String(), `""`) { + t.Fatalf("HTML escaping must be off, body %s", rec.Body.String()) + } +} + +func TestRequestIDIsGeneratedNotEchoed(t *testing.T) { + rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", map[string]string{"X-Request-Id": "client-chosen"}) + if got := rec.Header().Get("X-Request-Id"); got == "" || got == "client-chosen" { + t.Fatalf("X-Request-Id = %q, want a server generated value", got) + } +} + +func TestDecodeJSONHappyPath(t *testing.T) { + rec, _ := do(t, newEngine(t, nil), http.MethodPost, "/decode", `{"text":"Привет","count":2}`, map[string]string{"Content-Type": "application/json; charset=utf-8"}) + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"Привет"`) { + t.Fatalf("status %d body %s", rec.Code, rec.Body.String()) + } +} + +func TestDecodeJSONFailures(t *testing.T) { + jsonCT := map[string]string{"Content-Type": "application/json"} + cases := []struct { + name string + body string + headers map[string]string + status int + code string + contain string + }{ + {"no content type", `{"text":"x"}`, nil, http.StatusUnsupportedMediaType, "unsupported_media_type", ""}, + {"wrong content type", `{"text":"x"}`, map[string]string{"Content-Type": "text/plain"}, http.StatusUnsupportedMediaType, "unsupported_media_type", ""}, + {"empty body", "", jsonCT, http.StatusBadRequest, "invalid_request", "body"}, + {"malformed", `{"text":`, jsonCT, http.StatusBadRequest, "invalid_request", "malformed"}, + {"invalid utf-8", "{\"text\":\"bad \xff byte\"}", jsonCT, http.StatusBadRequest, "invalid_request", "UTF-8"}, + {"unknown field", `{"text":"x","extra":1}`, jsonCT, http.StatusBadRequest, "invalid_request", "extra"}, + {"wrong type", `{"text":123}`, jsonCT, http.StatusBadRequest, "invalid_request", "text"}, + {"trailing data", `{"text":"x"} {"text":"y"}`, jsonCT, http.StatusBadRequest, "invalid_request", "after"}, + {"array instead of object", `[1,2]`, jsonCT, http.StatusBadRequest, "invalid_request", ""}, + {"too large", `{"text":"` + strings.Repeat("x", 100) + `"}`, jsonCT, http.StatusRequestEntityTooLarge, "payload_too_large", ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + rec, eb := do(t, newEngine(t, nil), http.MethodPost, "/decode", tc.body, tc.headers) + assertError(t, rec, eb, tc.status, tc.code) + if tc.contain != "" && !strings.Contains(eb.Error.Message, tc.contain) { + t.Fatalf("message %q must mention %q", eb.Error.Message, tc.contain) + } + if strings.Contains(eb.Error.Message, "httpx_test") || strings.Contains(eb.Error.Message, "payload") { + t.Fatalf("message must not leak Go type names: %q", eb.Error.Message) + } + }) + } +} + +func TestAccessLogHasRequestFieldsButNoBody(t *testing.T) { + logs := &bytes.Buffer{} + e := newEngine(t, logs) + rec, _ := do(t, e, http.MethodPost, "/decode", `{"text":"confidential"}`, map[string]string{"Content-Type": "application/json"}) + var entry map[string]any + line := lastLine(logs.String()) + if err := json.Unmarshal([]byte(line), &entry); err != nil { + t.Fatalf("log line is not JSON: %v: %s", err, line) + } + for _, key := range []string{"request_id", "method", "path", "status", "duration_ms"} { + if _, ok := entry[key]; !ok { + t.Errorf("log entry missing %q: %s", key, line) + } + } + if entry["request_id"] != rec.Header().Get("X-Request-Id") { + t.Errorf("log request_id %v != header %q", entry["request_id"], rec.Header().Get("X-Request-Id")) + } + if strings.Contains(logs.String(), "confidential") { + t.Fatal("request body must never be logged") + } +} + +func TestAccessLogLevelFollowsStatus(t *testing.T) { + logs := &bytes.Buffer{} + e := newEngineAt(t, logs, slog.LevelInfo) + + do(t, e, http.MethodGet, "/ok", "", nil) + if strings.Contains(logs.String(), `"msg":"request"`) { + t.Fatalf("successful requests must be logged at debug level only, got %s", logs.String()) + } + do(t, e, http.MethodGet, "/notfound", "", nil) + if entry := logEntry(t, logs); entry["level"] != "INFO" || entry["status"] != float64(404) { + t.Fatalf("client error must be logged at info level with its status, got %v", entry) + } + do(t, e, http.MethodGet, "/fail", "", nil) + if entry := logEntry(t, logs); entry["level"] != "ERROR" || entry["status"] != float64(500) { + t.Fatalf("server error must be logged at error level with its status, got %v", entry) + } + + verbose := &bytes.Buffer{} + do(t, newEngineAt(t, verbose, slog.LevelDebug), http.MethodGet, "/ok", "", nil) + if entry := logEntry(t, verbose); entry["level"] != "DEBUG" || entry["status"] != float64(200) { + t.Fatalf("at debug level successful requests must be logged, got %v", entry) + } +} + +func TestUserIDRoundTripAndAccessLogAttribution(t *testing.T) { + logs := &bytes.Buffer{} + rec, _ := do(t, newEngine(t, logs), http.MethodGet, "/who", "", nil) + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"ok":true`) { + t.Fatalf("UserID must return what SetUserID stored: %d %s", rec.Code, rec.Body.String()) + } + if entry := logEntry(t, logs); entry["user_id"] != "11111111-1111-4111-8111-111111111111" { + t.Fatalf("access log must attribute the request to the user, got %v", entry) + } + anonymous := &bytes.Buffer{} + do(t, newEngine(t, anonymous), http.MethodGet, "/ok", "", nil) + if _, present := logEntry(t, anonymous)["user_id"]; present { + t.Fatal("anonymous requests must not carry user_id") + } +} + +func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any { + t.Helper() + var entry map[string]any + line := lastLine(logs.String()) + if err := json.Unmarshal([]byte(line), &entry); err != nil { + t.Fatalf("log line is not JSON: %v: %s", err, line) + } + return entry +} + +func TestTimestampFormat(t *testing.T) { + ts := time.Date(2026, 9, 1, 12, 0, 0, 0, time.FixedZone("MSK", 3*3600)) + if got := httpx.Timestamp(ts); got != "2026-09-01T09:00:00.000Z" { + t.Fatalf("Timestamp = %q", got) + } + ts = time.Date(2026, 9, 1, 12, 0, 0, 7_000_000, time.UTC) + if got := httpx.Timestamp(ts); got != "2026-09-01T12:00:00.007Z" { + t.Fatalf("Timestamp = %q", got) + } +} + +func lastLine(s string) string { + lines := strings.Split(strings.TrimSpace(s), "\n") + return lines[len(lines)-1] +} diff --git a/internal/platform/httpx/identity.go b/internal/platform/httpx/identity.go new file mode 100644 index 00000000..231d67e8 --- /dev/null +++ b/internal/platform/httpx/identity.go @@ -0,0 +1,25 @@ +package httpx + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +const keyUserID = "messenger.user_id" + +// SetUserID records the identified caller for the handlers behind the +// identification middleware and for the access log. It lives here rather +// than in the auth package because the access log needs it and auth already +// depends on this package. +func SetUserID(c *gin.Context, id uuid.UUID) { + c.Set(keyUserID, id) +} + +func UserID(c *gin.Context) (uuid.UUID, bool) { + v, ok := c.Get(keyUserID) + if !ok { + return uuid.Nil, false + } + id, ok := v.(uuid.UUID) + return id, ok +} diff --git a/internal/platform/httpx/middleware.go b/internal/platform/httpx/middleware.go new file mode 100644 index 00000000..724b1830 --- /dev/null +++ b/internal/platform/httpx/middleware.go @@ -0,0 +1,105 @@ +package httpx + +import ( + "crypto/rand" + "encoding/hex" + "fmt" + "log/slog" + "net/http" + "runtime/debug" + "time" + + "github.com/gin-gonic/gin" +) + +// requestID assigns a fresh identifier to every request. +// +// An incoming X-Request-Id is deliberately not reused. This service is +// client-facing: there is no trusted upstream that could have minted the +// value, so honouring it would let any caller pick an arbitrary string that +// ends up in logs (log injection, unbounded length) or collides with another +// request and corrupts log correlation. Reusing the header is the right +// choice only when the caller is a trusted proxy or a sibling service that +// propagates one id across a chain of calls. +func requestID(log *slog.Logger) gin.HandlerFunc { + return func(c *gin.Context) { + var raw [16]byte + if _, err := rand.Read(raw[:]); err != nil { + panic("httpx: random source unavailable: " + err.Error()) + } + id := hex.EncodeToString(raw[:]) + c.Set(keyRequestID, id) + c.Set(keyLogger, log) + c.Writer.Header().Set("X-Request-Id", id) + c.Next() + } +} + +// accessLog writes one line per request at a level that follows the +// outcome (2xx debug, 4xx info, 5xx error), so the ordinary log level decides +// how much traffic is recorded. Headers, query strings and bodies are never +// recorded. time.Since uses the monotonic clock, so wall clock adjustments +// cannot distort the duration. +func accessLog() gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + c.Next() + + status := c.Writer.Status() + level := slog.LevelDebug + switch { + case status >= http.StatusInternalServerError: + level = slog.LevelError + case status >= http.StatusBadRequest: + level = slog.LevelInfo + } + log := logger(c) + ctx := c.Request.Context() + if !log.Enabled(ctx, level) { + return + } + attrs := []any{ + "request_id", RequestID(c), + "method", c.Request.Method, + "path", c.Request.URL.Path, + "status", status, + "duration_ms", float64(time.Since(start).Microseconds()) / 1000, + "bytes", c.Writer.Size(), + } + if id, ok := UserID(c); ok { + attrs = append(attrs, "user_id", id.String()) + } + log.Log(ctx, level, "request", attrs...) + } +} + +// recovery turns a panic into a contract 500; the stack goes to the log only. +func recovery() gin.HandlerFunc { + return func(c *gin.Context) { + defer func() { + if r := recover(); r != nil { + logger(c).Error("panic recovered", + "request_id", RequestID(c), + "panic", fmt.Sprint(r), + "stack", string(debug.Stack()), + ) + if !c.Writer.Written() { + Fail(c, http.StatusInternalServerError, "internal_error", "internal error") + } + c.Abort() + } + }() + c.Next() + } +} + +// bodyLimit caps request bodies; exceeding it surfaces from the JSON +// decoder as *http.MaxBytesError. +func bodyLimit(maxBytes int64) gin.HandlerFunc { + return func(c *gin.Context) { + if c.Request.Body != nil { + c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes) + } + c.Next() + } +} diff --git a/internal/platform/httpx/respond.go b/internal/platform/httpx/respond.go new file mode 100644 index 00000000..f28ac418 --- /dev/null +++ b/internal/platform/httpx/respond.go @@ -0,0 +1,71 @@ +package httpx + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" + + "messenger/internal/platform/apperr" +) + +type errorEnvelope struct { + Error errorDetail `json:"error"` +} + +type errorDetail struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +type statusCode struct { + status int + code string +} + +var kinds = map[apperr.Kind]statusCode{ + apperr.KindInvalid: {http.StatusBadRequest, "invalid_request"}, + apperr.KindUnauthorized: {http.StatusUnauthorized, "unauthorized"}, + apperr.KindNotFound: {http.StatusNotFound, "not_found"}, + apperr.KindConflict: {http.StatusConflict, "conflict"}, + apperr.KindPayloadTooLarge: {http.StatusRequestEntityTooLarge, "payload_too_large"}, + apperr.KindUnsupportedMedia: {http.StatusUnsupportedMediaType, "unsupported_media_type"}, + apperr.KindUnavailable: {http.StatusServiceUnavailable, "unavailable"}, +} + +// JSON is the single place that fixes the response media type; PureJSON +// keeps non-ASCII and HTML characters verbatim. +func JSON(c *gin.Context, status int, body any) { + c.PureJSON(status, body) +} + +// Fail writes the contract error body and stops the handler chain. +func Fail(c *gin.Context, status int, code, message string) { + JSON(c, status, errorEnvelope{Error: errorDetail{ + Code: code, + Message: message, + RequestID: RequestID(c), + }}) + c.Abort() +} + +// Error maps an application error to its status. Anything that is not an +// application error is logged with its text and reported as an opaque 500, +// so internal details never reach the client. +func Error(c *gin.Context, err error) { + mapped, known := kinds[apperr.KindOf(err)] + if !known { + logger(c).Error("request failed", "request_id", RequestID(c), "error", err.Error()) + Fail(c, http.StatusInternalServerError, "internal_error", "internal error") + return + } + Fail(c, mapped.status, mapped.code, err.Error()) +} + +// Timestamp renders RFC 3339 UTC with a fixed millisecond width: Python 3.9 +// (the oldest version the public tests support) only parses 3 or 6 fractional +// digits, and RFC3339Nano would trim trailing zeros to a variable width. +func Timestamp(t time.Time) string { + return t.UTC().Format("2006-01-02T15:04:05.000Z") +} diff --git a/internal/platform/ident/ident.go b/internal/platform/ident/ident.go new file mode 100644 index 00000000..121500d9 --- /dev/null +++ b/internal/platform/ident/ident.go @@ -0,0 +1,46 @@ +// Package ident generates and parses entity identifiers. +// +// Identifiers are UUIDv7 (RFC 9562): the first 48 bits carry a Unix +// millisecond timestamp and google/uuid keeps them strictly increasing within +// one process even when the system clock steps backwards, so lexicographic +// order of ids equals creation order. +package ident + +import ( + "errors" + "time" + + "github.com/google/uuid" +) + +var ErrInvalid = errors.New("invalid uuid") + +// New panics only if the platform random source is unavailable, which is a +// fatal environment fault rather than a request error. +func New() uuid.UUID { + id, err := uuid.NewV7() + if err != nil { + panic("ident: random source unavailable: " + err.Error()) + } + return id +} + +// TimeOf extracts the creation time carried by a UUIDv7 as UTC with +// millisecond precision. +func TimeOf(id uuid.UUID) time.Time { + sec, nsec := id.Time().UnixTime() + return time.Unix(sec, nsec).UTC() +} + +// Parse accepts only the canonical 36-character form; uuid.Parse alone would +// also take braces, urn prefixes and bare hex. +func Parse(raw string) (uuid.UUID, error) { + if len(raw) != 36 { + return uuid.Nil, ErrInvalid + } + id, err := uuid.Parse(raw) + if err != nil { + return uuid.Nil, ErrInvalid + } + return id, nil +} diff --git a/internal/platform/ident/ident_test.go b/internal/platform/ident/ident_test.go new file mode 100644 index 00000000..1ae020df --- /dev/null +++ b/internal/platform/ident/ident_test.go @@ -0,0 +1,72 @@ +package ident_test + +import ( + "bytes" + "testing" + "time" + + "messenger/internal/platform/ident" +) + +func TestNewProducesVersion7(t *testing.T) { + id := ident.New() + if got := id.Version(); got != 7 { + t.Fatalf("version = %d, want 7", got) + } +} + +func TestNewIsStrictlyIncreasingWithinProcess(t *testing.T) { + prev := ident.New() + for i := 0; i < 20000; i++ { + next := ident.New() + if bytes.Compare(next[:], prev[:]) <= 0 { + t.Fatalf("id %s is not greater than previous %s at iteration %d", next, prev, i) + } + prev = next + } +} + +func TestTimeOfMatchesWallClockAtMillisecondPrecision(t *testing.T) { + before := time.Now().Truncate(time.Millisecond) + id := ident.New() + after := time.Now().Add(time.Millisecond) + + got := ident.TimeOf(id) + if got.Location() != time.UTC { + t.Fatalf("TimeOf must return UTC, got %v", got.Location()) + } + if got.Before(before) || got.After(after) { + t.Fatalf("TimeOf = %v, want within [%v, %v]", got, before, after) + } + if got.Nanosecond()%int(time.Millisecond) != 0 { + t.Fatalf("TimeOf must be truncated to milliseconds, got %v", got) + } +} + +func TestParseAcceptsCanonicalForm(t *testing.T) { + id := ident.New() + parsed, err := ident.Parse(id.String()) + if err != nil { + t.Fatalf("Parse(%q) error: %v", id, err) + } + if parsed != id { + t.Fatalf("Parse round trip mismatch: %s != %s", parsed, id) + } +} + +func TestParseRejectsNonCanonicalForms(t *testing.T) { + id := ident.New().String() + for _, raw := range []string{ + "", + "not-a-uuid", + "{" + id + "}", + "urn:uuid:" + id, + id[:32] + id[33:], // hex without one dash + id + "0", + " " + id, + } { + if _, err := ident.Parse(raw); err == nil { + t.Errorf("Parse(%q) must fail", raw) + } + } +} diff --git a/internal/platform/pagination/cursor.go b/internal/platform/pagination/cursor.go new file mode 100644 index 00000000..65a09854 --- /dev/null +++ b/internal/platform/pagination/cursor.go @@ -0,0 +1,63 @@ +// Package pagination implements keyset paging. A cursor is opaque to the +// client and bound to the resource it pages (the chat for history, the user +// for the chat list), so a cursor from one resource is rejected on another. +// Because ids are UUIDv7 and creation time derives from the id, the keyset +// (created_at, id) collapses to the id of the last returned item. +package pagination + +import ( + "encoding/base64" + "errors" + "strconv" + + "github.com/google/uuid" +) + +const ( + DefaultLimit = 50 + MaxLimit = 100 +) + +var ( + ErrInvalidCursor = errors.New("invalid cursor") + ErrInvalidLimit = errors.New("limit must be an integer between 1 and 100") +) + +const cursorVersion = 1 + +func EncodeCursor(scope, last uuid.UUID) string { + raw := make([]byte, 0, 1+16+16) + raw = append(raw, cursorVersion) + raw = append(raw, scope[:]...) + raw = append(raw, last[:]...) + return base64.RawURLEncoding.EncodeToString(raw) +} + +func DecodeCursor(token string, scope uuid.UUID) (uuid.UUID, error) { + raw, err := base64.RawURLEncoding.DecodeString(token) + if err != nil || len(raw) != 1+16+16 || raw[0] != cursorVersion { + return uuid.Nil, ErrInvalidCursor + } + if got, err := uuid.FromBytes(raw[1:17]); err != nil || got != scope { + return uuid.Nil, ErrInvalidCursor + } + last, err := uuid.FromBytes(raw[17:]) + if err != nil { + return uuid.Nil, ErrInvalidCursor + } + return last, nil +} + +// ParseLimit accepts an unsigned decimal between 1 and MaxLimit; an empty +// value means DefaultLimit. ParseUint rejects signs, whitespace and +// fractions and reports overflow, so no length guard is needed. +func ParseLimit(raw string) (int, error) { + if raw == "" { + return DefaultLimit, nil + } + n, err := strconv.ParseUint(raw, 10, 32) + if err != nil || n < 1 || n > MaxLimit { + return 0, ErrInvalidLimit + } + return int(n), nil +} diff --git a/internal/platform/pagination/cursor_test.go b/internal/platform/pagination/cursor_test.go new file mode 100644 index 00000000..5c93a17c --- /dev/null +++ b/internal/platform/pagination/cursor_test.go @@ -0,0 +1,71 @@ +package pagination_test + +import ( + "errors" + "strings" + "testing" + + "messenger/internal/platform/ident" + "messenger/internal/platform/pagination" +) + +func TestCursorRoundTrip(t *testing.T) { + scope, last := ident.New(), ident.New() + encoded := pagination.EncodeCursor(scope, last) + if encoded == "" || strings.ContainsAny(encoded, "+/=") { + t.Fatalf("cursor must be non-empty URL-safe base64 without padding, got %q", encoded) + } + got, err := pagination.DecodeCursor(encoded, scope) + if err != nil { + t.Fatalf("DecodeCursor error: %v", err) + } + if got != last { + t.Fatalf("decoded %s, want %s", got, last) + } +} + +func TestDecodeCursorRejectsForeignScope(t *testing.T) { + encoded := pagination.EncodeCursor(ident.New(), ident.New()) + _, err := pagination.DecodeCursor(encoded, ident.New()) + if !errors.Is(err, pagination.ErrInvalidCursor) { + t.Fatalf("err = %v, want ErrInvalidCursor", err) + } +} + +func TestDecodeCursorRejectsGarbage(t *testing.T) { + scope := ident.New() + valid := pagination.EncodeCursor(scope, ident.New()) + for _, raw := range []string{"", "***", "abc", valid[:len(valid)-2], valid + "AA", strings.ToUpper(valid)} { + if _, err := pagination.DecodeCursor(raw, scope); !errors.Is(err, pagination.ErrInvalidCursor) { + t.Errorf("DecodeCursor(%q) err = %v, want ErrInvalidCursor", raw, err) + } + } +} + +func TestParseLimit(t *testing.T) { + cases := []struct { + raw string + want int + ok bool + }{ + {"", 50, true}, + {"1", 1, true}, + {"100", 100, true}, + {"0", 0, false}, + {"101", 0, false}, + {"-1", 0, false}, + {"+5", 0, false}, + {"5.0", 0, false}, + {"abc", 0, false}, + {" 5", 0, false}, + } + for _, tc := range cases { + got, err := pagination.ParseLimit(tc.raw) + if tc.ok && (err != nil || got != tc.want) { + t.Errorf("ParseLimit(%q) = %d, %v; want %d", tc.raw, got, err, tc.want) + } + if !tc.ok && !errors.Is(err, pagination.ErrInvalidLimit) { + t.Errorf("ParseLimit(%q) err = %v, want ErrInvalidLimit", tc.raw, err) + } + } +} diff --git a/internal/platform/pagination/page.go b/internal/platform/pagination/page.go new file mode 100644 index 00000000..f905be82 --- /dev/null +++ b/internal/platform/pagination/page.go @@ -0,0 +1,41 @@ +package pagination + +import "github.com/google/uuid" + +// Page is the service-level page: the items and the id to continue from, +// nil when the listing is exhausted. It carries the raw keyset id so that any +// transport can encode the continuation its own way. +type Page[T any] struct { + Items []T + Next *uuid.UUID +} + +// PageResponse is the wire form of a page with the continuation already +// encoded as an opaque cursor. +type PageResponse[T any] struct { + Items []T `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +// Cut turns a slice over-fetched by one item into a page of at most limit +// items; the extra item only proves that a next page exists. +func Cut[T any](items []T, limit int, id func(T) uuid.UUID) Page[T] { + if len(items) <= limit { + return Page[T]{Items: items} + } + items = items[:limit] + next := id(items[limit-1]) + return Page[T]{Items: items, Next: &next} +} + +func ToResponse[D, R any](p Page[D], scope uuid.UUID, convert func(D) R) PageResponse[R] { + out := PageResponse[R]{Items: make([]R, 0, len(p.Items))} + for _, item := range p.Items { + out.Items = append(out.Items, convert(item)) + } + if p.Next != nil { + cursor := EncodeCursor(scope, *p.Next) + out.NextCursor = &cursor + } + return out +} diff --git a/internal/platform/pagination/page_test.go b/internal/platform/pagination/page_test.go new file mode 100644 index 00000000..6c3b0272 --- /dev/null +++ b/internal/platform/pagination/page_test.go @@ -0,0 +1,60 @@ +package pagination_test + +import ( + "encoding/json" + "testing" + + "github.com/google/uuid" + + "messenger/internal/platform/ident" + "messenger/internal/platform/pagination" +) + +func self(id uuid.UUID) uuid.UUID { return id } + +func TestCutTrimsOverfetchAndDerivesNext(t *testing.T) { + ids := []uuid.UUID{ident.New(), ident.New(), ident.New()} + + fewer := pagination.Cut(ids[:2], 5, self) + if len(fewer.Items) != 2 || fewer.Next != nil { + t.Fatalf("fewer than limit: %+v", fewer) + } + exact := pagination.Cut(ids[:2], 2, self) + if len(exact.Items) != 2 || exact.Next != nil { + t.Fatalf("exactly limit must not continue: %+v", exact) + } + over := pagination.Cut(ids, 2, self) + if len(over.Items) != 2 || over.Next == nil || *over.Next != ids[1] { + t.Fatalf("over-fetched page must trim and continue from the last kept id: %+v", over) + } + if over.Items[0] != ids[0] || over.Items[1] != ids[1] { + t.Fatalf("order changed: %v", over.Items) + } +} + +func TestToResponseEncodesCursorWithinScope(t *testing.T) { + scope, last := ident.New(), ident.New() + page := pagination.Page[uuid.UUID]{Items: []uuid.UUID{last}, Next: &last} + res := pagination.ToResponse(page, scope, func(id uuid.UUID) string { return id.String() }) + if len(res.Items) != 1 || res.Items[0] != last.String() { + t.Fatalf("items = %v", res.Items) + } + if res.NextCursor == nil { + t.Fatal("next cursor missing") + } + got, err := pagination.DecodeCursor(*res.NextCursor, scope) + if err != nil || got != last { + t.Fatalf("cursor decodes to %s, %v; want %s", got, err, last) + } +} + +func TestToResponseSerializesEmptyPageAsContractRequires(t *testing.T) { + res := pagination.ToResponse(pagination.Page[uuid.UUID]{}, ident.New(), func(id uuid.UUID) string { return id.String() }) + raw, err := json.Marshal(res) + if err != nil { + t.Fatal(err) + } + if string(raw) != `{"items":[],"next_cursor":null}` { + t.Fatalf("empty page = %s", raw) + } +} diff --git a/internal/server/server.go b/internal/server/server.go new file mode 100644 index 00000000..d3774da7 --- /dev/null +++ b/internal/server/server.go @@ -0,0 +1,57 @@ +// Package server runs the HTTP listener with the lifecycle the lab +// requires: readiness drops first, the listener stops accepting, in-flight +// requests drain within a bounded timeout, then the process exits. +package server + +import ( + "context" + "errors" + "log/slog" + "net" + "net/http" + "time" + + "messenger/internal/app" +) + +// Run serves until ctx is cancelled, then shuts down within shutdownTimeout. +func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shutdownTimeout time.Duration) error { + srv := &http.Server{ + Handler: a.Engine, + ReadHeaderTimeout: 5 * time.Second, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 60 * time.Second, + MaxHeaderBytes: 64 * 1024, + ErrorLog: slog.NewLogLogger(log.Handler(), slog.LevelWarn), + } + + served := make(chan error, 1) + go func() { + err := srv.Serve(ln) + if errors.Is(err, http.ErrServerClosed) { + err = nil + } + served <- err + }() + log.Info("listening", "addr", ln.Addr().String()) + + select { + case err := <-served: + return err + case <-ctx.Done(): + } + + log.Info("shutdown started", "timeout", shutdownTimeout.String()) + a.Health.SetReady(false) + drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout) + defer cancel() + if err := srv.Shutdown(drain); err != nil { + log.Error("shutdown forced", "error", err.Error()) + _ = srv.Close() + return err + } + <-served + log.Info("shutdown completed") + return nil +} diff --git a/internal/server/server_test.go b/internal/server/server_test.go new file mode 100644 index 00000000..69ceb5ad --- /dev/null +++ b/internal/server/server_test.go @@ -0,0 +1,90 @@ +package server_test + +import ( + "context" + "io" + "log/slog" + "net" + "net/http" + "testing" + "time" + + "messenger/internal/app" + "messenger/internal/server" +) + +func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) { + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + a := app.New(log, 1024) + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }() + + base := "http://" + ln.Addr().String() + waitFor(t, base+"/health/ready", http.StatusOK) + + cancel() + select { + case err := <-done: + if err != nil { + t.Fatalf("Run returned error: %v", err) + } + case <-time.After(10 * time.Second): + t.Fatal("Run did not stop within 10s after cancel") + } + if _, err := http.Get(base + "/health/live"); err == nil { + t.Fatal("listener must be closed after shutdown") + } +} + +func TestReadinessDropsBeforeListenerCloses(t *testing.T) { + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + a := app.New(log, 1024) + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }() + base := "http://" + ln.Addr().String() + waitFor(t, base+"/health/ready", http.StatusOK) + + // Hold one request open so shutdown has something to drain. + slow := make(chan struct{}) + go func() { + resp, err := http.Get(base + "/health/live") + if err == nil { + resp.Body.Close() + } + close(slow) + }() + + cancel() + <-done + <-slow + if !a.Health.IsReady() { + return + } + t.Fatal("readiness must be switched off during shutdown") +} + +func waitFor(t *testing.T, url string, status int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for time.Now().Before(deadline) { + resp, err := http.Get(url) + if err == nil { + resp.Body.Close() + if resp.StatusCode == status { + return + } + } + time.Sleep(20 * time.Millisecond) + } + t.Fatalf("%s did not return %d in time", url, status) +} diff --git a/internal/user/handler.go b/internal/user/handler.go new file mode 100644 index 00000000..af8dcbdc --- /dev/null +++ b/internal/user/handler.go @@ -0,0 +1,70 @@ +package user + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "messenger/internal/auth" + "messenger/internal/platform/httpx" +) + +type Handler struct { + users *Service +} + +func NewHandler(users *Service) *Handler { + return &Handler{users: users} +} + +// Register mounts registration on the public router and the current-user +// endpoint on the router guarded by RequireUser. +func (h *Handler) Register(public, protected gin.IRouter) { + public.POST("/users", h.register) + protected.GET("/users/me", auth.WithUser(h.me)) +} + +type createRequest struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` +} + +type Response struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt string `json:"created_at"` +} + +func ToResponse(u User) Response { + return Response{ + ID: u.ID.String(), + Username: u.Username, + DisplayName: u.DisplayName, + CreatedAt: httpx.Timestamp(u.CreatedAt), + } +} + +func (h *Handler) register(c *gin.Context) { + var req createRequest + if err := httpx.DecodeJSON(c, &req); err != nil { + httpx.Error(c, err) + return + } + u, err := h.users.Register(c.Request.Context(), req.Username, req.DisplayName) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusCreated, ToResponse(u)) +} + +func (h *Handler) me(c *gin.Context, actor uuid.UUID) { + u, err := h.users.GetByID(c.Request.Context(), actor) + if err != nil { + httpx.Error(c, err) + return + } + httpx.JSON(c, http.StatusOK, ToResponse(u)) +} diff --git a/internal/user/repository.go b/internal/user/repository.go new file mode 100644 index 00000000..05098e7e --- /dev/null +++ b/internal/user/repository.go @@ -0,0 +1,17 @@ +package user + +import ( + "context" + + "github.com/google/uuid" +) + +type Repository interface { + // Create stores a new user or returns ErrUsernameTaken; the uniqueness + // check and the insert must be atomic. + Create(ctx context.Context, user User) error + GetByID(ctx context.Context, id uuid.UUID) (User, error) + // ExistingIDs returns the subset of ids that belong to registered users, + // in input order, in one round trip. + ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) +} diff --git a/internal/user/service.go b/internal/user/service.go new file mode 100644 index 00000000..0a177b5e --- /dev/null +++ b/internal/user/service.go @@ -0,0 +1,55 @@ +package user + +import ( + "context" + "fmt" + + "github.com/google/uuid" + + "messenger/internal/platform/ident" +) + +type Service struct { + repo Repository +} + +func NewService(repo Repository) *Service { + return &Service{repo: repo} +} + +func (s *Service) Register(ctx context.Context, username, displayName string) (User, error) { + u, err := New(ident.New(), username, displayName) + if err != nil { + return User{}, err + } + if err := s.repo.Create(ctx, u); err != nil { + return User{}, err + } + return u, nil +} + +func (s *Service) GetByID(ctx context.Context, id uuid.UUID) (User, error) { + return s.repo.GetByID(ctx, id) +} + +// Exists serves the identification middleware; it is the single-id form of +// ExistingIDs so existence is answered by one code path. +func (s *Service) Exists(ctx context.Context, id uuid.UUID) (bool, error) { + found, err := s.ExistingIDs(ctx, []uuid.UUID{id}) + if err != nil { + return false, err + } + return len(found) == 1, nil +} + +// ExistingIDs serves the chats module when it validates member lists. +func (s *Service) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { + if len(ids) == 0 { + return []uuid.UUID{}, nil + } + found, err := s.repo.ExistingIDs(ctx, ids) + if err != nil { + return nil, fmt.Errorf("user lookup: %w", err) + } + return found, nil +} diff --git a/internal/user/service_test.go b/internal/user/service_test.go new file mode 100644 index 00000000..2f5d0531 --- /dev/null +++ b/internal/user/service_test.go @@ -0,0 +1,85 @@ +package user_test + +import ( + "context" + "errors" + "testing" + + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" + "messenger/internal/user" + "messenger/internal/user/store" +) + +func newService() *user.Service { + return user.NewService(store.NewInMemUsers()) +} + +func TestRegisterStoresUser(t *testing.T) { + svc := newService() + ctx := context.Background() + u, err := svc.Register(ctx, "alice", "Алиса") + if err != nil { + t.Fatalf("Register error: %v", err) + } + if u.ID.Version() != 7 { + t.Fatalf("id must be UUIDv7, got version %d", u.ID.Version()) + } + got, err := svc.GetByID(ctx, u.ID) + if err != nil || got != u { + t.Fatalf("GetByID = %+v, %v; want %+v", got, err, u) + } + if ok, err := svc.Exists(ctx, u.ID); err != nil || !ok { + t.Fatalf("Exists = %v, %v; want true", ok, err) + } +} + +func TestRegisterRejectsInvalidInput(t *testing.T) { + _, err := newService().Register(context.Background(), "Bad Name", "x") + if apperr.KindOf(err) != apperr.KindInvalid { + t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err) + } +} + +func TestRegisterReportsDuplicateAsConflict(t *testing.T) { + svc := newService() + ctx := context.Background() + if _, err := svc.Register(ctx, "alice", "A"); err != nil { + t.Fatal(err) + } + _, err := svc.Register(ctx, "alice", "B") + if !errors.Is(err, user.ErrUsernameTaken) || apperr.KindOf(err) != apperr.KindConflict { + t.Fatalf("err = %v, want ErrUsernameTaken with KindConflict", err) + } +} + +func TestGetByIDUnknownIsNotFound(t *testing.T) { + svc := newService() + _, err := svc.GetByID(context.Background(), ident.New()) + if !errors.Is(err, user.ErrNotFound) || apperr.KindOf(err) != apperr.KindNotFound { + t.Fatalf("err = %v, want ErrNotFound with KindNotFound", err) + } + if ok, err := svc.Exists(context.Background(), ident.New()); err != nil || ok { + t.Fatalf("Exists = %v, %v; want false, nil", ok, err) + } +} + +func TestExistingIDsFiltersInOneCall(t *testing.T) { + svc := newService() + ctx := context.Background() + alice, _ := svc.Register(ctx, "alice", "A") + bob, _ := svc.Register(ctx, "bob", "B") + unknown := ident.New() + got, err := svc.ExistingIDs(ctx, []uuid.UUID{unknown, bob.ID, alice.ID}) + if err != nil { + t.Fatal(err) + } + if len(got) != 2 || got[0] != bob.ID || got[1] != alice.ID { + t.Fatalf("ExistingIDs = %v, want bob then alice", got) + } + if empty, err := svc.ExistingIDs(ctx, nil); err != nil || len(empty) != 0 { + t.Fatalf("ExistingIDs(nil) = %v, %v", empty, err) + } +} diff --git a/internal/user/store/inmem.go b/internal/user/store/inmem.go new file mode 100644 index 00000000..882e5331 --- /dev/null +++ b/internal/user/store/inmem.go @@ -0,0 +1,63 @@ +// Package store holds the repository implementations of the users module. +// inmem.go is the in-process one: state lives only in this process and +// disappears with it by design of the lab. +package store + +import ( + "context" + "sync" + + "github.com/google/uuid" + + "messenger/internal/user" +) + +type InMemUsers struct { + mu sync.RWMutex + byID map[uuid.UUID]user.User + byUsername map[string]struct{} +} + +var _ user.Repository = (*InMemUsers)(nil) + +func NewInMemUsers() *InMemUsers { + return &InMemUsers{ + byID: make(map[uuid.UUID]user.User), + byUsername: make(map[string]struct{}), + } +} + +// Create checks and inserts under the same write lock, so concurrent +// registrations of one username admit exactly one winner. +func (r *InMemUsers) Create(_ context.Context, u user.User) error { + r.mu.Lock() + defer r.mu.Unlock() + if _, taken := r.byUsername[u.Username]; taken { + return user.ErrUsernameTaken + } + r.byUsername[u.Username] = struct{}{} + r.byID[u.ID] = u + return nil +} + +func (r *InMemUsers) GetByID(_ context.Context, id uuid.UUID) (user.User, error) { + r.mu.RLock() + defer r.mu.RUnlock() + u, ok := r.byID[id] + if !ok { + return user.User{}, user.ErrNotFound + } + return u, nil +} + +func (r *InMemUsers) ExistingIDs(_ context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { + r.mu.RLock() + defer r.mu.RUnlock() + found := make([]uuid.UUID, 0, len(ids)) + for _, id := range ids { + if _, ok := r.byID[id]; ok { + found = append(found, id) + } + } + return found, nil +} diff --git a/internal/user/store/inmem_test.go b/internal/user/store/inmem_test.go new file mode 100644 index 00000000..7ebcdbef --- /dev/null +++ b/internal/user/store/inmem_test.go @@ -0,0 +1,97 @@ +package store_test + +import ( + "context" + "errors" + "sync" + "testing" + + "github.com/google/uuid" + + "messenger/internal/platform/ident" + "messenger/internal/user" + "messenger/internal/user/store" +) + +func mustUser(t *testing.T, username string) user.User { + t.Helper() + u, err := user.New(ident.New(), username, "Display") + if err != nil { + t.Fatal(err) + } + return u +} + +func TestCreateThenGetByID(t *testing.T) { + repo := store.NewInMemUsers() + ctx := context.Background() + u := mustUser(t, "alice") + if err := repo.Create(ctx, u); err != nil { + t.Fatalf("Create error: %v", err) + } + got, err := repo.GetByID(ctx, u.ID) + if err != nil { + t.Fatalf("GetByID error: %v", err) + } + if got != u { + t.Fatalf("GetByID = %+v, want %+v", got, u) + } +} + +func TestGetByIDUnknownReturnsErrNotFound(t *testing.T) { + _, err := store.NewInMemUsers().GetByID(context.Background(), ident.New()) + if !errors.Is(err, user.ErrNotFound) { + t.Fatalf("err = %v, want ErrNotFound", err) + } +} + +func TestCreateRejectsDuplicateUsername(t *testing.T) { + repo := store.NewInMemUsers() + ctx := context.Background() + if err := repo.Create(ctx, mustUser(t, "alice")); err != nil { + t.Fatal(err) + } + err := repo.Create(ctx, mustUser(t, "alice")) + if !errors.Is(err, user.ErrUsernameTaken) { + t.Fatalf("err = %v, want ErrUsernameTaken", err) + } +} + +func TestCreateIsAtomicUnderConcurrency(t *testing.T) { + repo := store.NewInMemUsers() + ctx := context.Background() + const workers = 50 + var wg sync.WaitGroup + var mu sync.Mutex + created := 0 + for i := 0; i < workers; i++ { + wg.Add(1) + go func() { + defer wg.Done() + if err := repo.Create(ctx, mustUser(t, "same_name")); err == nil { + mu.Lock() + created++ + mu.Unlock() + } + }() + } + wg.Wait() + if created != 1 { + t.Fatalf("exactly one Create must win, got %d", created) + } +} + +func TestExistingIDsReturnsOnlyKnownIDs(t *testing.T) { + repo := store.NewInMemUsers() + ctx := context.Background() + alice, bob := mustUser(t, "alice"), mustUser(t, "bob") + _ = repo.Create(ctx, alice) + _ = repo.Create(ctx, bob) + got, err := repo.ExistingIDs(ctx, []uuid.UUID{ident.New(), bob.ID, alice.ID, bob.ID}) + if err != nil { + t.Fatal(err) + } + if len(got) != 3 || got[0] != bob.ID || got[1] != alice.ID || got[2] != bob.ID { + t.Fatalf("ExistingIDs must keep input order and drop unknown ids, got %v", got) + } +} diff --git a/internal/user/user.go b/internal/user/user.go new file mode 100644 index 00000000..f3050c85 --- /dev/null +++ b/internal/user/user.go @@ -0,0 +1,51 @@ +// Package user is the users module. The entity mirrors the future table row +// and carries no behaviour; New only enforces input invariants and CreatedAt +// derives from the UUIDv7 id. +package user + +import ( + "fmt" + "regexp" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" +) + +const MaxDisplayNameCodePoints = 100 + +var ( + ErrNotFound = apperr.NotFound("user not found") + ErrUsernameTaken = apperr.Conflict("username already taken") +) + +var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +type User struct { + ID uuid.UUID + Username string + DisplayName string + CreatedAt time.Time +} + +func New(id uuid.UUID, username, displayName string) (User, error) { + if !usernamePattern.MatchString(username) { + return User{}, apperr.Invalid("username must match [a-z0-9_]{3,32}") + } + if strings.TrimSpace(displayName) == "" { + return User{}, apperr.Invalid("display_name must not be blank") + } + if utf8.RuneCountInString(displayName) > MaxDisplayNameCodePoints { + return User{}, apperr.Invalid(fmt.Sprintf("display_name must be at most %d characters", MaxDisplayNameCodePoints)) + } + return User{ + ID: id, + Username: username, + DisplayName: displayName, + CreatedAt: ident.TimeOf(id), + }, nil +} diff --git a/internal/user/user_test.go b/internal/user/user_test.go new file mode 100644 index 00000000..72bca255 --- /dev/null +++ b/internal/user/user_test.go @@ -0,0 +1,56 @@ +package user_test + +import ( + "strings" + "testing" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/ident" + "messenger/internal/user" +) + +func TestNewDerivesCreatedAtFromID(t *testing.T) { + id := ident.New() + u, err := user.New(id, "alice", "Алиса") + if err != nil { + t.Fatalf("New error: %v", err) + } + if u.ID != id || u.Username != "alice" || u.DisplayName != "Алиса" { + t.Fatalf("unexpected user %+v", u) + } + if !u.CreatedAt.Equal(ident.TimeOf(id)) { + t.Fatalf("CreatedAt = %v, want %v", u.CreatedAt, ident.TimeOf(id)) + } +} + +func TestNewValidatesUsername(t *testing.T) { + valid := []string{"abc", "a_1", strings.Repeat("a", 32), "user_2026"} + for _, name := range valid { + if _, err := user.New(ident.New(), name, "ok"); err != nil { + t.Errorf("username %q must be accepted: %v", name, err) + } + } + invalid := []string{"", "ab", strings.Repeat("a", 33), "Alice", "al ice", "алиса", "a-b", "a.b", " abc", "abc\n"} + for _, name := range invalid { + _, err := user.New(ident.New(), name, "ok") + if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "username") { + t.Errorf("username %q must be rejected as invalid naming the field, got %v", name, err) + } + } +} + +func TestNewValidatesDisplayNameByCodePoints(t *testing.T) { + valid := []string{"A", "Алиса", strings.Repeat("я", 100), "👋 hi"} + for _, name := range valid { + if _, err := user.New(ident.New(), "alice", name); err != nil { + t.Errorf("display_name %q must be accepted: %v", name, err) + } + } + invalid := []string{"", " ", "\t\n", strings.Repeat("я", 101)} + for _, name := range invalid { + _, err := user.New(ident.New(), "alice", name) + if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "display_name") { + t.Errorf("display_name %q must be rejected as invalid naming the field, got %v", name, err) + } + } +} diff --git a/student-tests/lifecycle.sh b/student-tests/lifecycle.sh new file mode 100644 index 00000000..0eda2169 --- /dev/null +++ b/student-tests/lifecycle.sh @@ -0,0 +1,62 @@ +#!/bin/sh +# Lifecycle proof for the in-memory design: +# 1. create a user, a chat and a message, read the message back; +# 2. stop the container with SIGTERM and record how the process shut down; +# 3. start the same container again and show the old X-User-Id is gone (401). +# Usage: BASE_URL=http://localhost:8080 CONTAINER=mtusi-messenger-lab2 sh student-tests/lifecycle.sh +set -eu +BASE_URL=${BASE_URL:-http://localhost:8080} +CONTAINER=${CONTAINER:-mtusi-messenger-lab2} +HERE=$(dirname "$0") + +json_field() { # json_field '' key -> first string value of key + printf '%s' "$1" | sed -n "s/.*\"$2\":\"\([^\"]*\)\".*/\1/p" | head -n 1 +} + +echo "== 1. write and read before restart ==" +user=$(curl -sS -X POST "$BASE_URL/api/v1/users" -H 'Content-Type: application/json' \ + -d "{\"username\":\"lifecycle_$(date +%s)\",\"display_name\":\"Lifecycle\"}") +user_id=$(json_field "$user" id) +[ -n "$user_id" ] || { echo "user was not created: $user" >&2; exit 1; } +chat=$(curl -sS -X POST "$BASE_URL/api/v1/chats" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \ + -d "{\"title\":\"lifecycle\",\"member_ids\":[\"$user_id\"]}") +chat_id=$(json_field "$chat" id) +[ -n "$chat_id" ] || { echo "chat was not created: $chat" >&2; exit 1; } +message=$(curl -sS -X POST "$BASE_URL/api/v1/chats/$chat_id/messages" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \ + -d '{"text":"before restart"}') +message_id=$(json_field "$message" id) +[ -n "$message_id" ] || { echo "message was not created: $message" >&2; exit 1; } +history=$(curl -sS "$BASE_URL/api/v1/chats/$chat_id/messages" -H "X-User-Id: $user_id") +case "$history" in + *"$message_id"*) echo "message $message_id is readable before restart" ;; + *) echo "message missing from history: $history" >&2; exit 1 ;; +esac + +echo "== 2. SIGTERM via docker stop (grace 10s) ==" +started=$(date +%s) +docker stop -t 10 "$CONTAINER" >/dev/null +finished=$(date +%s) +exit_code=$(docker inspect -f '{{.State.ExitCode}}' "$CONTAINER") +echo "docker stop took $((finished - started))s wall clock, container exit code $exit_code" +echo "last log lines:" +docker logs --tail 3 "$CONTAINER" 2>&1 | sed 's/^/ /' +if [ "$exit_code" != "0" ]; then + echo "process did not exit cleanly on SIGTERM" >&2 + exit 1 +fi + +echo "== 3. restart the same container: state must be gone ==" +if ! docker start "$CONTAINER" >/dev/null; then + echo "cannot start $CONTAINER again: run it without --rm so the stopped container survives docker stop" >&2 + exit 1 +fi +BASE_URL="$BASE_URL" sh "$HERE/wait-ready.sh" >/dev/null +me_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/users/me" -H "X-User-Id: $user_id") +chat_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/chats/$chat_id" -H "X-User-Id: $user_id") +echo "GET /users/me with the old X-User-Id -> $me_status" +echo "GET /chats/$chat_id with the old X-User-Id -> $chat_status" +if [ "$me_status" != "401" ] || [ "$chat_status" != "401" ]; then + echo "expected 401 for both after restart" >&2 + exit 1 +fi +echo "PASS: state lived only in the process memory and was lost on restart" diff --git a/student-tests/wait-ready.sh b/student-tests/wait-ready.sh new file mode 100644 index 00000000..001f32a6 --- /dev/null +++ b/student-tests/wait-ready.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# Waits until GET /health/ready answers 200 or the attempt budget runs out. +# Usage: BASE_URL=http://localhost:8080 ATTEMPTS=60 sh student-tests/wait-ready.sh +set -eu +BASE_URL=${BASE_URL:-http://localhost:8080} +ATTEMPTS=${ATTEMPTS:-60} +i=0 +while [ "$i" -lt "$ATTEMPTS" ]; do + if curl -sf "$BASE_URL/health/ready" >/dev/null 2>&1; then + echo "ready: $BASE_URL" + exit 0 + fi + i=$((i + 1)) + sleep 0.5 +done +echo "not ready after $ATTEMPTS attempts: $BASE_URL" >&2 +exit 1 diff --git a/tools/concurrency/client.go b/tools/concurrency/client.go new file mode 100644 index 00000000..46c2c74b --- /dev/null +++ b/tools/concurrency/client.go @@ -0,0 +1,67 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "time" +) + +// client is a minimal typed HTTP client for the messenger API. +type client struct { + base string + http *http.Client +} + +func newClient(baseURL string) *client { + return &client{base: baseURL, http: &http.Client{Timeout: 10 * time.Second}} +} + +func (c *client) post(path, actor string, body, out any) error { + raw, err := json.Marshal(body) + if err != nil { + return err + } + req, err := http.NewRequest(http.MethodPost, c.base+path, bytes.NewReader(raw)) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + return c.do(req, actor, http.StatusCreated, out) +} + +// get requests the largest page the contract allows, continuing from cursor +// when it is set. +func (c *client) get(path, actor, cursor string, out any) error { + query := url.Values{"limit": {"100"}} + if cursor != "" { + query.Set("cursor", cursor) + } + req, err := http.NewRequest(http.MethodGet, c.base+path+"?"+query.Encode(), nil) + if err != nil { + return err + } + return c.do(req, actor, http.StatusOK, out) +} + +func (c *client) do(req *http.Request, actor string, want int, out any) error { + if actor != "" { + req.Header.Set("X-User-Id", actor) + } + resp, err := c.http.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + payload, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20)) + if err != nil { + return err + } + if resp.StatusCode != want { + return fmt.Errorf("%s %s: status %d, want %d: %s", req.Method, req.URL.Path, resp.StatusCode, want, payload) + } + return json.Unmarshal(payload, out) +} diff --git a/tools/concurrency/main.go b/tools/concurrency/main.go new file mode 100644 index 00000000..3d9fc4d0 --- /dev/null +++ b/tools/concurrency/main.go @@ -0,0 +1,216 @@ +// Command concurrency is the black-box race check from the grading rubric: +// N clients send M messages each into one chat at the same time, then the +// history is paged and must contain exactly N*M unique messages in id order. +package main + +import ( + "encoding/json" + "flag" + "fmt" + "os" + "path/filepath" + "sort" + "sync" + "time" + + "github.com/google/uuid" + + "messenger/internal/chat" + "messenger/internal/platform/pagination" + "messenger/internal/user" +) + +// Report is the machine readable outcome saved as evidence. +type Report struct { + BaseURL string `json:"base_url"` + StartedAt time.Time `json:"started_at"` + Clients int `json:"clients"` + PerClient int `json:"per_client"` + Expected int `json:"expected_messages"` + Created int `json:"created_201"` + Failed int `json:"failed_posts"` + Listed int `json:"listed_messages"` + Unique int `json:"unique_ids"` + Missing int `json:"created_but_not_listed"` + Ordered bool `json:"history_ordered_by_id"` + Pages int `json:"pages_read"` + ElapsedSeconds float64 `json:"elapsed_seconds"` + Passed bool `json:"passed"` +} + +func main() { + baseURL := flag.String("base-url", "http://localhost:8080", "API base URL") + clients := flag.Int("clients", 10, "number of concurrent clients") + perClient := flag.Int("per-client", 10, "messages sent by each client") + output := flag.String("output", "", "optional path for the JSON report") + flag.Parse() + + report, err := run(*baseURL, *clients, *perClient) + if err != nil { + fail(err) + } + text, _ := json.MarshalIndent(report, "", " ") + fmt.Println(string(text)) + if *output != "" { + if err := writeReport(*output, text); err != nil { + fail(err) + } + } + if !report.Passed { + fail(fmt.Errorf("concurrency invariants violated")) + } + fmt.Printf("PASS: %d clients x %d messages produced exactly %d unique ordered messages\n", + report.Clients, report.PerClient, report.Unique) +} + +func fail(err error) { + fmt.Fprintln(os.Stderr, "FAIL:", err) + os.Exit(1) +} + +func writeReport(path string, text []byte) error { + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return err + } + return os.WriteFile(path, append(text, '\n'), 0o644) +} + +func run(baseURL string, clients, perClient int) (Report, error) { + if clients < 1 || perClient < 1 { + return Report{}, fmt.Errorf("clients and per-client must be positive") + } + report := Report{ + BaseURL: baseURL, + StartedAt: time.Now().UTC(), + Clients: clients, + PerClient: perClient, + Expected: clients * perClient, + } + api := newClient(baseURL) + runID := uuid.NewString()[:8] + + members, err := registerClients(api, runID, clients) + if err != nil { + return report, err + } + room, err := createChat(api, runID, members) + if err != nil { + return report, err + } + + started := time.Now() + created, failed := sendConcurrently(baseURL, room.ID, members, perClient) + report.ElapsedSeconds = time.Since(started).Seconds() + report.Created = len(created) + report.Failed = failed + + listed, pages, err := readHistory(api, room.ID, members[0]) + if err != nil { + return report, err + } + report.Pages = pages + report.Listed = len(listed) + report.Unique, report.Missing = compare(created, listed) + report.Ordered = sort.StringsAreSorted(listed) + report.Passed = report.Failed == 0 && + report.Created == report.Expected && + report.Listed == report.Expected && + report.Unique == report.Expected && + report.Missing == 0 && + report.Ordered + return report, nil +} + +func registerClients(api *client, runID string, n int) ([]string, error) { + ids := make([]string, 0, n) + for i := range n { + var created user.Response + body := map[string]string{ + "username": fmt.Sprintf("load_%s_%d", runID, i), + "display_name": fmt.Sprintf("Load client %d", i), + } + if err := api.post("/api/v1/users", "", body, &created); err != nil { + return nil, fmt.Errorf("register client %d: %w", i, err) + } + ids = append(ids, created.ID) + } + return ids, nil +} + +func createChat(api *client, runID string, members []string) (chat.ChatResponse, error) { + var created chat.ChatResponse + body := map[string]any{"title": "Concurrency check " + runID, "member_ids": members} + if err := api.post("/api/v1/chats", members[0], body, &created); err != nil { + return chat.ChatResponse{}, fmt.Errorf("create chat: %w", err) + } + return created, nil +} + +// sendConcurrently gives every member its own HTTP client and releases them +// together, so the server sees genuinely parallel writes to one chat. +func sendConcurrently(baseURL, chatID string, members []string, perClient int) (map[string]struct{}, int) { + var mu sync.Mutex + created := make(map[string]struct{}, len(members)*perClient) + failed := 0 + + var wg sync.WaitGroup + start := make(chan struct{}) + for i, actor := range members { + wg.Add(1) + go func(n int, actor string) { + defer wg.Done() + own := newClient(baseURL) + <-start + for m := range perClient { + var msg chat.MessageResponse + body := map[string]string{"text": fmt.Sprintf("client %d message %d", n, m)} + err := own.post("/api/v1/chats/"+chatID+"/messages", actor, body, &msg) + mu.Lock() + if err != nil { + failed++ + } else { + created[msg.ID] = struct{}{} + } + mu.Unlock() + } + }(i, actor) + } + close(start) + wg.Wait() + return created, failed +} + +func readHistory(api *client, chatID, actor string) ([]string, int, error) { + var ids []string + pages := 0 + cursor := "" + for { + var page pagination.PageResponse[chat.MessageResponse] + if err := api.get("/api/v1/chats/"+chatID+"/messages", actor, cursor, &page); err != nil { + return nil, pages, fmt.Errorf("read history: %w", err) + } + pages++ + for _, m := range page.Items { + ids = append(ids, m.ID) + } + if page.NextCursor == nil { + return ids, pages, nil + } + cursor = *page.NextCursor + } +} + +// compare returns how many distinct ids the history holds and how many +// acknowledged ids never showed up in it. +func compare(created map[string]struct{}, listed []string) (unique, missing int) { + seen := make(map[string]struct{}, len(listed)) + for _, id := range listed { + seen[id] = struct{}{} + } + for id := range created { + if _, ok := seen[id]; !ok { + missing++ + } + } + return len(seen), missing +} diff --git a/tools/concurrency/main_test.go b/tools/concurrency/main_test.go new file mode 100644 index 00000000..8b64e201 --- /dev/null +++ b/tools/concurrency/main_test.go @@ -0,0 +1,36 @@ +package main + +import ( + "io" + "log/slog" + "net/http/httptest" + "testing" + + "messenger/internal/app" +) + +func TestRunAgainstLiveServer(t *testing.T) { + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + srv := httptest.NewServer(app.New(log, 64*1024).Engine) + defer srv.Close() + + report, err := run(srv.URL, 10, 10) + if err != nil { + t.Fatalf("run error: %v", err) + } + if report.Clients != 10 || report.PerClient != 10 || report.Expected != 100 { + t.Fatalf("report = %+v", report) + } + if report.Created != 100 || report.Listed != 100 || report.Unique != 100 || report.Missing != 0 { + t.Fatalf("report = %+v", report) + } + if !report.Ordered || !report.Passed { + t.Fatalf("report = %+v", report) + } +} + +func TestRunFailsOnUnreachableServer(t *testing.T) { + if _, err := run("http://127.0.0.1:1", 2, 2); err == nil { + t.Fatal("expected an error for an unreachable server") + } +} -- 2.54.0 From a552f15aa70aed04766e599d8e429b1b01fed18e Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 14:46:31 +0300 Subject: [PATCH 02/21] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=81=D1=85=D0=B5=D0=BC=D1=83=20PostgreSQL,=20?= =?UTF-8?q?=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D0=BE=D0=B5=20=D0=BE=D0=BA?= =?UTF-8?q?=D1=80=D1=83=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 6 + go.mod | 81 ++++++- go.sum | 251 ++++++++++++++++++-- internal/platform/postgres/postgres.go | 79 ++++++ internal/platform/postgres/postgres_test.go | 135 +++++++++++ internal/testenv/testenv.go | 128 ++++++++++ internal/testenv/testenv_test.go | 29 +++ migrations/00001_init.sql | 40 ++++ migrations/embed.go | 8 + 9 files changed, 731 insertions(+), 26 deletions(-) create mode 100644 .gitattributes create mode 100644 internal/platform/postgres/postgres.go create mode 100644 internal/platform/postgres/postgres_test.go create mode 100644 internal/testenv/testenv.go create mode 100644 internal/testenv/testenv_test.go create mode 100644 migrations/00001_init.sql create mode 100644 migrations/embed.go diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..1af1a8fe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# Files read inside Linux containers or by sh must keep LF on Windows +# checkouts with core.autocrlf=true. +*.sh text eol=lf +*.conf text eol=lf +*.sql text eol=lf +*.cmd text eol=crlf diff --git a/go.mod b/go.mod index 00dce21a..049a6502 100644 --- a/go.mod +++ b/go.mod @@ -5,36 +5,103 @@ go 1.27.1 require ( github.com/gin-gonic/gin v1.12.0 github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.11.0 + github.com/pressly/goose/v3 v3.27.0 + github.com/redis/go-redis/v9 v9.23.0 + github.com/testcontainers/testcontainers-go v0.39.0 + github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 + github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0 ) require ( + dario.cat/mergo v1.0.2 // indirect + github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect + github.com/Microsoft/go-winio v0.6.2 // indirect github.com/bytedance/gopkg v0.1.3 // indirect github.com/bytedance/sonic v1.15.0 // indirect github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect + github.com/containerd/errdefs v1.0.0 // indirect + github.com/containerd/errdefs/pkg v0.3.0 // indirect + github.com/containerd/log v0.1.0 // indirect + github.com/containerd/platforms v0.2.1 // indirect + github.com/cpuguy83/dockercfg v0.3.2 // indirect + github.com/creack/pty v1.1.24 // indirect + github.com/distribution/reference v0.6.0 // indirect + github.com/docker/docker v28.3.3+incompatible // indirect + github.com/docker/go-connections v0.6.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/ebitengine/purego v0.10.1 // indirect + github.com/felixge/httpsnoop v1.1.0 // indirect github.com/gabriel-vasile/mimetype v1.4.12 // indirect github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-logr/logr v1.4.4 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.30.1 // indirect github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.19.2 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/klauspost/compress v1.19.2 // indirect + github.com/klauspost/cpuid/v2 v2.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect + github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e // indirect + github.com/magiconair/properties v1.8.10 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/mdelapenya/tlscert v0.2.0 // indirect + github.com/mfridman/interpolate v0.0.2 // indirect + github.com/moby/docker-image-spec v1.3.1 // indirect + github.com/moby/go-archive v0.2.0 // indirect + github.com/moby/patternmatcher v0.6.1 // indirect + github.com/moby/sys/sequential v0.7.0 // indirect + github.com/moby/sys/user v0.4.0 // indirect + github.com/moby/sys/userns v0.1.0 // indirect + github.com/moby/term v0.5.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/morikuni/aec v1.0.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.1.1 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.59.0 // indirect + github.com/sethvargo/go-retry v0.4.0 // indirect + github.com/shirou/gopsutil/v4 v4.26.6 // indirect + github.com/sirupsen/logrus v1.9.4 // indirect + github.com/stretchr/testify v1.12.1 // indirect + github.com/tklauser/go-sysconf v0.4.0 // indirect + github.com/tklauser/numcpus v0.12.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.1 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + go.opentelemetry.io/auto/sdk v1.2.1 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 // indirect + go.opentelemetry.io/otel v1.46.0 // indirect + go.opentelemetry.io/otel/metric v1.46.0 // indirect + go.opentelemetry.io/otel/trace v1.46.0 // indirect + go.uber.org/atomic v1.12.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.yaml.in/yaml/v3 v3.0.5 // indirect golang.org/x/arch v0.22.0 // indirect - golang.org/x/crypto v0.48.0 // indirect - golang.org/x/net v0.51.0 // indirect - golang.org/x/sys v0.41.0 // indirect - golang.org/x/text v0.34.0 // indirect - google.golang.org/protobuf v1.36.10 // indirect + golang.org/x/crypto v0.57.0 // indirect + golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect + golang.org/x/net v0.59.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect + golang.org/x/time v0.11.0 // indirect + google.golang.org/grpc v1.84.0 // indirect + google.golang.org/protobuf v1.36.12 // indirect ) diff --git a/go.sum b/go.sum index 0b62b839..fb12767b 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,70 @@ +dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8= +dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= +github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg= +github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI= +github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= +github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= +github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= +github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= +github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= +github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A= +github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw= +github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA= +github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= +github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= +github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= +github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/docker v28.3.3+incompatible h1:Dypm25kh4rmk49v1eiVbsAtpAsYURjYkaKubwuBdxEI= +github.com/docker/docker v28.3.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94= +github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY= +github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= +github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8= +github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= @@ -27,65 +77,228 @@ github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 h1:Bd7KaOxzULLxtZ/K5s1aLbWhR0+5RToO65TXHsf3bqQ= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0/go.mod h1:nN7ts3dFXKtCZWc//yfkpcQNKJABg16/uDVAZpLDalo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= -github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8= +github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= -github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= -github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e h1:Q6MvJtQK/iRcRtzAscm/zF23XxJlbECiGPyRicsX+Ak= +github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= +github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE= +github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= +github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= +github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= +github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= +github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8= +github.com/moby/go-archive v0.2.0/go.mod h1:mNeivT14o8xU+5q1YnNrkQVpK+dnNe/K6fHqnTg4qPU= +github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U= +github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= +github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw= +github.com/moby/sys/atomicwriter v0.1.0/go.mod h1:Ul8oqv2ZMNHOceF643P6FKPXeCmYtlQMvpizfsSoaWs= +github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8= +github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o= +github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs= +github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs= +github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g= +github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28= +github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ= +github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= +github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= +github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/pressly/goose/v3 v3.27.0 h1:/D30gVTuQhu0WsNZYbJi4DMOsx1lNq+6SkLe+Wp59BM= +github.com/pressly/goose/v3 v3.27.0/go.mod h1:3ZBeCXqzkgIRvrEMDkYh1guvtoJTU5oMMuDdkutoM78= github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s= +github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/sethvargo/go-retry v0.4.0 h1:9qy1OoIAxBL+gBYnkTnTnWle5wlfsXQlwRzIbbpdqPw= +github.com/sethvargo/go-retry v0.4.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg= +github.com/shirou/gopsutil/v4 v4.26.6 h1:Mzr/npDtQC/xpeEuQKHZt8Zo9CmPvhTj8nkR8w5TLDs= +github.com/shirou/gopsutil/v4 v4.26.6/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ= +github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= +github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4= +github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= -github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/testcontainers/testcontainers-go v0.39.0 h1:uCUJ5tA+fcxbFAB0uP3pIK3EJ2IjjDUHFSZ1H1UxAts= +github.com/testcontainers/testcontainers-go v0.39.0/go.mod h1:qmHpkG7H5uPf/EvOORKvS6EuDkBUPE3zpVGaH9NL7f8= +github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 h1:REJz+XwNpGC/dCgTfYvM4SKqobNqDBfvhq74s2oHTUM= +github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0/go.mod h1:4K2OhtHEeT+JSIFX4V8DkGKsyLa96Y2vLdd3xsxD5HE= +github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0 h1:b5BwugyCMHrFfZYCSFKY9IHMhoWtR+REcN2ddcCQv6g= +github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0/go.mod h1:lgNiE/W5RPjOu1S8fZ9Z9e63JU8wyaxNQ43Umv6kroQ= +github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= +github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= +github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= +github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/valkey-io/valkey-go v1.0.59 h1:W67Z0UY+Qqk3k8NKkFCFlM3X4yQUniixl7dSJAch2Qo= +github.com/valkey-io/valkey-go v1.0.59/go.mod h1:bHmwjIEOrGq/ubOJfh5uMRs7Xj6mV3mQ/ZXUbmqpjqY= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= +github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= +go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 h1:3g7B90UzBltIDKq1/5mrTGxTnOFDV0ICOhLoxiZ8jlg= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0/go.mod h1:Ef8SuTh59BT7+ofpDxN9z+yOlc4t2GjLmKDgYNJL/NU= +go.opentelemetry.io/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc= +go.opentelemetry.io/otel v1.46.0/go.mod h1:Gj3SEScelsNC45tp4nSxRYlS+f5iez7W8XPMCt905kE= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU= +go.opentelemetry.io/otel/metric v1.46.0 h1:yBnkXvgV7AXFILZc5K6IZe/CBFF3OS7BJ8ov6/lj0K8= +go.opentelemetry.io/otel/metric v1.46.0/go.mod h1:iPmdWqifKUdzziPkvvzIJXITl56fQx2mGM/DHLB3/2o= +go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI= +go.opentelemetry.io/otel/sdk v1.46.0/go.mod h1:GAERFXFt5SYCEB+YiKUbMBeza6UaDH7GmGOZEfh2gSM= +go.opentelemetry.io/otel/sdk/metric v1.46.0 h1:0piZ26EG4RBfebb2jhDH6ERCYHoVWduc3kLgPCwSnSE= +go.opentelemetry.io/otel/sdk/metric v1.46.0/go.mod h1:I1PbKrdVc8Qu8HYVDNtqVIwLwjNrhsV/uFuxfwg8mO4= +go.opentelemetry.io/otel/trace v1.46.0 h1:OULy7ccdJnZtJ0UDYFOIGaCmiWzJ8Vi2G/Rsu60qs1c= +go.opentelemetry.io/otel/trace v1.46.0/go.mod h1:J7GAXweO77XSFkB/rmAqk9D6ihszhFjLU+d9WuUxDLI= +go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I= +go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM= +go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE= +go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk= go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= -golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= -golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= -golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= -golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= -golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= -golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= -golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= -google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= -google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba h1:Ck8QetSgk912qxWLMCKxd0in+aiyBQyDSMae6e/xmpU= +golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba/go.mod h1:50RgIsmK7OwqzTTeqcSXQW8SswW0o8fRcDxmqGluJ8E= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= +golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE= +golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= +golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459 h1:GS9OIt/j7c8bvBjYNgnKQysVfmV7e4jM0H8ZK95G4t8= +google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459/go.mod h1:PX5/4vemwVoXtwEcRDWwcR1/r0qrosfx3qoVADMwnVE= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679 h1:KmqdJU4vrNcxy/6qdg3JduZtalEXrJLspVltnR1cE+8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc= +google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0= +google.golang.org/grpc v1.84.0/go.mod h1:ljCht0DrxQrXBDRTZp52Qxh3Ffk8CdYm2sj4O2QN2C0= +google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= +google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= +gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA= +modernc.org/libc v1.68.0 h1:PJ5ikFOV5pwpW+VqCK1hKJuEWsonkIJhhIXyuF/91pQ= +modernc.org/libc v1.68.0/go.mod h1:NnKCYeoYgsEqnY3PgvNgAeaJnso968ygU8Z0DxjoEc0= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= +modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/sqlite v1.46.1 h1:eFJ2ShBLIEnUWlLy12raN0Z1plqmFX9Qe3rjQTKt6sU= +modernc.org/sqlite v1.46.1/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA= diff --git a/internal/platform/postgres/postgres.go b/internal/platform/postgres/postgres.go new file mode 100644 index 00000000..8ab3507b --- /dev/null +++ b/internal/platform/postgres/postgres.go @@ -0,0 +1,79 @@ +// Package postgres opens the connection pool and applies the schema. +// +// Pool size and server-side timeouts are constants rather than settings: they +// bound how long one request can hold a connection, and the request deadline +// of the API is chosen against them. +package postgres + +import ( + "context" + "errors" + "fmt" + "strconv" + "time" + + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/pressly/goose/v3" + "github.com/pressly/goose/v3/lock" + + "messenger/migrations" +) + +const ( + maxConns = 10 + connectTimeout = 3 * time.Second + statementTimeout = 5 * time.Second + idleInTxTimeout = 10 * time.Second +) + +const codeUniqueViolation = "23505" + +func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) { + cfg, err := pgxpool.ParseConfig(dsn) + if err != nil { + return nil, fmt.Errorf("parse database url: %w", err) + } + cfg.MaxConns = maxConns + cfg.ConnConfig.ConnectTimeout = connectTimeout + cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10) + cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10) + + pool, err := pgxpool.NewWithConfig(ctx, cfg) + if err != nil { + return nil, fmt.Errorf("create pool: %w", err) + } + if err := pool.Ping(ctx); err != nil { + pool.Close() + return nil, fmt.Errorf("ping database: %w", err) + } + return pool, nil +} + +// Migrate applies pending migrations. A session advisory lock serialises +// concurrent starts, so several API replicas may call it at once; applied +// versions are recorded, which makes a repeated run a no-op. +func Migrate(ctx context.Context, pool *pgxpool.Pool) error { + db := stdlib.OpenDBFromPool(pool) + defer db.Close() + locker, err := lock.NewPostgresSessionLocker() + if err != nil { + return fmt.Errorf("migration lock: %w", err) + } + provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.FS, goose.WithSessionLocker(locker)) + if err != nil { + return fmt.Errorf("load migrations: %w", err) + } + if _, err := provider.Up(ctx); err != nil { + return fmt.Errorf("apply migrations: %w", err) + } + return nil +} + +// IsUniqueViolation reports whether err is a unique violation of the named +// constraint. +func IsUniqueViolation(err error, constraint string) bool { + pgErr, ok := errors.AsType[*pgconn.PgError](err) + return ok && pgErr.Code == codeUniqueViolation && pgErr.ConstraintName == constraint +} diff --git a/internal/platform/postgres/postgres_test.go b/internal/platform/postgres/postgres_test.go new file mode 100644 index 00000000..68f67ea5 --- /dev/null +++ b/internal/platform/postgres/postgres_test.go @@ -0,0 +1,135 @@ +package postgres_test + +import ( + "context" + "errors" + "fmt" + "os" + "testing" + + "messenger/internal/platform/postgres" + "messenger/internal/testenv" +) + +var db *testenv.DB + +func TestMain(m *testing.M) { + var err error + db, err = testenv.StartPostgres(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + db.Close() + os.Exit(code) +} + +func TestMigrateIsRepeatable(t *testing.T) { + ctx := context.Background() + if err := postgres.Migrate(ctx, db.Pool); err != nil { + t.Fatalf("second migrate: %v", err) + } + var version int64 + if err := db.Pool.QueryRow(ctx, `SELECT max(version_id) FROM goose_db_version`).Scan(&version); err != nil { + t.Fatal(err) + } + if version != 1 { + t.Fatalf("schema version = %d, want 1", version) + } + for _, table := range []string{"users", "chats", "chat_members", "messages"} { + var exists bool + if err := db.Pool.QueryRow(ctx, `SELECT to_regclass('public.' || $1) IS NOT NULL`, table).Scan(&exists); err != nil { + t.Fatal(err) + } + if !exists { + t.Fatalf("table %s is missing", table) + } + } +} + +func TestPoolAppliesLimitsAndTimeouts(t *testing.T) { + if got := db.Pool.Config().MaxConns; got != 10 { + t.Fatalf("MaxConns = %d, want 10", got) + } + ctx := context.Background() + for setting, want := range map[string]string{ + "statement_timeout": "5s", + "idle_in_transaction_session_timeout": "10s", + } { + var got string + if err := db.Pool.QueryRow(ctx, "SHOW "+setting).Scan(&got); err != nil { + t.Fatal(err) + } + if got != want { + t.Fatalf("%s = %q, want %q", setting, got, want) + } + } +} + +func TestResetEmptiesEveryTableButKeepsSchemaVersion(t *testing.T) { + ctx := context.Background() + _, err := db.Pool.Exec(ctx, ` + WITH u AS ( + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ('0190f1d2-0000-7000-8000-000000000001', 'reset_check', 'Reset', 'x', now()) + RETURNING id + ), c AS ( + INSERT INTO chats (id, title, created_at) + VALUES ('0190f1d2-0000-7000-8000-000000000002', 'Reset', now()) + RETURNING id + ), m AS ( + INSERT INTO chat_members (chat_id, user_id) SELECT c.id, u.id FROM c, u + ) + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + SELECT '0190f1d2-0000-7000-8000-000000000003', c.id, u.id, 'hi', now() FROM c, u`) + if err != nil { + t.Fatal(err) + } + + db.Reset(t) + + for _, table := range []string{"users", "chats", "chat_members", "messages"} { + var n int + if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM "+table).Scan(&n); err != nil { + t.Fatal(err) + } + if n != 0 { + t.Fatalf("%s has %d rows after Reset", table, n) + } + } + var versions int + if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM goose_db_version`).Scan(&versions); err != nil { + t.Fatal(err) + } + if versions == 0 { + t.Fatal("Reset wiped the schema version table") + } +} + +func TestIsUniqueViolation(t *testing.T) { + db.Reset(t) + ctx := context.Background() + insert := `INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1, 'taken', 'Taken', 'x', now())` + if _, err := db.Pool.Exec(ctx, insert, "0190f1d2-0000-7000-8000-000000000011"); err != nil { + t.Fatal(err) + } + _, dup := db.Pool.Exec(ctx, insert, "0190f1d2-0000-7000-8000-000000000012") + if dup == nil { + t.Fatal("duplicate username was accepted") + } + + if !postgres.IsUniqueViolation(dup, "users_username_key") { + t.Fatalf("not recognised as a username violation: %v", dup) + } + if postgres.IsUniqueViolation(dup, "some_other_key") { + t.Fatal("matched a different constraint") + } + if postgres.IsUniqueViolation(fmt.Errorf("wrapped: %w", dup), "users_username_key") == false { + t.Fatal("wrapped error not recognised") + } + if postgres.IsUniqueViolation(errors.New("plain"), "users_username_key") || postgres.IsUniqueViolation(nil, "users_username_key") { + t.Fatal("non-database error recognised as a violation") + } +} diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go new file mode 100644 index 00000000..974651e9 --- /dev/null +++ b/internal/testenv/testenv.go @@ -0,0 +1,128 @@ +// Package testenv starts disposable PostgreSQL and Valkey containers for tests +// that exercise the real stores. Only _test files import it, so it never +// reaches the server binary. The images match compose.yaml. +package testenv + +import ( + "context" + "fmt" + "testing" + + "github.com/jackc/pgx/v5/pgxpool" + "github.com/redis/go-redis/v9" + "github.com/testcontainers/testcontainers-go" + tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres" + tcvalkey "github.com/testcontainers/testcontainers-go/modules/valkey" + + "messenger/internal/platform/postgres" +) + +const ( + PostgresImage = "postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873" + ValkeyImage = "valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11" +) + +// DB is a migrated PostgreSQL instance. +type DB struct { + Pool *pgxpool.Pool + DSN string + container *tcpostgres.PostgresContainer +} + +func StartPostgres(ctx context.Context) (*DB, error) { + c, err := tcpostgres.Run(ctx, PostgresImage, + tcpostgres.WithDatabase("messenger"), + tcpostgres.WithUsername("messenger"), + tcpostgres.WithPassword("messenger"), + tcpostgres.BasicWaitStrategies(), + ) + if err != nil { + return nil, fmt.Errorf("start postgres: %w", err) + } + db := &DB{container: c} + db.DSN, err = c.ConnectionString(ctx, "sslmode=disable") + if err != nil { + db.Close() + return nil, fmt.Errorf("postgres address: %w", err) + } + db.Pool, err = postgres.NewPool(ctx, db.DSN) + if err != nil { + db.Close() + return nil, err + } + if err := postgres.Migrate(ctx, db.Pool); err != nil { + db.Close() + return nil, err + } + return db, nil +} + +// Reset empties every application table, so each test starts from an empty +// schema. The table list comes from the catalog: a table added by a later +// migration cannot be forgotten here. +func (d *DB) Reset(t testing.TB) { + t.Helper() + ctx := context.Background() + var tables string + err := d.Pool.QueryRow(ctx, ` + SELECT coalesce(string_agg(format('%I.%I', schemaname, tablename), ', '), '') + FROM pg_tables + WHERE schemaname = 'public' AND tablename <> 'goose_db_version'`).Scan(&tables) + if err != nil { + t.Fatalf("list tables: %v", err) + } + if tables == "" { + return + } + if _, err := d.Pool.Exec(ctx, "TRUNCATE "+tables+" CASCADE"); err != nil { + t.Fatalf("truncate: %v", err) + } +} + +func (d *DB) Close() { + if d.Pool != nil { + d.Pool.Close() + } + _ = testcontainers.TerminateContainer(d.container) +} + +// KV is a Valkey instance without persistence or password. +type KV struct { + Client *redis.Client + URL string + container *tcvalkey.ValkeyContainer +} + +func StartValkey(ctx context.Context) (*KV, error) { + c, err := tcvalkey.Run(ctx, ValkeyImage) + if err != nil { + return nil, fmt.Errorf("start valkey: %w", err) + } + kv := &KV{container: c} + kv.URL, err = c.ConnectionString(ctx) + if err != nil { + kv.Close() + return nil, fmt.Errorf("valkey address: %w", err) + } + opts, err := redis.ParseURL(kv.URL) + if err != nil { + kv.Close() + return nil, fmt.Errorf("valkey url: %w", err) + } + kv.Client = redis.NewClient(opts) + return kv, nil +} + +func (k *KV) Reset(t testing.TB) { + t.Helper() + if err := k.Client.FlushDB(context.Background()).Err(); err != nil { + t.Fatalf("flushdb: %v", err) + } +} + +func (k *KV) Close() { + if k.Client != nil { + _ = k.Client.Close() + } + _ = testcontainers.TerminateContainer(k.container) +} diff --git a/internal/testenv/testenv_test.go b/internal/testenv/testenv_test.go new file mode 100644 index 00000000..6488bded --- /dev/null +++ b/internal/testenv/testenv_test.go @@ -0,0 +1,29 @@ +package testenv_test + +import ( + "context" + "testing" + + "messenger/internal/testenv" +) + +func TestValkeyStartsAndResetFlushesKeys(t *testing.T) { + ctx := context.Background() + kv, err := testenv.StartValkey(ctx) + if err != nil { + t.Fatal(err) + } + defer kv.Close() + + if err := kv.Client.Set(ctx, "probe", "1", 0).Err(); err != nil { + t.Fatal(err) + } + kv.Reset(t) + n, err := kv.Client.DBSize(ctx).Result() + if err != nil { + t.Fatal(err) + } + if n != 0 { + t.Fatalf("%d keys left after Reset", n) + } +} diff --git a/migrations/00001_init.sql b/migrations/00001_init.sql new file mode 100644 index 00000000..e19bbf97 --- /dev/null +++ b/migrations/00001_init.sql @@ -0,0 +1,40 @@ +-- +goose Up +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL CONSTRAINT users_username_key UNIQUE, + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats (id), + user_id uuid NOT NULL REFERENCES users (id), + PRIMARY KEY (chat_id, user_id) +); + +-- Lists the chats of one user in (created_at, id) order: chat ids are UUIDv7. +CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id); + +CREATE TABLE 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, + created_at timestamptz NOT NULL +); + +-- Keyset pagination of one chat's history: WHERE chat_id = $1 AND id > $2 ORDER BY id. +CREATE INDEX messages_chat_id_idx ON messages (chat_id, id); + +-- +goose Down +DROP TABLE messages; +DROP TABLE chat_members; +DROP TABLE chats; +DROP TABLE users; diff --git a/migrations/embed.go b/migrations/embed.go new file mode 100644 index 00000000..29af9ab0 --- /dev/null +++ b/migrations/embed.go @@ -0,0 +1,8 @@ +// Package migrations embeds the versioned SQL schema, so the server binary +// always carries the schema it was built against. +package migrations + +import "embed" + +//go:embed *.sql +var FS embed.FS -- 2.54.0 From f42bf97b3a991408a8334ec7db2e2fe61b247e98 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 14:47:47 +0300 Subject: [PATCH 03/21] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=85=D0=B5=D1=88=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=20Argon2id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- internal/platform/password/password.go | 74 +++++++++++++++++++ internal/platform/password/password_test.go | 78 +++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 internal/platform/password/password.go create mode 100644 internal/platform/password/password_test.go diff --git a/go.mod b/go.mod index 049a6502..9c6fb13e 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/testcontainers/testcontainers-go v0.39.0 github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0 + golang.org/x/crypto v0.57.0 ) require ( @@ -95,7 +96,6 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.yaml.in/yaml/v3 v3.0.5 // indirect golang.org/x/arch v0.22.0 // indirect - golang.org/x/crypto v0.57.0 // indirect golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect golang.org/x/net v0.59.0 // indirect golang.org/x/sync v0.23.0 // indirect diff --git a/internal/platform/password/password.go b/internal/platform/password/password.go new file mode 100644 index 00000000..2fc2c7a7 --- /dev/null +++ b/internal/platform/password/password.go @@ -0,0 +1,74 @@ +// Package password hashes user passwords with Argon2id. +// +// A password has little entropy, so its hash must be slow and memory-hard to +// make offline guessing expensive. Session tokens are the opposite case: they +// are random 256-bit values, and a fast digest is enough for them. +package password + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +// OWASP baseline for Argon2id. The parameters are written into every hash, +// so raising them later does not invalidate stored hashes. +const ( + memoryKiB = 19 * 1024 + iterations = 2 + threads = 1 + saltLen = 16 + keyLen = 32 +) + +var ErrMalformed = errors.New("malformed password hash") + +var b64 = base64.RawStdEncoding + +// Hash returns the PHC string $argon2id$v=19$m=…,t=…,p=…$$. +func Hash(plain string) string { + salt := make([]byte, saltLen) + // Since Go 1.24 crypto/rand.Read never returns an error. + _, _ = rand.Read(salt) + key := argon2.IDKey([]byte(plain), salt, iterations, memoryKiB, threads, keyLen) + return fmt.Sprintf("$argon2id$v=%d$%s$%s$%s", + argon2.Version, params(memoryKiB, iterations, threads), b64.EncodeToString(salt), b64.EncodeToString(key)) +} + +// Verify reports whether plain matches encoded. An error means the stored +// hash itself is unreadable, not that the password is wrong. +func Verify(encoded, plain string) (bool, error) { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" { + return false, ErrMalformed + } + var version int + if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || version != argon2.Version { + return false, ErrMalformed + } + var memory, time uint32 + var parallelism uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, ¶llelism); err != nil || + memory == 0 || time == 0 || parallelism == 0 || params(memory, time, parallelism) != parts[3] { + return false, ErrMalformed + } + salt, err := b64.DecodeString(parts[4]) + if err != nil || len(salt) == 0 { + return false, ErrMalformed + } + key, err := b64.DecodeString(parts[5]) + if err != nil || len(key) == 0 { + return false, ErrMalformed + } + got := argon2.IDKey([]byte(plain), salt, time, memory, parallelism, uint32(len(key))) + return subtle.ConstantTimeCompare(got, key) == 1, nil +} + +func params(memory, time uint32, parallelism uint8) string { + return fmt.Sprintf("m=%d,t=%d,p=%d", memory, time, parallelism) +} diff --git a/internal/platform/password/password_test.go b/internal/platform/password/password_test.go new file mode 100644 index 00000000..7c518ad1 --- /dev/null +++ b/internal/platform/password/password_test.go @@ -0,0 +1,78 @@ +package password_test + +import ( + "crypto/rand" + "encoding/base64" + "fmt" + "strings" + "testing" + + "golang.org/x/crypto/argon2" + + "messenger/internal/platform/password" +) + +func TestHashVerifiesOnlyTheOriginalPassword(t *testing.T) { + for _, plain := range []string{ + "correct horse battery staple", + "с:двоеточием:и кириллицей", + "emoji 👋👋👋 and : colon", + } { + encoded := password.Hash(plain) + ok, err := password.Verify(encoded, plain) + if err != nil || !ok { + t.Fatalf("Verify(%q) = %v, %v; want true", plain, ok, err) + } + ok, err = password.Verify(encoded, plain+"x") + if err != nil || ok { + t.Fatalf("Verify(wrong) = %v, %v; want false", ok, err) + } + } +} + +func TestHashIsSaltedPHCString(t *testing.T) { + a, b := password.Hash("same password"), password.Hash("same password") + if a == b { + t.Fatal("two hashes of one password are equal: salt is missing") + } + if !strings.HasPrefix(a, "$argon2id$v=19$m=19456,t=2,p=1$") { + t.Fatalf("unexpected encoding %q", a) + } + if strings.Contains(a, "same password") { + t.Fatal("hash contains the plaintext") + } +} + +// A hash made with other parameters must still verify: parameters travel in +// the encoded string, so they can be raised without invalidating old hashes. +func TestVerifyUsesParametersFromTheEncoding(t *testing.T) { + salt := make([]byte, 16) + _, _ = rand.Read(salt) + key := argon2.IDKey([]byte("legacy password"), salt, 1, 8*1024, 2, 32) + b64 := base64.RawStdEncoding + encoded := fmt.Sprintf("$argon2id$v=19$m=8192,t=1,p=2$%s$%s", b64.EncodeToString(salt), b64.EncodeToString(key)) + + ok, err := password.Verify(encoded, "legacy password") + if err != nil || !ok { + t.Fatalf("Verify = %v, %v; want true", ok, err) + } +} + +func TestVerifyRejectsMalformedEncoding(t *testing.T) { + valid := password.Hash("whatever password") + parts := strings.Split(valid, "$") + for name, encoded := range map[string]string{ + "empty": "", + "wrong algo": strings.Replace(valid, "argon2id", "argon2i", 1), + "wrong version": strings.Replace(valid, "v=19", "v=16", 1), + "bad params": strings.Replace(valid, "m=19456,t=2,p=1", "m=x,t=2,p=1", 1), + "zero params": strings.Replace(valid, "m=19456,t=2,p=1", "m=19456,t=0,p=1", 1), + "bad salt": strings.Join([]string{"", parts[1], parts[2], parts[3], "!!!", parts[5]}, "$"), + "bad key": strings.Join([]string{"", parts[1], parts[2], parts[3], parts[4], "!!!"}, "$"), + "missing fields": strings.Join(parts[:5], "$"), + } { + if _, err := password.Verify(encoded, "whatever password"); err == nil { + t.Errorf("%s: Verify accepted %q", name, encoded) + } + } +} -- 2.54.0 From 67201087934319cb53e5d06bc8cbdf0884d413f2 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 14:49:19 +0300 Subject: [PATCH 04/21] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20WithUser=20=D0=B2=20httpx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/auth/auth.go | 18 ------------------ internal/auth/auth_test.go | 20 ++++---------------- internal/chat/handler.go | 13 ++++++------- internal/platform/httpx/httpx_test.go | 25 +++++++++++++++++++++++++ internal/platform/httpx/identity.go | 27 +++++++++++++++++++++++---- internal/user/handler.go | 5 ++--- 6 files changed, 60 insertions(+), 48 deletions(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index e9d6fb59..20af4f49 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -5,7 +5,6 @@ package auth import ( "context" - "errors" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -46,20 +45,3 @@ func RequireUser(users UserLookup) gin.HandlerFunc { c.Next() } } - -// UserHandler is a handler that needs the identified caller. -type UserHandler func(c *gin.Context, actor uuid.UUID) - -// WithUser adapts a UserHandler to gin. Reaching it without RequireUser in -// front is a wiring mistake, so it is reported as an internal error rather -// than blamed on the client with a 401. -func WithUser(h UserHandler) gin.HandlerFunc { - return func(c *gin.Context) { - actor, ok := httpx.UserID(c) - if !ok { - httpx.Error(c, errors.New("route is missing the RequireUser middleware")) - return - } - h(c, actor) - } -} diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index aedfb6da..7630c4a5 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -28,13 +28,9 @@ func echoActor(c *gin.Context, actor uuid.UUID) { httpx.JSON(c, http.StatusOK, map[string]string{"id": actor.String()}) } -func newEngine(users lookup, guarded bool) *gin.Engine { +func newEngine(users lookup) *gin.Engine { e := httpx.NewEngine(slog.New(slog.NewTextHandler(io.Discard, nil)), 1024) - if guarded { - e.GET("/me", auth.RequireUser(users), auth.WithUser(echoActor)) - } else { - e.GET("/me", auth.WithUser(echoActor)) - } + e.GET("/me", auth.RequireUser(users), httpx.WithUser(echoActor)) return e } @@ -60,7 +56,7 @@ func call(e *gin.Engine, header string) (*httptest.ResponseRecorder, string) { func TestKnownUserReachesHandlerWithItsID(t *testing.T) { alice := ident.New() - rec, got := call(newEngine(lookup{alice: {}}, true), alice.String()) + rec, got := call(newEngine(lookup{alice: {}}), alice.String()) if rec.Code != http.StatusOK || got != alice.String() { t.Fatalf("status %d, handler saw %q, want %s", rec.Code, got, alice) } @@ -68,7 +64,7 @@ func TestKnownUserReachesHandlerWithItsID(t *testing.T) { func TestMiddlewareRejectsMissingMalformedAndUnknownWith401(t *testing.T) { alice := ident.New() - e := newEngine(lookup{alice: {}}, true) + e := newEngine(lookup{alice: {}}) for name, header := range map[string]string{ "missing": "", "malformed": "not-a-uuid", @@ -81,11 +77,3 @@ func TestMiddlewareRejectsMissingMalformedAndUnknownWith401(t *testing.T) { } } } - -func TestWithUserWithoutMiddlewareIsAWiringError(t *testing.T) { - alice := ident.New() - rec, code := call(newEngine(lookup{alice: {}}, false), alice.String()) - if rec.Code != http.StatusInternalServerError || code != "internal_error" { - t.Fatalf("status = %d code = %q, want 500 internal_error", rec.Code, code) - } -} diff --git a/internal/chat/handler.go b/internal/chat/handler.go index b18504c9..9a2c2cc9 100644 --- a/internal/chat/handler.go +++ b/internal/chat/handler.go @@ -6,7 +6,6 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" - "messenger/internal/auth" "messenger/internal/platform/apperr" "messenger/internal/platform/httpx" "messenger/internal/platform/ident" @@ -21,13 +20,13 @@ func NewHandler(chats *Service) *Handler { return &Handler{chats: chats} } -// Register mounts the chat endpoints on a router guarded by RequireUser. +// Register mounts the chat endpoints on a router guarded by the authentication middleware. func (h *Handler) Register(protected gin.IRouter) { - protected.POST("/chats", auth.WithUser(h.create)) - protected.GET("/chats", auth.WithUser(h.list)) - protected.GET("/chats/:chat_id", auth.WithUser(h.get)) - protected.POST("/chats/:chat_id/messages", auth.WithUser(h.send)) - protected.GET("/chats/:chat_id/messages", auth.WithUser(h.history)) + protected.POST("/chats", httpx.WithUser(h.create)) + protected.GET("/chats", httpx.WithUser(h.list)) + protected.GET("/chats/:chat_id", httpx.WithUser(h.get)) + protected.POST("/chats/:chat_id/messages", httpx.WithUser(h.send)) + protected.GET("/chats/:chat_id/messages", httpx.WithUser(h.history)) } type createChatRequest struct { diff --git a/internal/platform/httpx/httpx_test.go b/internal/platform/httpx/httpx_test.go index 2683bea9..f57fdedf 100644 --- a/internal/platform/httpx/httpx_test.go +++ b/internal/platform/httpx/httpx_test.go @@ -282,6 +282,31 @@ func TestUserIDRoundTripAndAccessLogAttribution(t *testing.T) { } } +func TestWithUserPassesTheIdentifiedCaller(t *testing.T) { + actor := uuid.MustParse("22222222-2222-4222-8222-222222222222") + e := newEngine(t, nil) + e.GET("/actor", + func(c *gin.Context) { httpx.SetUserID(c, actor) }, + httpx.WithUser(func(c *gin.Context, got uuid.UUID) { + httpx.JSON(c, http.StatusOK, gin.H{"id": got.String()}) + })) + rec, _ := do(t, e, http.MethodGet, "/actor", "", nil) + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), actor.String()) { + t.Fatalf("handler did not receive the caller: %d %s", rec.Code, rec.Body.String()) + } +} + +// Reaching WithUser without an authentication middleware in front is a +// wiring mistake, so it must not be blamed on the client with a 401. +func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) { + e := newEngine(t, nil) + e.GET("/actor", httpx.WithUser(func(c *gin.Context, _ uuid.UUID) { + httpx.JSON(c, http.StatusOK, gin.H{}) + })) + rec, eb := do(t, e, http.MethodGet, "/actor", "", nil) + assertError(t, rec, eb, http.StatusInternalServerError, "internal_error") +} + func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any { t.Helper() var entry map[string]any diff --git a/internal/platform/httpx/identity.go b/internal/platform/httpx/identity.go index 231d67e8..6a4b6fcf 100644 --- a/internal/platform/httpx/identity.go +++ b/internal/platform/httpx/identity.go @@ -1,16 +1,18 @@ package httpx import ( + "errors" + "github.com/gin-gonic/gin" "github.com/google/uuid" ) const keyUserID = "messenger.user_id" -// SetUserID records the identified caller for the handlers behind the -// identification middleware and for the access log. It lives here rather -// than in the auth package because the access log needs it and auth already -// depends on this package. +// SetUserID records the authenticated caller for the handlers behind the +// authentication middleware and for the access log. It lives here rather +// than in the auth package because the access log needs it and the business +// modules must not depend on auth. func SetUserID(c *gin.Context, id uuid.UUID) { c.Set(keyUserID, id) } @@ -23,3 +25,20 @@ func UserID(c *gin.Context) (uuid.UUID, bool) { id, ok := v.(uuid.UUID) return id, ok } + +// UserHandler is a handler that needs the authenticated caller. +type UserHandler func(c *gin.Context, actor uuid.UUID) + +// WithUser adapts a UserHandler to gin. Reaching it without the +// authentication middleware in front is a wiring mistake, so it is reported +// as an internal error rather than blamed on the client with a 401. +func WithUser(h UserHandler) gin.HandlerFunc { + return func(c *gin.Context) { + actor, ok := UserID(c) + if !ok { + Error(c, errors.New("route is missing the authentication middleware")) + return + } + h(c, actor) + } +} diff --git a/internal/user/handler.go b/internal/user/handler.go index af8dcbdc..0531bb3e 100644 --- a/internal/user/handler.go +++ b/internal/user/handler.go @@ -6,7 +6,6 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" - "messenger/internal/auth" "messenger/internal/platform/httpx" ) @@ -19,10 +18,10 @@ func NewHandler(users *Service) *Handler { } // Register mounts registration on the public router and the current-user -// endpoint on the router guarded by RequireUser. +// endpoint on the router guarded by the authentication middleware. func (h *Handler) Register(public, protected gin.IRouter) { public.POST("/users", h.register) - protected.GET("/users/me", auth.WithUser(h.me)) + protected.GET("/users/me", httpx.WithUser(h.me)) } type createRequest struct { -- 2.54.0 From 46c00436a01003089fbe7c0544d636bfd41156df Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 14:50:12 +0300 Subject: [PATCH 05/21] =?UTF-8?q?=D0=9E=D1=82=D0=B2=D1=8F=D0=B7=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B6=D0=B8=D0=B7=D0=BD=D0=B5=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20=D1=86=D0=B8=D0=BA=D0=BB=20=D1=81=D0=B5=D1=80=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=20=D0=BE=D1=82=20=D1=81=D0=B1=D0=BE=D1=80?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 3 ++- internal/server/server.go | 13 ++++++++----- internal/server/server_test.go | 24 ++++++++++++++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index ea200735..6113e4c7 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -33,7 +33,8 @@ func main() { defer stop() log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String()) - if err := server.Run(ctx, log, ln, app.New(log, cfg.MaxBodyBytes), cfg.ShutdownTimeout); err != nil { + a := app.New(log, cfg.MaxBodyBytes) + if err := server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout); err != nil { log.Error("server stopped with error", "error", err.Error()) os.Exit(1) } diff --git a/internal/server/server.go b/internal/server/server.go index d3774da7..8a70e0fe 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -10,14 +10,17 @@ import ( "net" "net/http" "time" - - "messenger/internal/app" ) +// Readiness is switched off when shutdown starts. +type Readiness interface { + SetReady(ready bool) +} + // Run serves until ctx is cancelled, then shuts down within shutdownTimeout. -func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shutdownTimeout time.Duration) error { +func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler, ready Readiness, shutdownTimeout time.Duration) error { srv := &http.Server{ - Handler: a.Engine, + Handler: h, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, @@ -43,7 +46,7 @@ func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shu } log.Info("shutdown started", "timeout", shutdownTimeout.String()) - a.Health.SetReady(false) + ready.SetReady(false) drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout) defer cancel() if err := srv.Shutdown(drain); err != nil { diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 69ceb5ad..040f172a 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -9,20 +9,32 @@ import ( "testing" "time" - "messenger/internal/app" + "github.com/gin-gonic/gin" + + "messenger/internal/health" + "messenger/internal/platform/httpx" "messenger/internal/server" ) +// newHandler builds the smallest handler with probes; the lifecycle does not +// depend on the business modules or their stores. +func newHandler(log *slog.Logger) (*gin.Engine, *health.Handler) { + e := httpx.NewEngine(log, 1024) + probes := health.New() + probes.Register(e) + return e, probes +} + func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) - a := app.New(log, 1024) + h, probes := newHandler(log) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) - go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }() + go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }() base := "http://" + ln.Addr().String() waitFor(t, base+"/health/ready", http.StatusOK) @@ -43,14 +55,14 @@ func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) { func TestReadinessDropsBeforeListenerCloses(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) - a := app.New(log, 1024) + h, probes := newHandler(log) ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) - go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }() + go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }() base := "http://" + ln.Addr().String() waitFor(t, base+"/health/ready", http.StatusOK) @@ -67,7 +79,7 @@ func TestReadinessDropsBeforeListenerCloses(t *testing.T) { cancel() <-done <-slow - if !a.Health.IsReady() { + if !probes.IsReady() { return } t.Fatal("readiness must be switched off during shutdown") -- 2.54.0 From 618d3919d10e16d53a3703708568522d82916214 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 14:54:34 +0300 Subject: [PATCH 06/21] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=82=D0=B5=D0=BB=D0=B5=D0=B9=20=D0=B2=20PostgreSQL=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 34 ++++-- internal/app/app.go | 19 ++- internal/app/app_test.go | 29 ++++- internal/platform/config/config.go | 30 ++++- internal/platform/config/config_test.go | 54 ++++++++- internal/user/handler.go | 3 +- internal/user/main_test.go | 24 ++++ internal/user/repository.go | 3 +- internal/user/service.go | 38 +++++- internal/user/service_test.go | 150 ++++++++++++++++++++---- internal/user/store/inmem.go | 63 ---------- internal/user/store/inmem_test.go | 97 --------------- internal/user/store/postgres.go | 75 ++++++++++++ internal/user/user.go | 36 ++++-- internal/user/user_test.go | 22 ++++ tools/concurrency/main.go | 4 + tools/concurrency/main_test.go | 20 +++- 17 files changed, 482 insertions(+), 219 deletions(-) create mode 100644 internal/user/main_test.go delete mode 100644 internal/user/store/inmem.go delete mode 100644 internal/user/store/inmem_test.go create mode 100644 internal/user/store/postgres.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 6113e4c7..efafc131 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,4 +1,4 @@ -// Command server starts the in-memory messenger API. +// Command server starts the messenger API over PostgreSQL. package main import ( @@ -12,6 +12,7 @@ import ( "messenger/internal/app" "messenger/internal/platform/config" + "messenger/internal/platform/postgres" "messenger/internal/server" ) @@ -23,23 +24,36 @@ func main() { } log := newLogger(cfg) - ln, err := net.Listen("tcp", cfg.Addr()) - if err != nil { - log.Error("listen failed", "addr", cfg.Addr(), "error", err.Error()) - os.Exit(1) - } - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) defer stop() - log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String()) - a := app.New(log, cfg.MaxBodyBytes) - if err := server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout); err != nil { + if err := run(ctx, log, cfg); err != nil { log.Error("server stopped with error", "error", err.Error()) os.Exit(1) } } +// run connects the dependencies before it listens, so the port accepts +// connections only once the schema is in place. +func run(ctx context.Context, log *slog.Logger, cfg config.Config) error { + pool, err := postgres.NewPool(ctx, cfg.DatabaseURL) + if err != nil { + return err + } + defer pool.Close() + if err := postgres.Migrate(ctx, pool); err != nil { + return err + } + + ln, err := net.Listen("tcp", cfg.Addr()) + if err != nil { + return fmt.Errorf("listen on %s: %w", cfg.Addr(), err) + } + log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String()) + a := app.New(app.Deps{Log: log, DB: pool, MaxBodyBytes: cfg.MaxBodyBytes}) + return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout) +} + func newLogger(cfg config.Config) *slog.Logger { opts := &slog.HandlerOptions{Level: cfg.LogLevel} if cfg.Env == "dev" { diff --git a/internal/app/app.go b/internal/app/app.go index f2e10cbc..d15a752d 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -1,12 +1,13 @@ // Package app is the composition root: it builds every module with its -// in-memory stores and mounts them on one HTTP engine. Both the server -// binary and the integration tests start from here. +// stores and mounts them on one HTTP engine. Both the server binary and the +// integration tests start from here. package app import ( "log/slog" "github.com/gin-gonic/gin" + "github.com/jackc/pgx/v5/pgxpool" "messenger/internal/auth" "messenger/internal/chat" @@ -22,11 +23,19 @@ type App struct { Health *health.Handler } -func New(log *slog.Logger, maxBodyBytes int64) *App { - users := user.NewService(userstore.NewInMemUsers()) +// Deps are the shared resources the modules are built on; their lifetime is +// owned by the caller. +type Deps struct { + Log *slog.Logger + DB *pgxpool.Pool + MaxBodyBytes int64 +} + +func New(d Deps) *App { + users := user.NewService(userstore.NewUsers(d.DB)) chats := chat.NewService(chatstore.NewInMemChats(), chatstore.NewInMemMessages(), users) - engine := httpx.NewEngine(log, maxBodyBytes) + engine := httpx.NewEngine(d.Log, d.MaxBodyBytes) probes := health.New() probes.Register(engine) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index b50f48be..01ec1cb7 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -2,6 +2,7 @@ package app_test import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -9,6 +10,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "os" "sort" "strings" "sync" @@ -17,9 +19,26 @@ import ( "messenger/internal/app" "messenger/internal/chat" "messenger/internal/platform/pagination" + "messenger/internal/testenv" "messenger/internal/user" ) +var db *testenv.DB + +func TestMain(m *testing.M) { + var err error + db, err = testenv.StartPostgres(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + db.Close() + os.Exit(code) +} + +const testPassword = "correct-horse-battery" + // errorBody mirrors the contract error envelope. type errorBody struct { Error struct { @@ -41,10 +60,12 @@ type tester struct { a *app.App } +// newTester assembles the application over an empty database. func newTester(t *testing.T) *tester { t.Helper() + db.Reset(t) log := slog.New(slog.NewTextHandler(io.Discard, nil)) - return &tester{t: t, a: app.New(log, 64*1024)} + return &tester{t: t, a: app.New(app.Deps{Log: log, DB: db.Pool, MaxBodyBytes: 64 * 1024})} } // do sends one request. A string body is sent verbatim, anything else is @@ -127,7 +148,8 @@ func decode[T any](t *testing.T, res response) T { func (ts *tester) user(username, display string) user.Response { ts.t.Helper() - res := ts.do(http.MethodPost, "/api/v1/users", "", map[string]string{"username": username, "display_name": display}, nil) + body := map[string]string{"username": username, "display_name": display, "password": testPassword} + res := ts.do(http.MethodPost, "/api/v1/users", "", body, nil) ts.expect(res, http.StatusCreated, "") return decode[user.Response](ts.t, res) } @@ -192,7 +214,8 @@ func TestRegisterUserShapeAndConflicts(t *testing.T) { t.Fatalf("created_at = %q", alice.CreatedAt) } post := func(body any) response { return ts.do(http.MethodPost, "/api/v1/users", "", body, nil) } - ts.expect(post(map[string]string{"username": "alice", "display_name": "Other"}), http.StatusConflict, "conflict") + ts.expect(post(map[string]string{"username": "alice", "display_name": "Other", "password": testPassword}), http.StatusConflict, "conflict") + ts.expect(post(map[string]string{"username": "bob", "display_name": "Bob"}), http.StatusBadRequest, "invalid_request") ts.expect(post(map[string]string{"username": "Alice", "display_name": "x"}), http.StatusBadRequest, "invalid_request") ts.expect(post(map[string]string{"username": "bob", "display_name": " "}), http.StatusBadRequest, "invalid_request") ts.expect(post(map[string]any{"username": "bob", "display_name": "x", "password": "no"}), http.StatusBadRequest, "invalid_request") diff --git a/internal/platform/config/config.go b/internal/platform/config/config.go index cd04dc3c..03961ddb 100644 --- a/internal/platform/config/config.go +++ b/internal/platform/config/config.go @@ -1,12 +1,15 @@ // Package config reads the process configuration from the environment. -// Every setting has a default suited to the lab stand; an invalid value stops -// the process at start-up with a message naming the variable. +// Settings have defaults suited to the lab stand, except credentials, which +// are required and read from files named by *_FILE variables so that their +// values never appear in the environment. An invalid value stops the process +// at start-up with a message naming the variable, never the secret. package config import ( "fmt" "log/slog" "net" + "os" "strconv" "strings" "time" @@ -19,6 +22,7 @@ type Config struct { LogLevel slog.Level ShutdownTimeout time.Duration MaxBodyBytes int64 + DatabaseURL string } // Lookup reads one variable, reporting whether it was set; os.LookupEnv @@ -71,9 +75,31 @@ func Load(lookup Lookup) (Config, error) { } cfg.MaxBodyBytes = n } + var err error + if cfg.DatabaseURL, err = secret(lookup, "DATABASE_URL_FILE"); err != nil { + return Config{}, err + } return cfg, nil } +// secret reads the file named by the variable; surrounding whitespace, +// including the trailing newline editors add, is dropped. +func secret(lookup Lookup, key string) (string, error) { + path, ok := lookup(key) + if !ok || strings.TrimSpace(path) == "" { + return "", fmt.Errorf("%s must name a file with the value", key) + } + raw, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("%s: cannot read %s: %w", key, path, err) + } + value := strings.TrimSpace(string(raw)) + if value == "" { + return "", fmt.Errorf("%s: file %s is empty", key, path) + } + return value, nil +} + func (c Config) Addr() string { return net.JoinHostPort(c.Host, strconv.Itoa(c.Port)) } diff --git a/internal/platform/config/config_test.go b/internal/platform/config/config_test.go index efc496af..af2881ab 100644 --- a/internal/platform/config/config_test.go +++ b/internal/platform/config/config_test.go @@ -2,6 +2,9 @@ package config_test import ( "log/slog" + "os" + "path/filepath" + "strings" "testing" "time" @@ -15,8 +18,29 @@ func env(values map[string]string) func(string) (string, bool) { } } +func secretFile(t *testing.T, content string) string { + t.Helper() + path := filepath.Join(t.TempDir(), "secret") + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + return path +} + +const databaseURL = "postgres://app:s3cret@db:5432/messenger" + +// required holds the variables without defaults, merged with extra. +func required(t *testing.T, extra map[string]string) map[string]string { + t.Helper() + values := map[string]string{"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n")} + for k, v := range extra { + values[k] = v + } + return values +} + func TestDefaults(t *testing.T) { - cfg, err := config.Load(env(nil)) + cfg, err := config.Load(env(required(t, nil))) if err != nil { t.Fatalf("Load error: %v", err) } @@ -29,17 +53,39 @@ func TestDefaults(t *testing.T) { if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 { t.Fatalf("cfg = %+v", cfg) } + if cfg.DatabaseURL != databaseURL { + t.Fatalf("DatabaseURL = %q, want the file content without the newline", cfg.DatabaseURL) + } +} + +func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) { + cases := map[string]map[string]string{ + "unset": nil, + "empty path": {"DATABASE_URL_FILE": ""}, + "no file": {"DATABASE_URL_FILE": filepath.Join(t.TempDir(), "missing")}, + "blank file": {"DATABASE_URL_FILE": secretFile(t, " \r\n")}, + } + for name, values := range cases { + _, err := config.Load(env(values)) + if err == nil { + t.Errorf("%s: want error", name) + continue + } + if !strings.Contains(err.Error(), "DATABASE_URL_FILE") || strings.Contains(err.Error(), "s3cret") { + t.Errorf("%s: error must name the variable and hide the secret: %v", name, err) + } + } } func TestOverrides(t *testing.T) { - cfg, err := config.Load(env(map[string]string{ + cfg, err := config.Load(env(required(t, map[string]string{ "APP_HOST": "127.0.0.1", "APP_PORT": "9090", "APP_ENV": "dev", "APP_LOG_LEVEL": "debug", "APP_SHUTDOWN_TIMEOUT": "3s", "APP_MAX_BODY_BYTES": "1024", - })) + }))) if err != nil { t.Fatalf("Load error: %v", err) } @@ -64,7 +110,7 @@ func TestInvalidValuesAreReported(t *testing.T) { "empty host": {"APP_HOST": ""}, } for name, values := range cases { - if _, err := config.Load(env(values)); err == nil { + if _, err := config.Load(env(required(t, values))); err == nil { t.Errorf("%s: want error", name) } } diff --git a/internal/user/handler.go b/internal/user/handler.go index 0531bb3e..a5f091af 100644 --- a/internal/user/handler.go +++ b/internal/user/handler.go @@ -27,6 +27,7 @@ func (h *Handler) Register(public, protected gin.IRouter) { type createRequest struct { Username string `json:"username"` DisplayName string `json:"display_name"` + Password string `json:"password"` } type Response struct { @@ -51,7 +52,7 @@ func (h *Handler) register(c *gin.Context) { httpx.Error(c, err) return } - u, err := h.users.Register(c.Request.Context(), req.Username, req.DisplayName) + u, err := h.users.Register(c.Request.Context(), req.Username, req.DisplayName, req.Password) if err != nil { httpx.Error(c, err) return diff --git a/internal/user/main_test.go b/internal/user/main_test.go new file mode 100644 index 00000000..ba3a1ca2 --- /dev/null +++ b/internal/user/main_test.go @@ -0,0 +1,24 @@ +package user_test + +import ( + "context" + "fmt" + "os" + "testing" + + "messenger/internal/testenv" +) + +var db *testenv.DB + +func TestMain(m *testing.M) { + var err error + db, err = testenv.StartPostgres(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + db.Close() + os.Exit(code) +} diff --git a/internal/user/repository.go b/internal/user/repository.go index 05098e7e..eb2ac2f6 100644 --- a/internal/user/repository.go +++ b/internal/user/repository.go @@ -11,7 +11,8 @@ type Repository interface { // check and the insert must be atomic. Create(ctx context.Context, user User) error GetByID(ctx context.Context, id uuid.UUID) (User, error) + GetByUsername(ctx context.Context, username string) (User, error) // ExistingIDs returns the subset of ids that belong to registered users, - // in input order, in one round trip. + // in no particular order, in one round trip. ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) } diff --git a/internal/user/service.go b/internal/user/service.go index 0a177b5e..7a78448a 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -2,11 +2,14 @@ package user import ( "context" + "errors" "fmt" + "sync" "github.com/google/uuid" "messenger/internal/platform/ident" + "messenger/internal/platform/password" ) type Service struct { @@ -17,17 +20,50 @@ func NewService(repo Repository) *Service { return &Service{repo: repo} } -func (s *Service) Register(ctx context.Context, username, displayName string) (User, error) { +// Register validates every field before hashing, so invalid input never pays +// for the KDF. +func (s *Service) Register(ctx context.Context, username, displayName, plain string) (User, error) { u, err := New(ident.New(), username, displayName) if err != nil { return User{}, err } + if err := ValidatePassword(plain); err != nil { + return User{}, err + } + u.PasswordHash = password.Hash(plain) if err := s.repo.Create(ctx, u); err != nil { return User{}, err } return u, nil } +// dummyHash is verified for unknown usernames, so a failed login takes the +// same time whether or not the user exists. +var dummyHash = sync.OnceValue(func() string { + return password.Hash("timing-equaliser-password") +}) + +// Authenticate returns the user whose password matches, or +// ErrInvalidCredentials for an unknown username and a wrong password alike. +func (s *Service) Authenticate(ctx context.Context, username, plain string) (User, error) { + u, err := s.repo.GetByUsername(ctx, username) + if errors.Is(err, ErrNotFound) { + _, _ = password.Verify(dummyHash(), plain) + return User{}, ErrInvalidCredentials + } + if err != nil { + return User{}, fmt.Errorf("find user: %w", err) + } + ok, err := password.Verify(u.PasswordHash, plain) + if err != nil { + return User{}, fmt.Errorf("verify password of user %s: %w", u.ID, err) + } + if !ok { + return User{}, ErrInvalidCredentials + } + return u, nil +} + func (s *Service) GetByID(ctx context.Context, id uuid.UUID) (User, error) { return s.repo.GetByID(ctx, id) } diff --git a/internal/user/service_test.go b/internal/user/service_test.go index 2f5d0531..de6d11eb 100644 --- a/internal/user/service_test.go +++ b/internal/user/service_test.go @@ -3,6 +3,8 @@ package user_test import ( "context" "errors" + "strings" + "sync" "testing" "github.com/google/uuid" @@ -13,50 +15,116 @@ import ( "messenger/internal/user/store" ) -func newService() *user.Service { - return user.NewService(store.NewInMemUsers()) +const pw = "correct-horse-battery" + +// newService wires the service to the real store over an empty database. +func newService(t *testing.T) *user.Service { + t.Helper() + db.Reset(t) + return user.NewService(store.NewUsers(db.Pool)) } -func TestRegisterStoresUser(t *testing.T) { - svc := newService() +func TestRegisterStoresUserWithPasswordHash(t *testing.T) { + svc := newService(t) ctx := context.Background() - u, err := svc.Register(ctx, "alice", "Алиса") + u, err := svc.Register(ctx, "alice", "Алиса", pw) if err != nil { t.Fatalf("Register error: %v", err) } if u.ID.Version() != 7 { t.Fatalf("id must be UUIDv7, got version %d", u.ID.Version()) } + if !strings.HasPrefix(u.PasswordHash, "$argon2id$") || strings.Contains(u.PasswordHash, pw) { + t.Fatalf("password must be stored as an Argon2id hash, got %q", u.PasswordHash) + } got, err := svc.GetByID(ctx, u.ID) - if err != nil || got != u { - t.Fatalf("GetByID = %+v, %v; want %+v", got, err, u) + if err != nil { + t.Fatalf("GetByID error: %v", err) + } + if got != u { + t.Fatalf("GetByID = %+v, want %+v", got, u) } if ok, err := svc.Exists(ctx, u.ID); err != nil || !ok { t.Fatalf("Exists = %v, %v; want true", ok, err) } } -func TestRegisterRejectsInvalidInput(t *testing.T) { - _, err := newService().Register(context.Background(), "Bad Name", "x") - if apperr.KindOf(err) != apperr.KindInvalid { - t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err) +func TestRegisterRejectsInvalidInputAndStoresNothing(t *testing.T) { + svc := newService(t) + ctx := context.Background() + cases := map[string][3]string{ + "username": {"Bad Name", "x", pw}, + "display_name": {"alice", " ", pw}, + "password": {"alice", "Алиса", "short"}, + } + for field, in := range cases { + _, err := svc.Register(ctx, in[0], in[1], in[2]) + if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), field) { + t.Errorf("%s: err = %v, want KindInvalid naming the field", field, err) + } + } + var n int + if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM users`).Scan(&n); err != nil || n != 0 { + t.Fatalf("users = %d, %v; invalid input must not be stored", n, err) } } func TestRegisterReportsDuplicateAsConflict(t *testing.T) { - svc := newService() + svc := newService(t) ctx := context.Background() - if _, err := svc.Register(ctx, "alice", "A"); err != nil { + if _, err := svc.Register(ctx, "alice", "A", pw); err != nil { t.Fatal(err) } - _, err := svc.Register(ctx, "alice", "B") + _, err := svc.Register(ctx, "alice", "B", pw) if !errors.Is(err, user.ErrUsernameTaken) || apperr.KindOf(err) != apperr.KindConflict { t.Fatalf("err = %v, want ErrUsernameTaken with KindConflict", err) } } +// Concurrent registrations of one username race on the unique constraint: +// exactly one wins and every other gets the conflict, never a 500. +func TestConcurrentRegistrationOfOneUsernameHasOneWinner(t *testing.T) { + svc := newService(t) + ctx := context.Background() + const attempts = 10 + errs := make(chan error, attempts) + var start, done sync.WaitGroup + start.Add(1) + for range attempts { + done.Add(1) + go func() { + defer done.Done() + start.Wait() + _, err := svc.Register(ctx, "racer", "Racer", pw) + errs <- err + }() + } + start.Done() + done.Wait() + close(errs) + + won, lost := 0, 0 + for err := range errs { + switch { + case err == nil: + won++ + case errors.Is(err, user.ErrUsernameTaken): + lost++ + default: + t.Errorf("unexpected error: %v", err) + } + } + if won != 1 || lost != attempts-1 { + t.Fatalf("won = %d, lost = %d; want 1 and %d", won, lost, attempts-1) + } + var n int + if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM users WHERE username = 'racer'`).Scan(&n); err != nil || n != 1 { + t.Fatalf("rows = %d, %v; want exactly one", n, err) + } +} + func TestGetByIDUnknownIsNotFound(t *testing.T) { - svc := newService() + svc := newService(t) _, err := svc.GetByID(context.Background(), ident.New()) if !errors.Is(err, user.ErrNotFound) || apperr.KindOf(err) != apperr.KindNotFound { t.Fatalf("err = %v, want ErrNotFound with KindNotFound", err) @@ -66,20 +134,56 @@ func TestGetByIDUnknownIsNotFound(t *testing.T) { } } -func TestExistingIDsFiltersInOneCall(t *testing.T) { - svc := newService() +func TestExistingIDsReturnsOnlyRegistered(t *testing.T) { + svc := newService(t) ctx := context.Background() - alice, _ := svc.Register(ctx, "alice", "A") - bob, _ := svc.Register(ctx, "bob", "B") - unknown := ident.New() - got, err := svc.ExistingIDs(ctx, []uuid.UUID{unknown, bob.ID, alice.ID}) + alice, _ := svc.Register(ctx, "alice", "A", pw) + bob, _ := svc.Register(ctx, "bob", "B", pw) + got, err := svc.ExistingIDs(ctx, []uuid.UUID{ident.New(), bob.ID, alice.ID}) if err != nil { t.Fatal(err) } - if len(got) != 2 || got[0] != bob.ID || got[1] != alice.ID { - t.Fatalf("ExistingIDs = %v, want bob then alice", got) + set := map[uuid.UUID]bool{} + for _, id := range got { + set[id] = true + } + if len(got) != 2 || !set[alice.ID] || !set[bob.ID] { + t.Fatalf("ExistingIDs = %v, want alice and bob", got) } if empty, err := svc.ExistingIDs(ctx, nil); err != nil || len(empty) != 0 { t.Fatalf("ExistingIDs(nil) = %v, %v", empty, err) } } + +func TestAuthenticateChecksPassword(t *testing.T) { + svc := newService(t) + ctx := context.Background() + const colonPassword = "пароль:с:двоеточием 👋" + alice, err := svc.Register(ctx, "alice", "Алиса", colonPassword) + if err != nil { + t.Fatal(err) + } + got, err := svc.Authenticate(ctx, "alice", colonPassword) + if err != nil || got != alice { + t.Fatalf("Authenticate = %+v, %v; want alice", got, err) + } +} + +// A wrong password and an unknown username must be indistinguishable. +func TestAuthenticateFailuresLookTheSame(t *testing.T) { + svc := newService(t) + ctx := context.Background() + if _, err := svc.Register(ctx, "alice", "Алиса", pw); err != nil { + t.Fatal(err) + } + _, wrong := svc.Authenticate(ctx, "alice", pw+"!") + _, unknown := svc.Authenticate(ctx, "nobody", pw) + for name, err := range map[string]error{"wrong password": wrong, "unknown user": unknown} { + if !errors.Is(err, user.ErrInvalidCredentials) || apperr.KindOf(err) != apperr.KindUnauthorized { + t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err) + } + } + if wrong.Error() != unknown.Error() { + t.Fatalf("messages differ: %q vs %q", wrong, unknown) + } +} diff --git a/internal/user/store/inmem.go b/internal/user/store/inmem.go deleted file mode 100644 index 882e5331..00000000 --- a/internal/user/store/inmem.go +++ /dev/null @@ -1,63 +0,0 @@ -// Package store holds the repository implementations of the users module. -// inmem.go is the in-process one: state lives only in this process and -// disappears with it by design of the lab. -package store - -import ( - "context" - "sync" - - "github.com/google/uuid" - - "messenger/internal/user" -) - -type InMemUsers struct { - mu sync.RWMutex - byID map[uuid.UUID]user.User - byUsername map[string]struct{} -} - -var _ user.Repository = (*InMemUsers)(nil) - -func NewInMemUsers() *InMemUsers { - return &InMemUsers{ - byID: make(map[uuid.UUID]user.User), - byUsername: make(map[string]struct{}), - } -} - -// Create checks and inserts under the same write lock, so concurrent -// registrations of one username admit exactly one winner. -func (r *InMemUsers) Create(_ context.Context, u user.User) error { - r.mu.Lock() - defer r.mu.Unlock() - if _, taken := r.byUsername[u.Username]; taken { - return user.ErrUsernameTaken - } - r.byUsername[u.Username] = struct{}{} - r.byID[u.ID] = u - return nil -} - -func (r *InMemUsers) GetByID(_ context.Context, id uuid.UUID) (user.User, error) { - r.mu.RLock() - defer r.mu.RUnlock() - u, ok := r.byID[id] - if !ok { - return user.User{}, user.ErrNotFound - } - return u, nil -} - -func (r *InMemUsers) ExistingIDs(_ context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { - r.mu.RLock() - defer r.mu.RUnlock() - found := make([]uuid.UUID, 0, len(ids)) - for _, id := range ids { - if _, ok := r.byID[id]; ok { - found = append(found, id) - } - } - return found, nil -} diff --git a/internal/user/store/inmem_test.go b/internal/user/store/inmem_test.go deleted file mode 100644 index 7ebcdbef..00000000 --- a/internal/user/store/inmem_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package store_test - -import ( - "context" - "errors" - "sync" - "testing" - - "github.com/google/uuid" - - "messenger/internal/platform/ident" - "messenger/internal/user" - "messenger/internal/user/store" -) - -func mustUser(t *testing.T, username string) user.User { - t.Helper() - u, err := user.New(ident.New(), username, "Display") - if err != nil { - t.Fatal(err) - } - return u -} - -func TestCreateThenGetByID(t *testing.T) { - repo := store.NewInMemUsers() - ctx := context.Background() - u := mustUser(t, "alice") - if err := repo.Create(ctx, u); err != nil { - t.Fatalf("Create error: %v", err) - } - got, err := repo.GetByID(ctx, u.ID) - if err != nil { - t.Fatalf("GetByID error: %v", err) - } - if got != u { - t.Fatalf("GetByID = %+v, want %+v", got, u) - } -} - -func TestGetByIDUnknownReturnsErrNotFound(t *testing.T) { - _, err := store.NewInMemUsers().GetByID(context.Background(), ident.New()) - if !errors.Is(err, user.ErrNotFound) { - t.Fatalf("err = %v, want ErrNotFound", err) - } -} - -func TestCreateRejectsDuplicateUsername(t *testing.T) { - repo := store.NewInMemUsers() - ctx := context.Background() - if err := repo.Create(ctx, mustUser(t, "alice")); err != nil { - t.Fatal(err) - } - err := repo.Create(ctx, mustUser(t, "alice")) - if !errors.Is(err, user.ErrUsernameTaken) { - t.Fatalf("err = %v, want ErrUsernameTaken", err) - } -} - -func TestCreateIsAtomicUnderConcurrency(t *testing.T) { - repo := store.NewInMemUsers() - ctx := context.Background() - const workers = 50 - var wg sync.WaitGroup - var mu sync.Mutex - created := 0 - for i := 0; i < workers; i++ { - wg.Add(1) - go func() { - defer wg.Done() - if err := repo.Create(ctx, mustUser(t, "same_name")); err == nil { - mu.Lock() - created++ - mu.Unlock() - } - }() - } - wg.Wait() - if created != 1 { - t.Fatalf("exactly one Create must win, got %d", created) - } -} - -func TestExistingIDsReturnsOnlyKnownIDs(t *testing.T) { - repo := store.NewInMemUsers() - ctx := context.Background() - alice, bob := mustUser(t, "alice"), mustUser(t, "bob") - _ = repo.Create(ctx, alice) - _ = repo.Create(ctx, bob) - got, err := repo.ExistingIDs(ctx, []uuid.UUID{ident.New(), bob.ID, alice.ID, bob.ID}) - if err != nil { - t.Fatal(err) - } - if len(got) != 3 || got[0] != bob.ID || got[1] != alice.ID || got[2] != bob.ID { - t.Fatalf("ExistingIDs must keep input order and drop unknown ids, got %v", got) - } -} diff --git a/internal/user/store/postgres.go b/internal/user/store/postgres.go new file mode 100644 index 00000000..4bfda146 --- /dev/null +++ b/internal/user/store/postgres.go @@ -0,0 +1,75 @@ +// Package store holds the repository implementations of the users module. +package store + +import ( + "context" + "errors" + "fmt" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "messenger/internal/platform/postgres" + "messenger/internal/user" +) + +const userColumns = `id, username, display_name, password_hash, created_at` + +type Users struct { + pool *pgxpool.Pool +} + +var _ user.Repository = (*Users)(nil) + +func NewUsers(pool *pgxpool.Pool) *Users { + return &Users{pool: pool} +} + +// Create relies on the unique constraint rather than a prior lookup, so two +// concurrent registrations of one username cannot both succeed. +func (r *Users) Create(ctx context.Context, u user.User) error { + _, err := r.pool.Exec(ctx, + `INSERT INTO users (`+userColumns+`) VALUES ($1, $2, $3, $4, $5)`, + u.ID, u.Username, u.DisplayName, u.PasswordHash, u.CreatedAt) + if postgres.IsUniqueViolation(err, "users_username_key") { + return user.ErrUsernameTaken + } + if err != nil { + return fmt.Errorf("insert user: %w", err) + } + return nil +} + +func (r *Users) GetByID(ctx context.Context, id uuid.UUID) (user.User, error) { + return r.one(ctx, `SELECT `+userColumns+` FROM users WHERE id = $1`, id) +} + +func (r *Users) GetByUsername(ctx context.Context, username string) (user.User, error) { + return r.one(ctx, `SELECT `+userColumns+` FROM users WHERE username = $1`, username) +} + +func (r *Users) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { + rows, err := r.pool.Query(ctx, `SELECT id FROM users WHERE id = ANY($1)`, ids) + if err != nil { + return nil, fmt.Errorf("select user ids: %w", err) + } + found, err := pgx.CollectRows(rows, pgx.RowTo[uuid.UUID]) + if err != nil { + return nil, fmt.Errorf("read user ids: %w", err) + } + return found, nil +} + +func (r *Users) one(ctx context.Context, query string, arg any) (user.User, error) { + var u user.User + err := r.pool.QueryRow(ctx, query, arg).Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return user.User{}, user.ErrNotFound + } + if err != nil { + return user.User{}, fmt.Errorf("select user: %w", err) + } + u.CreatedAt = u.CreatedAt.UTC() + return u, nil +} diff --git a/internal/user/user.go b/internal/user/user.go index f3050c85..f890c414 100644 --- a/internal/user/user.go +++ b/internal/user/user.go @@ -1,6 +1,7 @@ -// Package user is the users module. The entity mirrors the future table row -// and carries no behaviour; New only enforces input invariants and CreatedAt -// derives from the UUIDv7 id. +// Package user is the users module. The entity mirrors the table row and +// carries no behaviour; New only enforces input invariants and CreatedAt +// derives from the UUIDv7 id. The password itself is never kept: the service +// validates it and stores only its hash. package user import ( @@ -16,20 +17,28 @@ import ( "messenger/internal/platform/ident" ) -const MaxDisplayNameCodePoints = 100 +const ( + MaxDisplayNameCodePoints = 100 + MinPasswordCodePoints = 12 + MaxPasswordCodePoints = 128 +) var ( ErrNotFound = apperr.NotFound("user not found") ErrUsernameTaken = apperr.Conflict("username already taken") + // ErrInvalidCredentials covers an unknown username and a wrong password + // alike, so a login attempt cannot probe which usernames exist. + ErrInvalidCredentials = apperr.Unauthorized("invalid username or password") ) var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) type User struct { - ID uuid.UUID - Username string - DisplayName string - CreatedAt time.Time + ID uuid.UUID + Username string + DisplayName string + PasswordHash string + CreatedAt time.Time } func New(id uuid.UUID, username, displayName string) (User, error) { @@ -49,3 +58,14 @@ func New(id uuid.UUID, username, displayName string) (User, error) { CreatedAt: ident.TimeOf(id), }, nil } + +// ValidatePassword checks the contract length in Unicode code points. +func ValidatePassword(plain string) error { + if strings.TrimSpace(plain) == "" { + return apperr.Invalid("password must not be blank") + } + if n := utf8.RuneCountInString(plain); n < MinPasswordCodePoints || n > MaxPasswordCodePoints { + return apperr.Invalid(fmt.Sprintf("password must be %d to %d characters", MinPasswordCodePoints, MaxPasswordCodePoints)) + } + return nil +} diff --git a/internal/user/user_test.go b/internal/user/user_test.go index 72bca255..ada07ff2 100644 --- a/internal/user/user_test.go +++ b/internal/user/user_test.go @@ -39,6 +39,28 @@ func TestNewValidatesUsername(t *testing.T) { } } +// The contract counts the password length in Unicode code points, not bytes. +func TestValidatePasswordCountsCodePoints(t *testing.T) { + valid := []string{ + strings.Repeat("я", 12), + strings.Repeat("👋", 128), + "pass:word:with:colons", + " spaced but long enough ", + } + for _, p := range valid { + if err := user.ValidatePassword(p); err != nil { + t.Errorf("password of %d code points must be accepted: %v", len([]rune(p)), err) + } + } + invalid := []string{"", strings.Repeat(" ", 12), strings.Repeat("\t", 20), strings.Repeat("я", 11), strings.Repeat("👋", 129)} + for _, p := range invalid { + err := user.ValidatePassword(p) + if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "password") { + t.Errorf("password %q must be rejected as invalid naming the field, got %v", p, err) + } + } +} + func TestNewValidatesDisplayNameByCodePoints(t *testing.T) { valid := []string{"A", "Алиса", strings.Repeat("я", 100), "👋 hi"} for _, name := range valid { diff --git a/tools/concurrency/main.go b/tools/concurrency/main.go index 3d9fc4d0..11ef2e39 100644 --- a/tools/concurrency/main.go +++ b/tools/concurrency/main.go @@ -121,6 +121,9 @@ func run(baseURL string, clients, perClient int) (Report, error) { return report, nil } +// clientPassword is shared by the synthetic clients of one run. +const clientPassword = "concurrency-check-password" + func registerClients(api *client, runID string, n int) ([]string, error) { ids := make([]string, 0, n) for i := range n { @@ -128,6 +131,7 @@ func registerClients(api *client, runID string, n int) ([]string, error) { body := map[string]string{ "username": fmt.Sprintf("load_%s_%d", runID, i), "display_name": fmt.Sprintf("Load client %d", i), + "password": clientPassword, } if err := api.post("/api/v1/users", "", body, &created); err != nil { return nil, fmt.Errorf("register client %d: %w", i, err) diff --git a/tools/concurrency/main_test.go b/tools/concurrency/main_test.go index 8b64e201..e26a9d79 100644 --- a/tools/concurrency/main_test.go +++ b/tools/concurrency/main_test.go @@ -1,17 +1,35 @@ package main import ( + "context" + "fmt" "io" "log/slog" "net/http/httptest" + "os" "testing" "messenger/internal/app" + "messenger/internal/testenv" ) +var db *testenv.DB + +func TestMain(m *testing.M) { + var err error + db, err = testenv.StartPostgres(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + db.Close() + os.Exit(code) +} + func TestRunAgainstLiveServer(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) - srv := httptest.NewServer(app.New(log, 64*1024).Engine) + srv := httptest.NewServer(app.New(app.Deps{Log: log, DB: db.Pool, MaxBodyBytes: 64 * 1024}).Engine) defer srv.Close() report, err := run(srv.URL, 10, 10) -- 2.54.0 From 0b2db7eb2d2fff40d002d862e6a3faaa9521ad01 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 14:57:44 +0300 Subject: [PATCH 07/21] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20=D1=87=D0=B0=D1=82=D1=8B=20=D0=B8=20=D1=81?= =?UTF-8?q?=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20Pos?= =?UTF-8?q?tgreSQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/app.go | 2 +- internal/chat/chat.go | 11 +- internal/chat/chat_test.go | 12 +- internal/chat/repository.go | 3 +- internal/chat/service_test.go | 112 ++++++---- internal/chat/store/inmem.go | 188 ---------------- internal/chat/store/inmem_test.go | 262 ---------------------- internal/chat/store/postgres.go | 155 +++++++++++++ internal/chat/store/postgres_test.go | 313 +++++++++++++++++++++++++++ 9 files changed, 559 insertions(+), 499 deletions(-) delete mode 100644 internal/chat/store/inmem.go delete mode 100644 internal/chat/store/inmem_test.go create mode 100644 internal/chat/store/postgres.go create mode 100644 internal/chat/store/postgres_test.go diff --git a/internal/app/app.go b/internal/app/app.go index d15a752d..59408504 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -33,7 +33,7 @@ type Deps struct { func New(d Deps) *App { users := user.NewService(userstore.NewUsers(d.DB)) - chats := chat.NewService(chatstore.NewInMemChats(), chatstore.NewInMemMessages(), users) + chats := chat.NewService(chatstore.NewChats(d.DB), chatstore.NewMessages(d.DB), users) engine := httpx.NewEngine(d.Log, d.MaxBodyBytes) probes := health.New() diff --git a/internal/chat/chat.go b/internal/chat/chat.go index 6bde7698..0c1b2e77 100644 --- a/internal/chat/chat.go +++ b/internal/chat/chat.go @@ -1,4 +1,4 @@ -// Package chat is the chats module. Entities mirror future table rows and +// Package chat is the chats module. Entities mirror table rows and // carry no behaviour; membership is a query rather than a field, and // ChatWithMembers is the projection the API renders. Both entities derive // CreatedAt from their UUIDv7 id, so the history order (created_at, id) is @@ -6,7 +6,9 @@ package chat import ( + "bytes" "fmt" + "slices" "strings" "time" "unicode/utf8" @@ -48,7 +50,8 @@ func NewChat(id uuid.UUID, title string) (Chat, error) { } // ValidateMembers checks the member list of a new chat and returns its own -// copy in the original order. +// copy ordered by id: members are a set, and this is the one order in which +// every read returns them. func ValidateMembers(memberIDs []uuid.UUID, creator uuid.UUID) ([]uuid.UUID, error) { if len(memberIDs) == 0 { return nil, apperr.Invalid("member_ids must not be empty") @@ -69,5 +72,7 @@ func ValidateMembers(memberIDs []uuid.UUID, creator uuid.UUID) ([]uuid.UUID, err if _, ok := seen[creator]; !ok { return nil, apperr.Invalid("member_ids must include the creator") } - return append([]uuid.UUID(nil), memberIDs...), nil + members := slices.Clone(memberIDs) + slices.SortFunc(members, func(a, b uuid.UUID) int { return bytes.Compare(a[:], b[:]) }) + return members, nil } diff --git a/internal/chat/chat_test.go b/internal/chat/chat_test.go index ebfd5a85..829856c9 100644 --- a/internal/chat/chat_test.go +++ b/internal/chat/chat_test.go @@ -34,18 +34,20 @@ func TestNewChatValidatesTitle(t *testing.T) { } } -func TestValidateMembersCopiesAndKeepsOrder(t *testing.T) { +// Members come back in one canonical order (by id) whatever order the client +// sent, so the created chat and every later read of it agree. +func TestValidateMembersCopiesInCanonicalOrder(t *testing.T) { alice, bob := ident.New(), ident.New() input := []uuid.UUID{bob, alice} members, err := chat.ValidateMembers(input, alice) if err != nil { t.Fatalf("ValidateMembers error: %v", err) } - if len(members) != 2 || members[0] != bob || members[1] != alice { - t.Fatalf("order must be preserved, got %v", members) + if len(members) != 2 || members[0] != alice || members[1] != bob { + t.Fatalf("members must be ordered by id, got %v", members) } - input[0] = ident.New() - if members[0] != bob { + input[1] = ident.New() + if members[0] != alice { t.Fatal("result must not alias the input slice") } } diff --git a/internal/chat/repository.go b/internal/chat/repository.go index fb785932..765b1840 100644 --- a/internal/chat/repository.go +++ b/internal/chat/repository.go @@ -8,7 +8,8 @@ import ( // ChatRepository stores chats and their membership. Membership is answered // by queries; the projection with members is assembled by the store in one -// query, never by loading a member list into the entity. +// query, never by loading a member list into the entity. Members are always +// returned ordered by id. type ChatRepository interface { // Create stores the chat row and its members atomically. Create(ctx context.Context, chat Chat, memberIDs []uuid.UUID) error diff --git a/internal/chat/service_test.go b/internal/chat/service_test.go index ad6dde18..7e9525d8 100644 --- a/internal/chat/service_test.go +++ b/internal/chat/service_test.go @@ -3,6 +3,8 @@ package chat_test import ( "context" "errors" + "fmt" + "os" "strings" "sync/atomic" "testing" @@ -13,50 +15,70 @@ import ( "messenger/internal/chat/store" "messenger/internal/platform/apperr" "messenger/internal/platform/ident" + "messenger/internal/testenv" + "messenger/internal/user" + userstore "messenger/internal/user/store" ) -// knownUsers doubles chat.UserLookup and counts calls to prove that member -// checks are batched. -type knownUsers struct { - ids map[uuid.UUID]struct{} +var db *testenv.DB + +func TestMain(m *testing.M) { + var err error + db, err = testenv.StartPostgres(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + db.Close() + os.Exit(code) +} + +// countingUsers passes through to the real users service and counts the +// lookups, which proves that member checks are batched. +type countingUsers struct { + *user.Service calls atomic.Int32 } -func (k *knownUsers) ExistingIDs(_ context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { - k.calls.Add(1) - out := make([]uuid.UUID, 0, len(ids)) - for _, id := range ids { - if _, ok := k.ids[id]; ok { - out = append(out, id) - } - } - return out, nil +func (c *countingUsers) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { + c.calls.Add(1) + return c.Service.ExistingIDs(ctx, ids) } type fixture struct { svc *chat.Service - users *knownUsers + users *countingUsers alice, bob uuid.UUID eve uuid.UUID ctx context.Context } -func newFixture() fixture { - alice, bob, eve := ident.New(), ident.New(), ident.New() - users := &knownUsers{ids: map[uuid.UUID]struct{}{alice: {}, bob: {}, eve: {}}} +func newFixture(t *testing.T) fixture { + t.Helper() + db.Reset(t) + ctx := context.Background() + users := &countingUsers{Service: user.NewService(userstore.NewUsers(db.Pool))} + register := func(name string) uuid.UUID { + u, err := users.Register(ctx, name, name, "correct-horse-battery") + if err != nil { + t.Fatal(err) + } + return u.ID + } return fixture{ - svc: chat.NewService(store.NewInMemChats(), store.NewInMemMessages(), users), + svc: chat.NewService(store.NewChats(db.Pool), store.NewMessages(db.Pool), users), users: users, - alice: alice, - bob: bob, - eve: eve, - ctx: context.Background(), + alice: register("alice"), + bob: register("bob"), + eve: register("eve"), + ctx: ctx, } } func TestCreateChatChecksMembersInOneCall(t *testing.T) { - f := newFixture() - c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.alice, f.bob, f.eve}) + f := newFixture(t) + c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.eve, f.alice, f.bob}) if err != nil { t.Fatalf("Create error: %v", err) } @@ -67,13 +89,16 @@ func TestCreateChatChecksMembersInOneCall(t *testing.T) { t.Fatalf("member existence must be checked with one batch call, got %d", got) } got, err := f.svc.Get(f.ctx, f.bob, c.ID) - if err != nil || got.ID != c.ID || len(got.MemberIDs) != 3 { - t.Fatalf("member Get = %+v, %v", got, err) + if err != nil { + t.Fatalf("member Get error: %v", err) + } + if fmt.Sprint(got.MemberIDs) != fmt.Sprint(c.MemberIDs) || !got.CreatedAt.Equal(c.CreatedAt) || got.Title != c.Title { + t.Fatalf("Get = %+v, want what Create returned %+v", got, c) } } func TestCreateChatRejectsUnknownMember(t *testing.T) { - f := newFixture() + f := newFixture(t) _, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, ident.New()}) if apperr.KindOf(err) != apperr.KindInvalid { t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err) @@ -84,7 +109,7 @@ func TestCreateChatRejectsUnknownMember(t *testing.T) { } func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) { - f := newFixture() + f := newFixture(t) cases := map[string]struct { title string members []uuid.UUID @@ -105,8 +130,11 @@ func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) { } func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) { - f := newFixture() - c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + f := newFixture(t) + c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + if err != nil { + t.Fatal(err) + } _, foreign := f.svc.Get(f.ctx, f.eve, c.ID) _, unknown := f.svc.Get(f.ctx, f.eve, ident.New()) if !errors.Is(foreign, chat.ErrNotFound) || !errors.Is(unknown, chat.ErrNotFound) { @@ -118,9 +146,9 @@ func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) { } func TestListPagesOnlyOwnChats(t *testing.T) { - f := newFixture() + f := newFixture(t) var mine []uuid.UUID - for i := 0; i < 3; i++ { + for range 3 { c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) if err != nil { t.Fatal(err) @@ -154,9 +182,12 @@ func TestListPagesOnlyOwnChats(t *testing.T) { } func TestSendChecksAccessBeforeValidation(t *testing.T) { - f := newFixture() - c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) - _, err := f.svc.Send(f.ctx, f.eve, c.ID, " ") + f := newFixture(t) + c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + if err != nil { + t.Fatal(err) + } + _, err = f.svc.Send(f.ctx, f.eve, c.ID, " ") if !errors.Is(err, chat.ErrNotFound) { t.Fatalf("outsider with blank text: err = %v, want ErrNotFound", err) } @@ -171,8 +202,11 @@ func TestSendChecksAccessBeforeValidation(t *testing.T) { } func TestHistoryPagesAndHidesForeignChat(t *testing.T) { - f := newFixture() - c, _ := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + f := newFixture(t) + c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob}) + if err != nil { + t.Fatal(err) + } first, _ := f.svc.Send(f.ctx, f.alice, c.ID, "1") second, _ := f.svc.Send(f.ctx, f.bob, c.ID, "2") @@ -180,14 +214,14 @@ func TestHistoryPagesAndHidesForeignChat(t *testing.T) { if err != nil { t.Fatal(err) } - if len(page.Items) != 1 || page.Items[0].ID != first.ID || page.Next == nil || *page.Next != first.ID { + if len(page.Items) != 1 || page.Items[0] != first || page.Next == nil || *page.Next != first.ID { t.Fatalf("first page = %+v", page) } rest, err := f.svc.History(f.ctx, f.alice, c.ID, page.Next, 1) if err != nil { t.Fatal(err) } - if len(rest.Items) != 1 || rest.Items[0].ID != second.ID || rest.Next != nil { + if len(rest.Items) != 1 || rest.Items[0] != second || rest.Next != nil { t.Fatalf("second page = %+v", rest) } if _, err := f.svc.History(f.ctx, f.eve, c.ID, nil, 10); !errors.Is(err, chat.ErrNotFound) { diff --git a/internal/chat/store/inmem.go b/internal/chat/store/inmem.go deleted file mode 100644 index ae433acb..00000000 --- a/internal/chat/store/inmem.go +++ /dev/null @@ -1,188 +0,0 @@ -// Package store holds the repository implementations of the chats module. -// inmem.go is the in-process one: state lives only in this process and -// disappears with it by design of the lab. -package store - -import ( - "bytes" - "context" - "sort" - "sync" - - "github.com/google/uuid" - - "messenger/internal/chat" -) - -// InMemChats keeps chat rows, a per-chat member list and set, and a -// per-member index of chat ids ordered by (created_at, id), which for UUIDv7 -// ids is plain id order. One read-write mutex guards all four maps. -type InMemChats struct { - mu sync.RWMutex - rows map[uuid.UUID]chat.Chat - members map[uuid.UUID][]uuid.UUID - memberOf map[uuid.UUID]map[uuid.UUID]struct{} - byMember map[uuid.UUID][]uuid.UUID -} - -var _ chat.ChatRepository = (*InMemChats)(nil) - -func NewInMemChats() *InMemChats { - return &InMemChats{ - rows: make(map[uuid.UUID]chat.Chat), - members: make(map[uuid.UUID][]uuid.UUID), - memberOf: make(map[uuid.UUID]map[uuid.UUID]struct{}), - byMember: make(map[uuid.UUID][]uuid.UUID), - } -} - -func (r *InMemChats) Create(_ context.Context, c chat.Chat, memberIDs []uuid.UUID) error { - members := append([]uuid.UUID(nil), memberIDs...) - set := make(map[uuid.UUID]struct{}, len(members)) - for _, m := range members { - set[m] = struct{}{} - } - - r.mu.Lock() - defer r.mu.Unlock() - r.rows[c.ID] = c - r.members[c.ID] = members - r.memberOf[c.ID] = set - for _, m := range members { - r.byMember[m] = insertSorted(r.byMember[m], c.ID) - } - return nil -} - -func (r *InMemChats) GetForMember(_ context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) { - r.mu.RLock() - defer r.mu.RUnlock() - if _, ok := r.memberOf[chatID][userID]; !ok { - return chat.ChatWithMembers{}, chat.ErrNotFound - } - return r.projection(chatID), nil -} - -func (r *InMemChats) IsMember(_ context.Context, chatID, userID uuid.UUID) (bool, error) { - r.mu.RLock() - defer r.mu.RUnlock() - _, ok := r.memberOf[chatID][userID] - return ok, nil -} - -func (r *InMemChats) ListByMember(_ context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) { - r.mu.RLock() - defer r.mu.RUnlock() - ids := r.byMember[userID] - start := firstAfter(ids, after) - end := min(start+limit, len(ids)) - out := make([]chat.ChatWithMembers, 0, end-start) - for _, id := range ids[start:end] { - out = append(out, r.projection(id)) - } - return out, nil -} - -// projection copies the member list so callers never alias the store. -func (r *InMemChats) projection(chatID uuid.UUID) chat.ChatWithMembers { - return chat.ChatWithMembers{ - Chat: r.rows[chatID], - MemberIDs: append([]uuid.UUID(nil), r.members[chatID]...), - } -} - -// InMemMessages keeps one ordered history per chat. The outer lock only -// guards the map of histories; each history has its own lock, so writers to -// different chats never contend. -type InMemMessages struct { - mu sync.RWMutex - byChat map[uuid.UUID]*history -} - -type history struct { - mu sync.RWMutex - items []chat.Message -} - -var _ chat.MessageRepository = (*InMemMessages)(nil) - -func NewInMemMessages() *InMemMessages { - return &InMemMessages{byChat: make(map[uuid.UUID]*history)} -} - -// Append inserts at the sorted position; ids are monotonic, so the usual -// case is an append at the tail. -func (r *InMemMessages) Append(_ context.Context, m chat.Message) error { - h := r.historyFor(m.ChatID, true) - h.mu.Lock() - defer h.mu.Unlock() - n := len(h.items) - if n == 0 || less(h.items[n-1].ID, m.ID) { - h.items = append(h.items, m) - return nil - } - i := sort.Search(n, func(k int) bool { return !less(h.items[k].ID, m.ID) }) - h.items = append(h.items, chat.Message{}) - copy(h.items[i+1:], h.items[i:]) - h.items[i] = m - return nil -} - -func (r *InMemMessages) ListByChat(_ context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]chat.Message, error) { - h := r.historyFor(chatID, false) - if h == nil { - return []chat.Message{}, nil - } - h.mu.RLock() - defer h.mu.RUnlock() - start := 0 - if after != nil { - start = sort.Search(len(h.items), func(k int) bool { return less(*after, h.items[k].ID) }) - } - end := min(start+limit, len(h.items)) - out := make([]chat.Message, end-start) - copy(out, h.items[start:end]) - return out, nil -} - -func (r *InMemMessages) historyFor(chatID uuid.UUID, create bool) *history { - r.mu.RLock() - h := r.byChat[chatID] - r.mu.RUnlock() - if h != nil || !create { - return h - } - r.mu.Lock() - defer r.mu.Unlock() - if h = r.byChat[chatID]; h == nil { - h = &history{} - r.byChat[chatID] = h - } - return h -} - -// less orders ids lexicographically, which for UUIDv7 is creation order. -func less(a, b uuid.UUID) bool { - return bytes.Compare(a[:], b[:]) < 0 -} - -func insertSorted(ids []uuid.UUID, id uuid.UUID) []uuid.UUID { - n := len(ids) - if n == 0 || less(ids[n-1], id) { - return append(ids, id) - } - i := sort.Search(n, func(k int) bool { return !less(ids[k], id) }) - ids = append(ids, uuid.Nil) - copy(ids[i+1:], ids[i:]) - ids[i] = id - return ids -} - -// firstAfter returns the index of the first id strictly greater than after, -// or len(ids) when there is none; nil after means the start. -func firstAfter(ids []uuid.UUID, after *uuid.UUID) int { - if after == nil { - return 0 - } - return sort.Search(len(ids), func(k int) bool { return less(*after, ids[k]) }) -} diff --git a/internal/chat/store/inmem_test.go b/internal/chat/store/inmem_test.go deleted file mode 100644 index 43b998a4..00000000 --- a/internal/chat/store/inmem_test.go +++ /dev/null @@ -1,262 +0,0 @@ -package store_test - -import ( - "bytes" - "context" - "errors" - "sync" - "testing" - - "github.com/google/uuid" - - "messenger/internal/chat" - "messenger/internal/chat/store" - "messenger/internal/platform/ident" -) - -func mustChat(t *testing.T) chat.Chat { - t.Helper() - c, err := chat.NewChat(ident.New(), "title") - if err != nil { - t.Fatal(err) - } - return c -} - -func mustMessage(t *testing.T, chatID uuid.UUID) chat.Message { - t.Helper() - m, err := chat.NewMessage(ident.New(), chatID, ident.New(), "text") - if err != nil { - t.Fatal(err) - } - return m -} - -func TestChatCreateThenGetForMember(t *testing.T) { - repo := store.NewInMemChats() - ctx := context.Background() - alice, bob := ident.New(), ident.New() - c := mustChat(t) - if err := repo.Create(ctx, c, []uuid.UUID{alice, bob}); err != nil { - t.Fatal(err) - } - got, err := repo.GetForMember(ctx, c.ID, bob) - if err != nil { - t.Fatal(err) - } - if got.Chat != c || len(got.MemberIDs) != 2 || got.MemberIDs[0] != alice || got.MemberIDs[1] != bob { - t.Fatalf("GetForMember = %+v", got) - } - got.MemberIDs[0] = ident.New() - again, _ := repo.GetForMember(ctx, c.ID, alice) - if again.MemberIDs[0] != alice { - t.Fatal("GetForMember must return a copy of the member list") - } - if _, err := repo.GetForMember(ctx, c.ID, ident.New()); !errors.Is(err, chat.ErrNotFound) { - t.Fatalf("outsider must get ErrNotFound, got %v", err) - } - for who, want := range map[uuid.UUID]bool{alice: true, bob: true, ident.New(): false} { - ok, err := repo.IsMember(ctx, c.ID, who) - if err != nil || ok != want { - t.Fatalf("IsMember(%s) = %v, %v; want %v", who, ok, err, want) - } - } -} - -func TestChatUnknownID(t *testing.T) { - repo := store.NewInMemChats() - ctx := context.Background() - if _, err := repo.GetForMember(ctx, ident.New(), ident.New()); !errors.Is(err, chat.ErrNotFound) { - t.Fatalf("GetForMember err = %v, want ErrNotFound", err) - } - if ok, err := repo.IsMember(ctx, ident.New(), ident.New()); err != nil || ok { - t.Fatalf("IsMember on unknown chat = %v, %v; want false, nil", ok, err) - } -} - -func TestListByMemberFiltersOrdersAndPages(t *testing.T) { - repo := store.NewInMemChats() - ctx := context.Background() - alice, bob := ident.New(), ident.New() - var mine []chat.Chat - for i := 0; i < 5; i++ { - mine = append(mine, mustChat(t)) - // A chat without alice must never show up in her listing. - if err := repo.Create(ctx, mustChat(t), []uuid.UUID{bob}); err != nil { - t.Fatal(err) - } - } - // Insert the oldest chat last to prove the store sorts, not appends. - for _, c := range mine[1:] { - if err := repo.Create(ctx, c, []uuid.UUID{alice, bob}); err != nil { - t.Fatal(err) - } - } - if err := repo.Create(ctx, mine[0], []uuid.UUID{alice, bob}); err != nil { - t.Fatal(err) - } - - page, err := repo.ListByMember(ctx, alice, nil, 3) - if err != nil { - t.Fatal(err) - } - if len(page) != 3 || page[0].ID != mine[0].ID || page[2].ID != mine[2].ID { - t.Fatalf("first page mismatch: %v", page) - } - if len(page[0].MemberIDs) != 2 { - t.Fatalf("listing must carry members: %+v", page[0]) - } - last := page[2].ID - rest, err := repo.ListByMember(ctx, alice, &last, 10) - if err != nil { - t.Fatal(err) - } - if len(rest) != 2 || rest[0].ID != mine[3].ID || rest[1].ID != mine[4].ID { - t.Fatalf("second page mismatch: %v", rest) - } - none, err := repo.ListByMember(ctx, ident.New(), nil, 10) - if err != nil { - t.Fatal(err) - } - if none == nil || len(none) != 0 { - t.Fatalf("unknown member must get an empty non-nil slice, got %#v", none) - } -} - -func TestListByChatUnknownChatIsEmpty(t *testing.T) { - got, err := store.NewInMemMessages().ListByChat(context.Background(), ident.New(), nil, 10) - if err != nil { - t.Fatal(err) - } - if got == nil || len(got) != 0 { - t.Fatalf("want empty non-nil slice, got %#v", got) - } -} - -func TestAppendOutOfOrderIsSortedByID(t *testing.T) { - repo := store.NewInMemMessages() - ctx := context.Background() - chatID := ident.New() - first, second, third := mustMessage(t, chatID), mustMessage(t, chatID), mustMessage(t, chatID) - for _, m := range []chat.Message{third, first, second} { - if err := repo.Append(ctx, m); err != nil { - t.Fatal(err) - } - } - got, err := repo.ListByChat(ctx, chatID, nil, 10) - if err != nil { - t.Fatal(err) - } - if len(got) != 3 || got[0].ID != first.ID || got[1].ID != second.ID || got[2].ID != third.ID { - t.Fatalf("history must be ordered by id, got %v", got) - } -} - -func TestPagingWalksWholeHistoryWithoutGapsOrDuplicates(t *testing.T) { - repo := store.NewInMemMessages() - ctx := context.Background() - chatID := ident.New() - const total = 250 - var expected []uuid.UUID - for i := 0; i < total; i++ { - m := mustMessage(t, chatID) - expected = append(expected, m.ID) - if err := repo.Append(ctx, m); err != nil { - t.Fatal(err) - } - } - for _, limit := range []int{1, 7, 100} { - var seen []uuid.UUID - var after *uuid.UUID - for { - page, err := repo.ListByChat(ctx, chatID, after, limit) - if err != nil { - t.Fatal(err) - } - if len(page) == 0 { - break - } - if len(page) > limit { - t.Fatalf("limit %d: page has %d items", limit, len(page)) - } - for _, m := range page { - seen = append(seen, m.ID) - } - last := page[len(page)-1].ID - after = &last - } - if len(seen) != total { - t.Fatalf("limit %d: walked %d messages, want %d", limit, len(seen), total) - } - for i := range seen { - if seen[i] != expected[i] { - t.Fatalf("limit %d: position %d has %s, want %s", limit, i, seen[i], expected[i]) - } - } - } -} - -func TestAfterUnknownIDStartsAtNextGreater(t *testing.T) { - repo := store.NewInMemMessages() - ctx := context.Background() - chatID := ident.New() - a := mustMessage(t, chatID) - gap := ident.New() // never stored, but sorts between a and b - b := mustMessage(t, chatID) - _ = repo.Append(ctx, a) - _ = repo.Append(ctx, b) - got, _ := repo.ListByChat(ctx, chatID, &gap, 10) - if len(got) != 1 || got[0].ID != b.ID { - t.Fatalf("want only b after the gap id, got %v", got) - } -} - -func TestListByChatReturnsCopy(t *testing.T) { - repo := store.NewInMemMessages() - ctx := context.Background() - chatID := ident.New() - _ = repo.Append(ctx, mustMessage(t, chatID)) - page, _ := repo.ListByChat(ctx, chatID, nil, 10) - page[0].Text = "mutated" - again, _ := repo.ListByChat(ctx, chatID, nil, 10) - if again[0].Text != "text" { - t.Fatal("ListByChat must not expose internal storage") - } -} - -func TestConcurrentAppendsKeepEveryMessageOrdered(t *testing.T) { - repo := store.NewInMemMessages() - ctx := context.Background() - chatID := ident.New() - const clients, perClient = 10, 10 - var wg sync.WaitGroup - for c := 0; c < clients; c++ { - wg.Add(1) - go func() { - defer wg.Done() - for i := 0; i < perClient; i++ { - if err := repo.Append(ctx, mustMessage(t, chatID)); err != nil { - t.Error(err) - } - } - }() - } - wg.Wait() - got, err := repo.ListByChat(ctx, chatID, nil, clients*perClient+1) - if err != nil { - t.Fatal(err) - } - if len(got) != clients*perClient { - t.Fatalf("stored %d messages, want %d", len(got), clients*perClient) - } - unique := make(map[uuid.UUID]struct{}, len(got)) - for i, m := range got { - unique[m.ID] = struct{}{} - if i > 0 && bytes.Compare(got[i-1].ID[:], m.ID[:]) >= 0 { - t.Fatalf("history out of order at %d", i) - } - } - if len(unique) != clients*perClient { - t.Fatalf("%d unique ids, want %d", len(unique), clients*perClient) - } -} diff --git a/internal/chat/store/postgres.go b/internal/chat/store/postgres.go new file mode 100644 index 00000000..76d7b9c8 --- /dev/null +++ b/internal/chat/store/postgres.go @@ -0,0 +1,155 @@ +// Package store holds the repository implementations of the chats module. +// +// Keyset pages use `id > COALESCE($after, nil uuid)`: every UUIDv7 is +// greater than the nil UUID, so one query serves the first page and the +// following ones, and the condition stays a range the index can scan. Ids +// are UUIDv7 and created_at derives from them, so ordering by id is the +// contract order (created_at, id). +package store + +import ( + "context" + "errors" + "fmt" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "messenger/internal/chat" +) + +type Chats struct { + pool *pgxpool.Pool +} + +var _ chat.ChatRepository = (*Chats)(nil) + +func NewChats(pool *pgxpool.Pool) *Chats { + return &Chats{pool: pool} +} + +// chatWithMembers selects the projection from a chats row aliased c. +const chatWithMembers = `c.id, c.title, c.created_at, + ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)` + +func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error { + return pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error { + if _, err := tx.Exec(ctx, + `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`, + c.ID, c.Title, c.CreatedAt); err != nil { + return fmt.Errorf("insert chat: %w", err) + } + if _, err := tx.Exec(ctx, + `INSERT INTO chat_members (chat_id, user_id) SELECT $1, unnest($2::uuid[])`, + c.ID, memberIDs); err != nil { + return fmt.Errorf("insert chat members: %w", err) + } + return nil + }) +} + +func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) { + row := r.pool.QueryRow(ctx, ` + SELECT `+chatWithMembers+` + FROM chats c + WHERE c.id = $1 + AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, + chatID, userID) + c, err := scanChat(row) + if errors.Is(err, pgx.ErrNoRows) { + return chat.ChatWithMembers{}, chat.ErrNotFound + } + if err != nil { + return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", err) + } + return c, nil +} + +func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) { + var ok bool + err := r.pool.QueryRow(ctx, + `SELECT EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, + chatID, userID).Scan(&ok) + if err != nil { + return false, fmt.Errorf("check membership: %w", err) + } + return ok, nil +} + +func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) { + rows, err := r.pool.Query(ctx, ` + SELECT `+chatWithMembers+` + FROM chat_members own + JOIN chats c ON c.id = own.chat_id + WHERE own.user_id = $1 + AND own.chat_id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid) + ORDER BY own.chat_id + LIMIT $3`, + userID, after, limit) + if err != nil { + return nil, fmt.Errorf("select chats: %w", err) + } + chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.ChatWithMembers, error) { + return scanChat(row) + }) + if err != nil { + return nil, fmt.Errorf("read chats: %w", err) + } + return chats, nil +} + +func scanChat(row pgx.Row) (chat.ChatWithMembers, error) { + var c chat.ChatWithMembers + if err := row.Scan(&c.ID, &c.Title, &c.CreatedAt, &c.MemberIDs); err != nil { + return chat.ChatWithMembers{}, err + } + c.CreatedAt = c.CreatedAt.UTC() + return c, nil +} + +type Messages struct { + pool *pgxpool.Pool +} + +var _ chat.MessageRepository = (*Messages)(nil) + +func NewMessages(pool *pgxpool.Pool) *Messages { + return &Messages{pool: pool} +} + +func (r *Messages) Append(ctx context.Context, m chat.Message) error { + _, err := r.pool.Exec(ctx, + `INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)`, + m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt) + if err != nil { + return fmt.Errorf("insert message: %w", err) + } + return nil +} + +// ListByChat is the history query; scripts/explain-history.sql runs the same +// statement for the EXPLAIN evidence. +func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]chat.Message, error) { + rows, err := r.pool.Query(ctx, ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1 + AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid) + ORDER BY id + LIMIT $3`, + chatID, after, limit) + if err != nil { + return nil, fmt.Errorf("select messages: %w", err) + } + messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) { + var m chat.Message + err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt) + m.CreatedAt = m.CreatedAt.UTC() + return m, err + }) + if err != nil { + return nil, fmt.Errorf("read messages: %w", err) + } + return messages, nil +} diff --git a/internal/chat/store/postgres_test.go b/internal/chat/store/postgres_test.go new file mode 100644 index 00000000..5b005ea7 --- /dev/null +++ b/internal/chat/store/postgres_test.go @@ -0,0 +1,313 @@ +package store_test + +import ( + "bytes" + "context" + "errors" + "fmt" + "os" + "sync" + "testing" + + "github.com/google/uuid" + + "messenger/internal/chat" + "messenger/internal/chat/store" + "messenger/internal/platform/ident" + "messenger/internal/testenv" + "messenger/internal/user" + userstore "messenger/internal/user/store" +) + +var db *testenv.DB + +func TestMain(m *testing.M) { + var err error + db, err = testenv.StartPostgres(context.Background()) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + db.Close() + os.Exit(code) +} + +type fixture struct { + ctx context.Context + chats *store.Chats + messages *store.Messages +} + +func newFixture(t *testing.T) fixture { + t.Helper() + db.Reset(t) + return fixture{ctx: context.Background(), chats: store.NewChats(db.Pool), messages: store.NewMessages(db.Pool)} +} + +// mustUser stores a user row: chats and messages reference users by FK. +func (f fixture) mustUser(t *testing.T) uuid.UUID { + t.Helper() + id := ident.New() + u, err := user.New(id, "u"+id.String()[24:], "User") + if err != nil { + t.Fatal(err) + } + u.PasswordHash = "x" + if err := userstore.NewUsers(db.Pool).Create(f.ctx, u); err != nil { + t.Fatal(err) + } + return id +} + +func (f fixture) mustChat(t *testing.T, members ...uuid.UUID) chat.Chat { + t.Helper() + c, err := chat.NewChat(ident.New(), "title") + if err != nil { + t.Fatal(err) + } + if err := f.chats.Create(f.ctx, c, members); err != nil { + t.Fatal(err) + } + return c +} + +func (f fixture) newMessage(t *testing.T, chatID, sender uuid.UUID) chat.Message { + t.Helper() + m, err := chat.NewMessage(ident.New(), chatID, sender, "text") + if err != nil { + t.Fatal(err) + } + return m +} + +func TestChatCreateThenGetForMember(t *testing.T) { + f := newFixture(t) + alice, bob := f.mustUser(t), f.mustUser(t) + c := f.mustChat(t, alice, bob) + + got, err := f.chats.GetForMember(f.ctx, c.ID, bob) + if err != nil { + t.Fatal(err) + } + if got.Chat != c || len(got.MemberIDs) != 2 || got.MemberIDs[0] != alice || got.MemberIDs[1] != bob { + t.Fatalf("GetForMember = %+v, want %+v with members [alice bob]", got, c) + } + if _, err := f.chats.GetForMember(f.ctx, c.ID, f.mustUser(t)); !errors.Is(err, chat.ErrNotFound) { + t.Fatalf("outsider must get ErrNotFound, got %v", err) + } + for who, want := range map[uuid.UUID]bool{alice: true, bob: true, ident.New(): false} { + ok, err := f.chats.IsMember(f.ctx, c.ID, who) + if err != nil || ok != want { + t.Fatalf("IsMember(%s) = %v, %v; want %v", who, ok, err, want) + } + } +} + +// The chat row and its members are written in one transaction: a failing +// member insert must leave no chat behind. +func TestCreateRollsBackWhenMemberInsertFails(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c, err := chat.NewChat(ident.New(), "half-made") + if err != nil { + t.Fatal(err) + } + if err := f.chats.Create(f.ctx, c, []uuid.UUID{alice, ident.New()}); err == nil { + t.Fatal("Create with an unknown member must fail") + } + var chats, members int + if err := db.Pool.QueryRow(f.ctx, `SELECT (SELECT count(*) FROM chats), (SELECT count(*) FROM chat_members)`).Scan(&chats, &members); err != nil { + t.Fatal(err) + } + if chats != 0 || members != 0 { + t.Fatalf("chats = %d, members = %d after a failed create; want 0 and 0", chats, members) + } +} + +func TestChatUnknownID(t *testing.T) { + f := newFixture(t) + if _, err := f.chats.GetForMember(f.ctx, ident.New(), ident.New()); !errors.Is(err, chat.ErrNotFound) { + t.Fatalf("GetForMember err = %v, want ErrNotFound", err) + } + if ok, err := f.chats.IsMember(f.ctx, ident.New(), ident.New()); err != nil || ok { + t.Fatalf("IsMember on unknown chat = %v, %v; want false, nil", ok, err) + } +} + +func TestListByMemberFiltersOrdersAndPages(t *testing.T) { + f := newFixture(t) + alice, bob := f.mustUser(t), f.mustUser(t) + var mine []chat.Chat + for range 5 { + c, err := chat.NewChat(ident.New(), "title") + if err != nil { + t.Fatal(err) + } + mine = append(mine, c) + // A chat without alice must never show up in her listing. + f.mustChat(t, bob) + } + // Insert the oldest chat last to prove the store sorts, not appends. + for _, c := range append(mine[1:], mine[0]) { + if err := f.chats.Create(f.ctx, c, []uuid.UUID{alice, bob}); err != nil { + t.Fatal(err) + } + } + + page, err := f.chats.ListByMember(f.ctx, alice, nil, 3) + if err != nil { + t.Fatal(err) + } + if len(page) != 3 || page[0].ID != mine[0].ID || page[2].ID != mine[2].ID { + t.Fatalf("first page mismatch: %v", page) + } + if len(page[0].MemberIDs) != 2 || page[0].MemberIDs[0] != alice { + t.Fatalf("listing must carry members in id order: %+v", page[0]) + } + last := page[2].ID + rest, err := f.chats.ListByMember(f.ctx, alice, &last, 10) + if err != nil { + t.Fatal(err) + } + if len(rest) != 2 || rest[0].ID != mine[3].ID || rest[1].ID != mine[4].ID { + t.Fatalf("second page mismatch: %v", rest) + } + none, err := f.chats.ListByMember(f.ctx, ident.New(), nil, 10) + if err != nil { + t.Fatal(err) + } + if none == nil || len(none) != 0 { + t.Fatalf("unknown member must get an empty non-nil slice, got %#v", none) + } +} + +func TestListByChatUnknownChatIsEmpty(t *testing.T) { + f := newFixture(t) + got, err := f.messages.ListByChat(f.ctx, ident.New(), nil, 10) + if err != nil { + t.Fatal(err) + } + if got == nil || len(got) != 0 { + t.Fatalf("want empty non-nil slice, got %#v", got) + } +} + +func TestAppendOutOfOrderIsListedByID(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c := f.mustChat(t, alice) + first, second, third := f.newMessage(t, c.ID, alice), f.newMessage(t, c.ID, alice), f.newMessage(t, c.ID, alice) + for _, m := range []chat.Message{third, first, second} { + if err := f.messages.Append(f.ctx, m); err != nil { + t.Fatal(err) + } + } + got, err := f.messages.ListByChat(f.ctx, c.ID, nil, 10) + if err != nil { + t.Fatal(err) + } + if len(got) != 3 || got[0] != first || got[1] != second || got[2] != third { + t.Fatalf("history must be ordered by id and round-trip every field, got %+v", got) + } +} + +func TestPagingWalksWholeHistoryWithoutGapsOrDuplicates(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c := f.mustChat(t, alice) + const total = 250 + var expected []uuid.UUID + for range total { + m := f.newMessage(t, c.ID, alice) + expected = append(expected, m.ID) + if err := f.messages.Append(f.ctx, m); err != nil { + t.Fatal(err) + } + } + // Another chat's history must not leak into this one. + other := f.mustChat(t, alice) + if err := f.messages.Append(f.ctx, f.newMessage(t, other.ID, alice)); err != nil { + t.Fatal(err) + } + for _, limit := range []int{1, 7, 100} { + var seen []uuid.UUID + var after *uuid.UUID + for { + page, err := f.messages.ListByChat(f.ctx, c.ID, after, limit) + if err != nil { + t.Fatal(err) + } + if len(page) == 0 { + break + } + if len(page) > limit { + t.Fatalf("limit %d: page has %d items", limit, len(page)) + } + for _, m := range page { + seen = append(seen, m.ID) + } + last := page[len(page)-1].ID + after = &last + } + if len(seen) != total { + t.Fatalf("limit %d: walked %d messages, want %d", limit, len(seen), total) + } + for i := range seen { + if seen[i] != expected[i] { + t.Fatalf("limit %d: position %d has %s, want %s", limit, i, seen[i], expected[i]) + } + } + } +} + +func TestAfterUnknownIDStartsAtNextGreater(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c := f.mustChat(t, alice) + a := f.newMessage(t, c.ID, alice) + gap := ident.New() // never stored, but sorts between a and b + b := f.newMessage(t, c.ID, alice) + for _, m := range []chat.Message{a, b} { + if err := f.messages.Append(f.ctx, m); err != nil { + t.Fatal(err) + } + } + got, err := f.messages.ListByChat(f.ctx, c.ID, &gap, 10) + if err != nil { + t.Fatal(err) + } + if len(got) != 1 || got[0].ID != b.ID { + t.Fatalf("want only b after the gap id, got %v", got) + } +} + +func TestConcurrentAppendsKeepEveryMessage(t *testing.T) { + f := newFixture(t) + alice := f.mustUser(t) + c := f.mustChat(t, alice) + const clients, perClient = 10, 10 + var wg sync.WaitGroup + for range clients { + wg.Go(func() { + for range perClient { + if err := f.messages.Append(f.ctx, f.newMessage(t, c.ID, alice)); err != nil { + t.Error(err) + } + } + }) + } + wg.Wait() + got, err := f.messages.ListByChat(f.ctx, c.ID, nil, clients*perClient+1) + if err != nil { + t.Fatal(err) + } + if len(got) != clients*perClient { + t.Fatalf("stored %d messages, want %d", len(got), clients*perClient) + } + for i := 1; i < len(got); i++ { + if bytes.Compare(got[i-1].ID[:], got[i].ID[:]) >= 0 { + t.Fatalf("history out of order at %d", i) + } + } +} -- 2.54.0 From 3c1b63ed98a28a74a7fef9e484599e3417d4508e Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:00:19 +0300 Subject: [PATCH 08/21] =?UTF-8?q?=D0=A1=D1=82=D0=B0=D0=B1=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B8=D1=81=D0=BA=20Docker=20Desktop=20=D0=B2=20=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B0=D1=85=20=D0=BD=D0=B0=20Windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/testenv/testenv.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 974651e9..90974eda 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -6,6 +6,9 @@ package testenv import ( "context" "fmt" + "os" + "runtime" + "sync" "testing" "github.com/jackc/pgx/v5/pgxpool" @@ -22,6 +25,17 @@ const ( ValkeyImage = "valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11" ) +// useDockerDesktopPipe names the Docker Desktop engine pipe explicitly on +// Windows. Without DOCKER_HOST, testcontainers probes the pipe with a file +// stat, which fails while the pipe is busy; with several test packages +// starting containers at once that probe fails spuriously and the run panics. +// Dialling the named host instead waits for a free pipe instance. +var useDockerDesktopPipe = sync.OnceFunc(func() { + if runtime.GOOS == "windows" && os.Getenv("DOCKER_HOST") == "" { + _ = os.Setenv("DOCKER_HOST", "npipe:////./pipe/docker_engine") + } +}) + // DB is a migrated PostgreSQL instance. type DB struct { Pool *pgxpool.Pool @@ -30,6 +44,7 @@ type DB struct { } func StartPostgres(ctx context.Context) (*DB, error) { + useDockerDesktopPipe() c, err := tcpostgres.Run(ctx, PostgresImage, tcpostgres.WithDatabase("messenger"), tcpostgres.WithUsername("messenger"), @@ -94,6 +109,7 @@ type KV struct { } func StartValkey(ctx context.Context) (*KV, error) { + useDockerDesktopPipe() c, err := tcvalkey.Run(ctx, ValkeyImage) if err != nil { return nil, fmt.Errorf("start valkey: %w", err) -- 2.54.0 From 9890482747bf05653f81eedab9667764a758b989 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:08:39 +0300 Subject: [PATCH 09/21] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8=20=D0=B2=20?= =?UTF-8?q?Valkey=20=D0=B8=20=D0=B2=D1=85=D0=BE=D0=B4=20=D1=87=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B7=20Basic=20=D1=81=20Bearer-=D1=82=D0=BE=D0=BA=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 19 +- internal/app/app.go | 10 +- internal/app/app_test.go | 226 +++++++++++++++--------- internal/app/auth_test.go | 160 +++++++++++++++++ internal/auth/auth.go | 47 ----- internal/auth/auth_test.go | 79 --------- internal/auth/handler.go | 89 ++++++++++ internal/auth/main_test.go | 33 ++++ internal/auth/middleware.go | 48 +++++ internal/auth/repository.go | 21 +++ internal/auth/service.go | 69 ++++++++ internal/auth/service_test.go | 145 +++++++++++++++ internal/auth/session.go | 39 ++++ internal/auth/store/valkey.go | 64 +++++++ internal/platform/config/config.go | 13 ++ internal/platform/config/config_test.go | 54 ++++-- internal/platform/valkey/valkey.go | 44 +++++ internal/platform/valkey/valkey_test.go | 51 ++++++ internal/user/service.go | 10 -- internal/user/service_test.go | 6 - tools/concurrency/client.go | 30 +++- tools/concurrency/main.go | 45 +++-- tools/concurrency/main_test.go | 19 +- 23 files changed, 1047 insertions(+), 274 deletions(-) create mode 100644 internal/app/auth_test.go delete mode 100644 internal/auth/auth.go delete mode 100644 internal/auth/auth_test.go create mode 100644 internal/auth/handler.go create mode 100644 internal/auth/main_test.go create mode 100644 internal/auth/middleware.go create mode 100644 internal/auth/repository.go create mode 100644 internal/auth/service.go create mode 100644 internal/auth/service_test.go create mode 100644 internal/auth/session.go create mode 100644 internal/auth/store/valkey.go create mode 100644 internal/platform/valkey/valkey.go create mode 100644 internal/platform/valkey/valkey_test.go diff --git a/cmd/server/main.go b/cmd/server/main.go index efafc131..f3ac4f5c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,4 +1,4 @@ -// Command server starts the messenger API over PostgreSQL. +// Command server starts the messenger API over PostgreSQL and Valkey. package main import ( @@ -13,6 +13,7 @@ import ( "messenger/internal/app" "messenger/internal/platform/config" "messenger/internal/platform/postgres" + "messenger/internal/platform/valkey" "messenger/internal/server" ) @@ -44,13 +45,25 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config) error { if err := postgres.Migrate(ctx, pool); err != nil { return err } + sessions, err := valkey.NewClient(ctx, cfg.SessionStoreURL) + if err != nil { + return err + } + defer sessions.Close() ln, err := net.Listen("tcp", cfg.Addr()) if err != nil { return fmt.Errorf("listen on %s: %w", cfg.Addr(), err) } - log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String()) - a := app.New(app.Deps{Log: log, DB: pool, MaxBodyBytes: cfg.MaxBodyBytes}) + log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), + "shutdown_timeout", cfg.ShutdownTimeout.String(), "session_ttl", cfg.SessionTTL.String()) + a := app.New(app.Deps{ + Log: log, + DB: pool, + Sessions: sessions, + SessionTTL: cfg.SessionTTL, + MaxBodyBytes: cfg.MaxBodyBytes, + }) return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout) } diff --git a/internal/app/app.go b/internal/app/app.go index 59408504..6722ee20 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -5,11 +5,14 @@ package app import ( "log/slog" + "time" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v5/pgxpool" + "github.com/redis/go-redis/v9" "messenger/internal/auth" + authstore "messenger/internal/auth/store" "messenger/internal/chat" chatstore "messenger/internal/chat/store" "messenger/internal/health" @@ -28,6 +31,8 @@ type App struct { type Deps struct { Log *slog.Logger DB *pgxpool.Pool + Sessions *redis.Client + SessionTTL time.Duration MaxBodyBytes int64 } @@ -39,8 +44,11 @@ func New(d Deps) *App { probes := health.New() probes.Register(engine) + sessions := auth.NewService(users, authstore.NewSessions(d.Sessions), d.SessionTTL) + public := engine.Group("/api/v1") - protected := public.Group("", auth.RequireUser(users)) + protected := public.Group("", auth.RequireSession(sessions)) + auth.NewHandler(sessions).Register(public, protected) user.NewHandler(users).Register(public, protected) chat.NewHandler(chats).Register(protected) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 01ec1cb7..e27fd8aa 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -3,6 +3,7 @@ package app_test import ( "bytes" "context" + "encoding/base64" "encoding/json" "fmt" "io" @@ -15,6 +16,7 @@ import ( "strings" "sync" "testing" + "time" "messenger/internal/app" "messenger/internal/chat" @@ -23,16 +25,25 @@ import ( "messenger/internal/user" ) -var db *testenv.DB +var ( + db *testenv.DB + kv *testenv.KV +) func TestMain(m *testing.M) { + ctx := context.Background() var err error - db, err = testenv.StartPostgres(context.Background()) - if err != nil { + if db, err = testenv.StartPostgres(ctx); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if kv, err = testenv.StartValkey(ctx); err != nil { + db.Close() fmt.Fprintln(os.Stderr, err) os.Exit(1) } code := m.Run() + kv.Close() db.Close() os.Exit(code) } @@ -51,6 +62,7 @@ type errorBody struct { type response struct { status int requestID string + challenge string raw []byte } @@ -60,25 +72,53 @@ type tester struct { a *app.App } -// newTester assembles the application over an empty database. -func newTester(t *testing.T) *tester { - t.Helper() - db.Reset(t) - log := slog.New(slog.NewTextHandler(io.Discard, nil)) - return &tester{t: t, a: app.New(app.Deps{Log: log, DB: db.Pool, MaxBodyBytes: 64 * 1024})} +// account is a registered user with a live session. +type account struct { + user.Response + token string } -// do sends one request. A string body is sent verbatim, anything else is -// JSON-encoded; both get a JSON content type. Every response is checked for -// X-Request-Id, and error bodies for the contract envelope. -func (ts *tester) do(method, path, actor string, body any, headers map[string]string) response { +// sessionBody mirrors the contract Session schema. +type sessionBody struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt string `json:"expires_at"` + User user.Response `json:"user"` +} + +func newTester(t *testing.T) *tester { + t.Helper() + return newTesterWithTTL(t, time.Hour) +} + +// newTesterWithTTL assembles the application over empty stores. +func newTesterWithTTL(t *testing.T, ttl time.Duration) *tester { + t.Helper() + db.Reset(t) + kv.Reset(t) + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + return &tester{t: t, a: app.New(app.Deps{ + Log: log, + DB: db.Pool, + Sessions: kv.Client, + SessionTTL: ttl, + MaxBodyBytes: 64 * 1024, + })} +} + +// do sends one request, authenticated with the bearer token when it is set. +// A string body is sent verbatim, anything else is JSON-encoded; both get a +// JSON content type. Every response is checked for X-Request-Id, error +// bodies for the contract envelope, and every 401 for the challenge of the +// scheme the route expects. +func (ts *tester) do(method, path, token string, body any, headers map[string]string) response { ts.t.Helper() req := httptest.NewRequest(method, path, encode(ts.t, body)) if body != nil { req.Header.Set("Content-Type", "application/json") } - if actor != "" { - req.Header.Set("X-User-Id", actor) + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) } for k, v := range headers { req.Header.Set(k, v) @@ -86,7 +126,12 @@ func (ts *tester) do(method, path, actor string, body any, headers map[string]st rec := httptest.NewRecorder() ts.a.Engine.ServeHTTP(rec, req) - res := response{status: rec.Code, requestID: rec.Header().Get("X-Request-Id"), raw: rec.Body.Bytes()} + res := response{ + status: rec.Code, + requestID: rec.Header().Get("X-Request-Id"), + challenge: rec.Header().Get("WWW-Authenticate"), + raw: rec.Body.Bytes(), + } if res.requestID == "" { ts.t.Fatalf("%s %s: missing X-Request-Id", method, path) } @@ -99,6 +144,15 @@ func (ts *tester) do(method, path, actor string, body any, headers map[string]st ts.t.Fatalf("%s %s: bad error envelope: %s", method, path, res.raw) } } + if res.status == http.StatusUnauthorized { + scheme := "Bearer " + if path == "/api/v1/auth/sessions" { + scheme = "Basic " + } + if !strings.HasPrefix(res.challenge, scheme) { + ts.t.Fatalf("%s %s: 401 challenge = %q, want scheme %q", method, path, res.challenge, scheme) + } + } return res } @@ -146,38 +200,52 @@ func decode[T any](t *testing.T, res response) T { return out } -func (ts *tester) user(username, display string) user.Response { +// user registers a user and logs it in. +func (ts *tester) user(username, display string) account { ts.t.Helper() body := map[string]string{"username": username, "display_name": display, "password": testPassword} res := ts.do(http.MethodPost, "/api/v1/users", "", body, nil) ts.expect(res, http.StatusCreated, "") - return decode[user.Response](ts.t, res) + registered := decode[user.Response](ts.t, res) + return account{Response: registered, token: ts.login(username, testPassword).Token} } -func (ts *tester) chat(actor, title string, members ...string) chat.ChatResponse { +func basic(username, password string) map[string]string { + raw := base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) + return map[string]string{"Authorization": "Basic " + raw} +} + +func (ts *tester) login(username, password string) sessionBody { ts.t.Helper() - res := ts.do(http.MethodPost, "/api/v1/chats", actor, map[string]any{"title": title, "member_ids": members}, nil) + res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic(username, password)) + ts.expect(res, http.StatusCreated, "") + return decode[sessionBody](ts.t, res) +} + +func (ts *tester) chat(token, title string, members ...string) chat.ChatResponse { + ts.t.Helper() + res := ts.do(http.MethodPost, "/api/v1/chats", token, map[string]any{"title": title, "member_ids": members}, nil) ts.expect(res, http.StatusCreated, "") return decode[chat.ChatResponse](ts.t, res) } -func (ts *tester) message(actor, chatID, text string) chat.MessageResponse { +func (ts *tester) message(token, chatID, text string) chat.MessageResponse { ts.t.Helper() - res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", actor, map[string]string{"text": text}, nil) + res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", token, map[string]string{"text": text}, nil) ts.expect(res, http.StatusCreated, "") return decode[chat.MessageResponse](ts.t, res) } -func (ts *tester) chats(actor, query string) pagination.PageResponse[chat.ChatResponse] { +func (ts *tester) chats(token, query string) pagination.PageResponse[chat.ChatResponse] { ts.t.Helper() - res := ts.do(http.MethodGet, "/api/v1/chats"+query, actor, nil, nil) + res := ts.do(http.MethodGet, "/api/v1/chats"+query, token, nil, nil) ts.expect(res, http.StatusOK, "") return decode[pagination.PageResponse[chat.ChatResponse]](ts.t, res) } -func (ts *tester) history(actor, chatID, query string) pagination.PageResponse[chat.MessageResponse] { +func (ts *tester) history(token, chatID, query string) pagination.PageResponse[chat.MessageResponse] { ts.t.Helper() - res := ts.do(http.MethodGet, "/api/v1/chats/"+chatID+"/messages"+query, actor, nil, nil) + res := ts.do(http.MethodGet, "/api/v1/chats/"+chatID+"/messages"+query, token, nil, nil) ts.expect(res, http.StatusOK, "") return decode[pagination.PageResponse[chat.MessageResponse]](ts.t, res) } @@ -223,24 +291,21 @@ func TestRegisterUserShapeAndConflicts(t *testing.T) { ts.expect(post(map[string]any{"username": 5, "display_name": "x"}), http.StatusBadRequest, "invalid_request") } -func TestCurrentUserRequiresIdentity(t *testing.T) { +func TestCurrentUserIsTheTokenOwner(t *testing.T) { ts := newTester(t) alice := ts.user("alice", "Алиса") - me := func(actor string) response { return ts.do(http.MethodGet, "/api/v1/users/me", actor, nil, nil) } - ts.expect(me(""), http.StatusUnauthorized, "unauthorized") - ts.expect(me("garbage"), http.StatusUnauthorized, "unauthorized") - ts.expect(me("11111111-1111-4111-8111-111111111111"), http.StatusUnauthorized, "unauthorized") - res := me(alice.ID) + ts.user("bob", "Боб") + res := ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil) ts.expect(res, http.StatusOK, "") - if got := decode[user.Response](t, res); got != alice { - t.Fatalf("me = %+v, want %+v", got, alice) + if got := decode[user.Response](t, res); got != alice.Response { + t.Fatalf("me = %+v, want %+v", got, alice.Response) } } func TestChatCreationRules(t *testing.T) { ts := newTester(t) alice, bob := ts.user("alice", "A"), ts.user("bob", "B") - created := ts.chat(alice.ID, "Проект по ВНП", alice.ID, bob.ID) + created := ts.chat(alice.token, "Проект по ВНП", alice.ID, bob.ID) if created.Title != "Проект по ВНП" || len(created.MemberIDs) != 2 || created.MemberIDs[0] != alice.ID || created.MemberIDs[1] != bob.ID { t.Fatalf("chat = %+v", created) } @@ -257,7 +322,7 @@ func TestChatCreationRules(t *testing.T) { "extra field": map[string]any{"title": "t", "member_ids": []string{alice.ID}, "owner": alice.ID}, } for name, body := range invalid { - res := ts.do(http.MethodPost, "/api/v1/chats", alice.ID, body, nil) + res := ts.do(http.MethodPost, "/api/v1/chats", alice.token, body, nil) if res.status != http.StatusBadRequest { t.Errorf("%s: status = %d, want 400; body %s", name, res.status, res.raw) } @@ -268,24 +333,24 @@ func TestChatCreationRules(t *testing.T) { func TestChatVisibilityAndListing(t *testing.T) { ts := newTester(t) alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E") - created := ts.chat(alice.ID, "Контрактный тест", alice.ID, bob.ID) + created := ts.chat(alice.token, "Контрактный тест", alice.ID, bob.ID) path := "/api/v1/chats/" + created.ID - res := ts.do(http.MethodGet, path, bob.ID, nil, nil) + res := ts.do(http.MethodGet, path, bob.token, nil, nil) ts.expect(res, http.StatusOK, "") if got := decode[chat.ChatResponse](t, res); got.ID != created.ID || len(got.MemberIDs) != 2 { t.Fatalf("GET chat = %+v", got) } - ts.expect(ts.do(http.MethodGet, path, eve.ID, nil, nil), http.StatusNotFound, "not_found") - ts.expect(ts.do(http.MethodGet, "/api/v1/chats/33333333-3333-4333-8333-333333333333", alice.ID, nil, nil), http.StatusNotFound, "not_found") - ts.expect(ts.do(http.MethodGet, "/api/v1/chats/not-a-uuid", alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") + ts.expect(ts.do(http.MethodGet, path, eve.token, nil, nil), http.StatusNotFound, "not_found") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/33333333-3333-4333-8333-333333333333", alice.token, nil, nil), http.StatusNotFound, "not_found") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/not-a-uuid", alice.token, nil, nil), http.StatusBadRequest, "invalid_request") ts.expect(ts.do(http.MethodGet, path, "", nil, nil), http.StatusUnauthorized, "unauthorized") - listed := ts.chats(bob.ID, "?limit=100") + listed := ts.chats(bob.token, "?limit=100") if len(listed.Items) != 1 || listed.Items[0].ID != created.ID || listed.NextCursor != nil { t.Fatalf("bob list = %+v", listed) } - hidden := ts.do(http.MethodGet, "/api/v1/chats?limit=100", eve.ID, nil, nil) + hidden := ts.do(http.MethodGet, "/api/v1/chats?limit=100", eve.token, nil, nil) ts.expect(hidden, http.StatusOK, "") if !strings.Contains(string(hidden.raw), `"items":[]`) || !strings.Contains(string(hidden.raw), `"next_cursor":null`) { t.Fatalf("empty list must serialize items as [] and next_cursor as null: %s", hidden.raw) @@ -297,12 +362,12 @@ func TestChatListPagination(t *testing.T) { alice := ts.user("alice", "A") var expected []string for i := 0; i < 5; i++ { - expected = append(expected, ts.chat(alice.ID, fmt.Sprintf("chat %d", i), alice.ID).ID) + expected = append(expected, ts.chat(alice.token, fmt.Sprintf("chat %d", i), alice.ID).ID) } var walked []string var cursor *string for pages := 0; pages < 10; pages++ { - page := ts.chats(alice.ID, cursorQuery(2, cursor)) + page := ts.chats(alice.token, cursorQuery(2, cursor)) for _, c := range page.Items { walked = append(walked, c.ID) } @@ -316,20 +381,20 @@ func TestChatListPagination(t *testing.T) { } bob := ts.user("bob", "B") - foreign := ts.chats(alice.ID, "?limit=1").NextCursor - ts.expect(ts.do(http.MethodGet, "/api/v1/chats"+cursorQuery(50, foreign), bob.ID, nil, nil), http.StatusBadRequest, "invalid_request") - ts.expect(ts.do(http.MethodGet, "/api/v1/chats?cursor=", alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") - ts.expect(ts.do(http.MethodGet, "/api/v1/chats?limit=abc", alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") + foreign := ts.chats(alice.token, "?limit=1").NextCursor + ts.expect(ts.do(http.MethodGet, "/api/v1/chats"+cursorQuery(50, foreign), bob.token, nil, nil), http.StatusBadRequest, "invalid_request") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats?cursor=", alice.token, nil, nil), http.StatusBadRequest, "invalid_request") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats?limit=abc", alice.token, nil, nil), http.StatusBadRequest, "invalid_request") } func TestMessagesEndToEnd(t *testing.T) { ts := newTester(t) alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E") - room := ts.chat(alice.ID, "Контрактный тест", alice.ID, bob.ID) + room := ts.chat(alice.token, "Контрактный тест", alice.ID, bob.ID) path := "/api/v1/chats/" + room.ID + "/messages" - first := ts.message(alice.ID, room.ID, "Привет, мир 👋") - second := ts.message(bob.ID, room.ID, "Сообщение Боба") + first := ts.message(alice.token, room.ID, "Привет, мир 👋") + second := ts.message(bob.token, room.ID, "Сообщение Боба") if first.SenderID != alice.ID || second.SenderID != bob.ID { t.Fatalf("sender must come from X-User-Id: %+v %+v", first, second) } @@ -337,48 +402,48 @@ func TestMessagesEndToEnd(t *testing.T) { t.Fatalf("message = %+v", first) } - page1 := ts.history(bob.ID, room.ID, "?limit=1") + page1 := ts.history(bob.token, room.ID, "?limit=1") if len(page1.Items) != 1 || page1.NextCursor == nil || page1.Items[0] != first { t.Fatalf("page1 = %+v", page1) } - page2 := ts.history(alice.ID, room.ID, cursorQuery(1, page1.NextCursor)) + page2 := ts.history(alice.token, room.ID, cursorQuery(1, page1.NextCursor)) if len(page2.Items) != 1 || page2.NextCursor != nil || page2.Items[0] != second { t.Fatalf("page2 = %+v", page2) } post := func(actor string, body any) response { return ts.do(http.MethodPost, path, actor, body, nil) } - ts.expect(post(alice.ID, map[string]string{"text": ""}), http.StatusBadRequest, "invalid_request") - ts.expect(post(alice.ID, map[string]string{"text": " "}), http.StatusBadRequest, "invalid_request") - ts.expect(post(alice.ID, map[string]string{"text": strings.Repeat("я", 4001)}), http.StatusBadRequest, "invalid_request") - ts.expect(post(alice.ID, map[string]string{"text": "x", "sender_id": bob.ID}), http.StatusBadRequest, "invalid_request") - ts.expect(post(alice.ID, `{"text":`), http.StatusBadRequest, "invalid_request") - ts.expect(post(eve.ID, map[string]string{"text": "чужое"}), http.StatusNotFound, "not_found") - ts.expect(post(eve.ID, map[string]string{"text": ""}), http.StatusNotFound, "not_found") + ts.expect(post(alice.token, map[string]string{"text": ""}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.token, map[string]string{"text": " "}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4001)}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.token, map[string]string{"text": "x", "sender_id": bob.ID}), http.StatusBadRequest, "invalid_request") + ts.expect(post(alice.token, `{"text":`), http.StatusBadRequest, "invalid_request") + ts.expect(post(eve.token, map[string]string{"text": "чужое"}), http.StatusNotFound, "not_found") + ts.expect(post(eve.token, map[string]string{"text": ""}), http.StatusNotFound, "not_found") ts.expect(post("", map[string]string{"text": "x"}), http.StatusUnauthorized, "unauthorized") - ts.expect(post(alice.ID, map[string]string{"text": strings.Repeat("я", 4000)}), http.StatusCreated, "") + ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4000)}), http.StatusCreated, "") get := func(actor, query string) response { return ts.do(http.MethodGet, path+query, actor, nil, nil) } - ts.expect(get(eve.ID, ""), http.StatusNotFound, "not_found") - ts.expect(get(alice.ID, "?limit=0"), http.StatusBadRequest, "invalid_request") - ts.expect(get(alice.ID, "?limit=101"), http.StatusBadRequest, "invalid_request") - ts.expect(get(alice.ID, "?cursor=garbage"), http.StatusBadRequest, "invalid_request") + ts.expect(get(eve.token, ""), http.StatusNotFound, "not_found") + ts.expect(get(alice.token, "?limit=0"), http.StatusBadRequest, "invalid_request") + ts.expect(get(alice.token, "?limit=101"), http.StatusBadRequest, "invalid_request") + ts.expect(get(alice.token, "?cursor=garbage"), http.StatusBadRequest, "invalid_request") - other := ts.chat(alice.ID, "другой", alice.ID) - ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+other.ID+"/messages"+cursorQuery(50, page1.NextCursor), alice.ID, nil, nil), http.StatusBadRequest, "invalid_request") + other := ts.chat(alice.token, "другой", alice.ID) + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+other.ID+"/messages"+cursorQuery(50, page1.NextCursor), alice.token, nil, nil), http.StatusBadRequest, "invalid_request") } func TestMessageHistoryWalkAcrossPages(t *testing.T) { ts := newTester(t) alice := ts.user("alice", "A") - room := ts.chat(alice.ID, "t", alice.ID) + room := ts.chat(alice.token, "t", alice.ID) var expected []string for i := 0; i < 23; i++ { - expected = append(expected, ts.message(alice.ID, room.ID, fmt.Sprintf("m%d", i)).ID) + expected = append(expected, ts.message(alice.token, room.ID, fmt.Sprintf("m%d", i)).ID) } var walked []string var cursor *string for { - page := ts.history(alice.ID, room.ID, cursorQuery(5, cursor)) + page := ts.history(alice.token, room.ID, cursorQuery(5, cursor)) for _, m := range page.Items { walked = append(walked, m.ID) } @@ -395,28 +460,29 @@ func TestMessageHistoryWalkAcrossPages(t *testing.T) { func TestConcurrentSendsProduceExactlyUniqueMessages(t *testing.T) { ts := newTester(t) const clients, perClient = 10, 10 + clientsList := make([]account, 0, clients) members := make([]string, 0, clients) for i := 0; i < clients; i++ { - members = append(members, ts.user(fmt.Sprintf("client_%d", i), "C").ID) + c := ts.user(fmt.Sprintf("client_%d", i), "C") + clientsList = append(clientsList, c) + members = append(members, c.ID) } - room := ts.chat(members[0], "нагрузка", members...) + room := ts.chat(clientsList[0].token, "нагрузка", members...) var wg sync.WaitGroup - for n, actor := range members { - wg.Add(1) - go func(n int, actor string) { - defer wg.Done() + for n, c := range clientsList { + wg.Go(func() { for i := 0; i < perClient; i++ { - res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", actor, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, nil) + res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", c.token, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, nil) if res.status != http.StatusCreated { t.Errorf("client %d: status %d body %s", n, res.status, res.raw) } } - }(n, actor) + }) } wg.Wait() - page := ts.history(members[0], room.ID, "?limit=100") + page := ts.history(clientsList[0].token, room.ID, "?limit=100") if len(page.Items) != clients*perClient || page.NextCursor != nil { t.Fatalf("got %d messages, next=%v; want %d and null", len(page.Items), page.NextCursor, clients*perClient) } diff --git a/internal/app/auth_test.go b/internal/app/auth_test.go new file mode 100644 index 00000000..3ee7d0ec --- /dev/null +++ b/internal/app/auth_test.go @@ -0,0 +1,160 @@ +package app_test + +import ( + "encoding/base64" + "encoding/json" + "net/http" + "strings" + "testing" + "time" + + "messenger/internal/user" +) + +const ( + basicChallenge = `Basic realm="messenger", charset="UTF-8"` + bearerChallenge = `Bearer realm="messenger"` +) + +func TestLoginReturnsBearerSession(t *testing.T) { + ts := newTester(t) + alice := ts.user("alice", "Алиса") + before := time.Now() + + res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)) + ts.expect(res, http.StatusCreated, "") + s := decode[sessionBody](t, res) + + if s.TokenType != "Bearer" || len(s.Token) != 43 || s.Token == alice.token { + t.Fatalf("session = %+v; want a fresh 43-character Bearer token", s) + } + if s.User != alice.Response { + t.Fatalf("user = %+v, want %+v", s.User, alice.Response) + } + expires, err := time.Parse(time.RFC3339, s.ExpiresAt) + if err != nil || !strings.HasSuffix(s.ExpiresAt, "Z") { + t.Fatalf("expires_at = %q: %v", s.ExpiresAt, err) + } + if ttl := expires.Sub(before); ttl < 59*time.Minute || ttl > 61*time.Minute { + t.Fatalf("expires_at is %v after login, want the 1h TTL", ttl) + } + if strings.Contains(string(res.raw), "password") || strings.Contains(string(res.raw), "argon2") { + t.Fatalf("session response leaks password data: %s", res.raw) + } +} + +func TestLoginFailuresAreUniformBasicChallenges(t *testing.T) { + ts := newTester(t) + ts.user("alice", "Алиса") + encoded := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } + cases := map[string]map[string]string{ + "wrong password": basic("alice", testPassword+"!"), + "unknown user": basic("mallory", testPassword), + "no header": nil, + "bearer scheme": {"Authorization": "Bearer " + strings.Repeat("A", 43)}, + "not base64": {"Authorization": "Basic !!!"}, + "no colon": {"Authorization": "Basic " + encoded("alice")}, + } + messages := map[string]string{} + for name, headers := range cases { + res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, headers) + ts.expect(res, http.StatusUnauthorized, "unauthorized") + if res.challenge != basicChallenge { + t.Errorf("%s: WWW-Authenticate = %q, want %q", name, res.challenge, basicChallenge) + } + var eb errorBody + _ = json.Unmarshal(res.raw, &eb) + messages[name] = eb.Error.Message + } + if messages["wrong password"] != messages["unknown user"] { + t.Fatalf("wrong password and unknown user must look the same: %q vs %q", messages["wrong password"], messages["unknown user"]) + } +} + +// The password is UTF-8 and may contain colons; only the first colon of the +// Basic credentials separates the username (contracts/README.md). +func TestLoginAcceptsColonAndUnicodePasswords(t *testing.T) { + ts := newTester(t) + const password = "пароль:с:двоеточием 👋" + body := map[string]string{"username": "alice", "display_name": "Алиса", "password": password} + ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "") + s := ts.login("alice", password) + if s.User.Username != "alice" { + t.Fatalf("logged in as %+v", s.User) + } +} + +func TestProtectedRoutesAcceptOnlyBearer(t *testing.T) { + ts := newTester(t) + alice, eve := ts.user("alice", "A"), ts.user("eve", "E") + me := func(headers map[string]string) response { + return ts.do(http.MethodGet, "/api/v1/users/me", "", nil, headers) + } + rejected := map[string]map[string]string{ + "no header": nil, + "basic with valid credentials": basic("alice", testPassword), + "x-user-id alone": {"X-User-Id": alice.ID}, + "unknown token": {"Authorization": "Bearer " + strings.Repeat("A", 43)}, + "empty token": {"Authorization": "Bearer "}, + "scheme only": {"Authorization": "Bearer"}, + "other scheme": {"Authorization": "Token " + alice.token}, + "two tokens": {"Authorization": "Bearer " + alice.token + " " + eve.token}, + } + for name, headers := range rejected { + res := me(headers) + ts.expect(res, http.StatusUnauthorized, "unauthorized") + if res.challenge != bearerChallenge { + t.Errorf("%s: WWW-Authenticate = %q, want %q", name, res.challenge, bearerChallenge) + } + } + accepted := map[string]map[string]string{ + "lowercase scheme": {"Authorization": "bearer " + alice.token}, + "several spaces": {"Authorization": "Bearer " + alice.token}, + "x-user-id is ignored": {"Authorization": "Bearer " + alice.token, "X-User-Id": eve.ID}, + } + for name, headers := range accepted { + res := me(headers) + ts.expect(res, http.StatusOK, "") + if got := decode[user.Response](t, res); got.ID != alice.ID { + t.Errorf("%s: caller = %s, want alice", name, got.ID) + } + } +} + +func TestLogoutRevokesOnlyTheCurrentToken(t *testing.T) { + ts := newTester(t) + alice := ts.user("alice", "A") + other := ts.login("alice", testPassword) + + res := ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil) + ts.expect(res, http.StatusNoContent, "") + if len(res.raw) != 0 { + t.Fatalf("204 must have no body, got %q", res.raw) + } + + for _, again := range []response{ + ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), + ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil), + } { + ts.expect(again, http.StatusUnauthorized, "unauthorized") + if again.challenge != bearerChallenge { + t.Fatalf("revoked token: WWW-Authenticate = %q, want %q", again.challenge, bearerChallenge) + } + } + ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", other.Token, nil, nil), http.StatusOK, "") +} + +func TestSessionExpiresAfterTTL(t *testing.T) { + ts := newTesterWithTTL(t, time.Second) + alice := ts.user("alice", "A") + ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusOK, "") + time.Sleep(1200 * time.Millisecond) + ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusUnauthorized, "unauthorized") +} + +func TestRegistrationIsPublicAndIgnoresAuthorization(t *testing.T) { + ts := newTester(t) + body := map[string]string{"username": "alice", "display_name": "A", "password": testPassword} + headers := map[string]string{"Authorization": "Bearer garbage"} + ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, headers), http.StatusCreated, "") +} diff --git a/internal/auth/auth.go b/internal/auth/auth.go deleted file mode 100644 index 20af4f49..00000000 --- a/internal/auth/auth.go +++ /dev/null @@ -1,47 +0,0 @@ -// Package auth identifies the caller. In this lab the identity is the -// X-User-Id header naming a registered user: identification, not -// authentication, by design of the assignment. -package auth - -import ( - "context" - - "github.com/gin-gonic/gin" - "github.com/google/uuid" - - "messenger/internal/platform/apperr" - "messenger/internal/platform/httpx" - "messenger/internal/platform/ident" -) - -type UserLookup interface { - Exists(ctx context.Context, id uuid.UUID) (bool, error) -} - -// RequireUser answers 401 unless X-User-Id names a registered user, and -// records that user for the handlers behind it. -func RequireUser(users UserLookup) gin.HandlerFunc { - return func(c *gin.Context) { - raw := c.GetHeader("X-User-Id") - if raw == "" { - httpx.Error(c, apperr.Unauthorized("X-User-Id header is required")) - return - } - id, err := ident.Parse(raw) - if err != nil { - httpx.Error(c, apperr.Unauthorized("X-User-Id must be the UUID of a registered user")) - return - } - ok, err := users.Exists(c.Request.Context(), id) - if err != nil { - httpx.Error(c, err) - return - } - if !ok { - httpx.Error(c, apperr.Unauthorized("X-User-Id must be the UUID of a registered user")) - return - } - httpx.SetUserID(c, id) - c.Next() - } -} diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go deleted file mode 100644 index 7630c4a5..00000000 --- a/internal/auth/auth_test.go +++ /dev/null @@ -1,79 +0,0 @@ -package auth_test - -import ( - "context" - "encoding/json" - "io" - "log/slog" - "net/http" - "net/http/httptest" - "testing" - - "github.com/gin-gonic/gin" - "github.com/google/uuid" - - "messenger/internal/auth" - "messenger/internal/platform/httpx" - "messenger/internal/platform/ident" -) - -type lookup map[uuid.UUID]struct{} - -func (l lookup) Exists(_ context.Context, id uuid.UUID) (bool, error) { - _, ok := l[id] - return ok, nil -} - -func echoActor(c *gin.Context, actor uuid.UUID) { - httpx.JSON(c, http.StatusOK, map[string]string{"id": actor.String()}) -} - -func newEngine(users lookup) *gin.Engine { - e := httpx.NewEngine(slog.New(slog.NewTextHandler(io.Discard, nil)), 1024) - e.GET("/me", auth.RequireUser(users), httpx.WithUser(echoActor)) - return e -} - -func call(e *gin.Engine, header string) (*httptest.ResponseRecorder, string) { - req := httptest.NewRequest(http.MethodGet, "/me", nil) - if header != "" { - req.Header.Set("X-User-Id", header) - } - rec := httptest.NewRecorder() - e.ServeHTTP(rec, req) - var body struct { - ID string `json:"id"` - Error struct { - Code string `json:"code"` - } `json:"error"` - } - _ = json.Unmarshal(rec.Body.Bytes(), &body) - if body.ID != "" { - return rec, body.ID - } - return rec, body.Error.Code -} - -func TestKnownUserReachesHandlerWithItsID(t *testing.T) { - alice := ident.New() - rec, got := call(newEngine(lookup{alice: {}}), alice.String()) - if rec.Code != http.StatusOK || got != alice.String() { - t.Fatalf("status %d, handler saw %q, want %s", rec.Code, got, alice) - } -} - -func TestMiddlewareRejectsMissingMalformedAndUnknownWith401(t *testing.T) { - alice := ident.New() - e := newEngine(lookup{alice: {}}) - for name, header := range map[string]string{ - "missing": "", - "malformed": "not-a-uuid", - "braces": "{" + alice.String() + "}", - "unknown": ident.New().String(), - } { - rec, code := call(e, header) - if rec.Code != http.StatusUnauthorized || code != "unauthorized" { - t.Errorf("%s: status = %d code = %q, want 401 unauthorized", name, rec.Code, code) - } - } -} diff --git a/internal/auth/handler.go b/internal/auth/handler.go new file mode 100644 index 00000000..e8c8ccef --- /dev/null +++ b/internal/auth/handler.go @@ -0,0 +1,89 @@ +package auth + +import ( + "errors" + "net/http" + + "github.com/gin-gonic/gin" + + "messenger/internal/platform/apperr" + "messenger/internal/platform/httpx" + "messenger/internal/user" +) + +// Challenges sent with every 401: Basic on the login endpoint, Bearer on +// every protected one. +const ( + basicChallenge = `Basic realm="messenger", charset="UTF-8"` + bearerChallenge = `Bearer realm="messenger"` +) + +var errCredentialsRequired = apperr.Unauthorized("basic credentials are required") + +type Handler struct { + sessions *Service +} + +func NewHandler(sessions *Service) *Handler { + return &Handler{sessions: sessions} +} + +// Register mounts login on the public router and logout on the router +// guarded by RequireSession. +func (h *Handler) Register(public, protected gin.IRouter) { + public.POST("/auth/sessions", h.login) + protected.DELETE("/auth/sessions/current", h.logout) +} + +type sessionResponse struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt string `json:"expires_at"` + User user.Response `json:"user"` +} + +// login accepts only the Authorization header; the body is not read. +// BasicAuth splits at the first colon, so a password may contain colons. +func (h *Handler) login(c *gin.Context) { + username, password, ok := c.Request.BasicAuth() + if !ok { + unauthorized(c, basicChallenge, errCredentialsRequired) + return + } + issued, err := h.sessions.Login(c.Request.Context(), username, password) + if err != nil { + unauthorized(c, basicChallenge, err) + return + } + httpx.JSON(c, http.StatusCreated, sessionResponse{ + Token: issued.Token, + TokenType: "Bearer", + ExpiresAt: httpx.Timestamp(issued.ExpiresAt), + User: user.ToResponse(issued.User), + }) +} + +func (h *Handler) logout(c *gin.Context) { + token, ok := c.Get(keyToken) + if !ok { + httpx.Error(c, errors.New("logout route is missing the RequireSession middleware")) + return + } + if err := h.sessions.Logout(c.Request.Context(), token.(string)); err != nil { + httpx.Error(c, err) + return + } + // Status alone is buffered by gin; writing the header now sends the 204 + // with an empty body. + c.Status(http.StatusNoContent) + c.Writer.WriteHeaderNow() +} + +// unauthorized adds the challenge only to a 401: an unavailable store is a +// 503 and must not invite the client to retry its credentials. +func unauthorized(c *gin.Context, challenge string, err error) { + if apperr.KindOf(err) == apperr.KindUnauthorized { + c.Header("WWW-Authenticate", challenge) + } + httpx.Error(c, err) +} diff --git a/internal/auth/main_test.go b/internal/auth/main_test.go new file mode 100644 index 00000000..a542ee95 --- /dev/null +++ b/internal/auth/main_test.go @@ -0,0 +1,33 @@ +package auth_test + +import ( + "context" + "fmt" + "os" + "testing" + + "messenger/internal/testenv" +) + +var ( + db *testenv.DB + kv *testenv.KV +) + +func TestMain(m *testing.M) { + ctx := context.Background() + var err error + if db, err = testenv.StartPostgres(ctx); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if kv, err = testenv.StartValkey(ctx); err != nil { + db.Close() + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + code := m.Run() + kv.Close() + db.Close() + os.Exit(code) +} diff --git a/internal/auth/middleware.go b/internal/auth/middleware.go new file mode 100644 index 00000000..13dbeeae --- /dev/null +++ b/internal/auth/middleware.go @@ -0,0 +1,48 @@ +package auth + +import ( + "strings" + + "github.com/gin-gonic/gin" + + "messenger/internal/platform/httpx" +) + +const keyToken = "messenger.session_token" + +// RequireSession admits a request only with Authorization: Bearer of +// a live session and records its owner for the handlers behind it. Any other +// identification, including Basic credentials and X-User-Id, is ignored, so +// it can neither grant access nor change the caller. +func RequireSession(sessions *Service) gin.HandlerFunc { + return func(c *gin.Context) { + token, ok := bearerToken(c.GetHeader("Authorization")) + if !ok { + unauthorized(c, bearerChallenge, ErrUnauthenticated) + return + } + userID, err := sessions.Authenticate(c.Request.Context(), token) + if err != nil { + unauthorized(c, bearerChallenge, err) + return + } + httpx.SetUserID(c, userID) + c.Set(keyToken, token) + c.Next() + } +} + +// bearerToken parses "Bearer ": the scheme is case-insensitive and +// may be followed by several spaces (RFC 7235); the token itself never +// contains whitespace. +func bearerToken(header string) (string, bool) { + scheme, rest, found := strings.Cut(header, " ") + if !found || !strings.EqualFold(scheme, "Bearer") { + return "", false + } + token := strings.TrimLeft(rest, " ") + if token == "" || strings.ContainsAny(token, " \t") { + return "", false + } + return token, true +} diff --git a/internal/auth/repository.go b/internal/auth/repository.go new file mode 100644 index 00000000..1893e795 --- /dev/null +++ b/internal/auth/repository.go @@ -0,0 +1,21 @@ +package auth + +import ( + "context" + "errors" + + "github.com/google/uuid" +) + +// ErrSessionNotFound means the token is unknown, expired or revoked; the +// store cannot tell these apart, and the client must not either. +var ErrSessionNotFound = errors.New("session not found") + +type Repository interface { + // Create stores the session until its absolute ExpiresAt. + Create(ctx context.Context, s Session) error + // UserID returns the owner of a live session or ErrSessionNotFound. + UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) + // Delete revokes the session; deleting a missing one is not an error. + Delete(ctx context.Context, tokenHash string) error +} diff --git a/internal/auth/service.go b/internal/auth/service.go new file mode 100644 index 00000000..15ef9abb --- /dev/null +++ b/internal/auth/service.go @@ -0,0 +1,69 @@ +package auth + +import ( + "context" + "errors" + "time" + + "github.com/google/uuid" + + "messenger/internal/platform/apperr" + "messenger/internal/user" +) + +var ErrUnauthenticated = apperr.Unauthorized("a valid bearer token is required") + +// Credentials is what this module needs from the users module: the user +// whose password matches, or user.ErrInvalidCredentials. +type Credentials interface { + Authenticate(ctx context.Context, username, password string) (user.User, error) +} + +// Issued is a new session as the client sees it; Token is shown only here. +type Issued struct { + Token string + ExpiresAt time.Time + User user.User +} + +type Service struct { + users Credentials + sessions Repository + ttl time.Duration +} + +func NewService(users Credentials, sessions Repository, ttl time.Duration) *Service { + return &Service{users: users, sessions: sessions, ttl: ttl} +} + +// Login checks the credentials first, so a store outage never hands out a +// token without a stored session. The expiry is fixed here and never +// extended; it is truncated to milliseconds, the precision of both the API +// timestamps and the store. +func (s *Service) Login(ctx context.Context, username, password string) (Issued, error) { + u, err := s.users.Authenticate(ctx, username, password) + if err != nil { + return Issued{}, err + } + token := newToken() + expiresAt := time.Now().Add(s.ttl).Truncate(time.Millisecond).UTC() + if err := s.sessions.Create(ctx, Session{TokenHash: HashToken(token), UserID: u.ID, ExpiresAt: expiresAt}); err != nil { + return Issued{}, err + } + return Issued{Token: token, ExpiresAt: expiresAt, User: u}, nil +} + +// Authenticate returns the owner of the token's live session. A store +// failure is returned as is, never as ErrUnauthenticated: an outage must not +// look like a bad token to the client. +func (s *Service) Authenticate(ctx context.Context, token string) (uuid.UUID, error) { + id, err := s.sessions.UserID(ctx, HashToken(token)) + if errors.Is(err, ErrSessionNotFound) { + return uuid.Nil, ErrUnauthenticated + } + return id, err +} + +func (s *Service) Logout(ctx context.Context, token string) error { + return s.sessions.Delete(ctx, HashToken(token)) +} diff --git a/internal/auth/service_test.go b/internal/auth/service_test.go new file mode 100644 index 00000000..fb5d767b --- /dev/null +++ b/internal/auth/service_test.go @@ -0,0 +1,145 @@ +package auth_test + +import ( + "context" + "encoding/base64" + "errors" + "strings" + "testing" + "time" + + "messenger/internal/auth" + authstore "messenger/internal/auth/store" + "messenger/internal/platform/apperr" + "messenger/internal/user" + userstore "messenger/internal/user/store" +) + +const pw = "correct-horse-battery" + +type fixture struct { + ctx context.Context + svc *auth.Service + alice user.User +} + +// newFixture wires the real users service and session store over empty +// databases and registers alice. +func newFixture(t *testing.T, ttl time.Duration) fixture { + t.Helper() + db.Reset(t) + kv.Reset(t) + ctx := context.Background() + users := user.NewService(userstore.NewUsers(db.Pool)) + alice, err := users.Register(ctx, "alice", "Алиса", pw) + if err != nil { + t.Fatal(err) + } + return fixture{ctx: ctx, svc: auth.NewService(users, authstore.NewSessions(kv.Client), ttl), alice: alice} +} + +func TestLoginIssuesOpaqueTokenBackedByStoredSession(t *testing.T) { + f := newFixture(t, time.Hour) + before := time.Now() + issued, err := f.svc.Login(f.ctx, "alice", pw) + after := time.Now() + if err != nil { + t.Fatalf("Login: %v", err) + } + raw, err := base64.RawURLEncoding.DecodeString(issued.Token) + if err != nil || len(raw) != 32 { + t.Fatalf("token %q must be 32 random bytes in base64url, got %d bytes, %v", issued.Token, len(raw), err) + } + if strings.Contains(issued.Token, f.alice.ID.String()) || strings.Contains(issued.Token, "alice") { + t.Fatal("token must not carry the identity") + } + if issued.User != f.alice { + t.Fatalf("User = %+v, want alice", issued.User) + } + if issued.ExpiresAt.Before(before.Add(time.Hour).Add(-time.Millisecond)) || issued.ExpiresAt.After(after.Add(time.Hour)) { + t.Fatalf("ExpiresAt = %v, want login time + 1h", issued.ExpiresAt) + } + + key := "session:" + auth.HashToken(issued.Token) + owner, err := kv.Client.Get(f.ctx, key).Result() + if err != nil || owner != f.alice.ID.String() { + t.Fatalf("stored owner = %q, %v; want alice", owner, err) + } + expireAt, err := kv.Client.PExpireTime(f.ctx, key).Result() + if err != nil || expireAt != time.Duration(issued.ExpiresAt.UnixMilli())*time.Millisecond { + t.Fatalf("store expiry = %v, %v; want the absolute ExpiresAt %d ms", expireAt, err, issued.ExpiresAt.UnixMilli()) + } + keys, err := kv.Client.Keys(f.ctx, "*").Result() + if err != nil { + t.Fatal(err) + } + for _, k := range keys { + if strings.Contains(k, issued.Token) { + t.Fatal("the raw token must never be stored") + } + } +} + +func TestLoginWithBadCredentialsCreatesNoSession(t *testing.T) { + f := newFixture(t, time.Hour) + for name, in := range map[string][2]string{ + "wrong password": {"alice", pw + "!"}, + "unknown user": {"mallory", pw}, + } { + if _, err := f.svc.Login(f.ctx, in[0], in[1]); !errors.Is(err, user.ErrInvalidCredentials) { + t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err) + } + } + if n, err := kv.Client.DBSize(f.ctx).Result(); err != nil || n != 0 { + t.Fatalf("sessions = %d, %v; want none", n, err) + } +} + +func TestAuthenticateResolvesTheOwner(t *testing.T) { + f := newFixture(t, time.Hour) + first, _ := f.svc.Login(f.ctx, "alice", pw) + second, _ := f.svc.Login(f.ctx, "alice", pw) + if first.Token == second.Token { + t.Fatal("every login must get its own token") + } + for _, token := range []string{first.Token, second.Token} { + id, err := f.svc.Authenticate(f.ctx, token) + if err != nil || id != f.alice.ID { + t.Fatalf("Authenticate = %v, %v; want alice", id, err) + } + } + _, err := f.svc.Authenticate(f.ctx, "unknown-token") + if !errors.Is(err, auth.ErrUnauthenticated) || apperr.KindOf(err) != apperr.KindUnauthorized { + t.Fatalf("unknown token: err = %v, want ErrUnauthenticated", err) + } +} + +func TestLogoutRevokesOnlyThatSession(t *testing.T) { + f := newFixture(t, time.Hour) + first, _ := f.svc.Login(f.ctx, "alice", pw) + second, _ := f.svc.Login(f.ctx, "alice", pw) + if err := f.svc.Logout(f.ctx, first.Token); err != nil { + t.Fatalf("Logout: %v", err) + } + if _, err := f.svc.Authenticate(f.ctx, first.Token); !errors.Is(err, auth.ErrUnauthenticated) { + t.Fatalf("revoked token: err = %v, want ErrUnauthenticated", err) + } + if id, err := f.svc.Authenticate(f.ctx, second.Token); err != nil || id != f.alice.ID { + t.Fatalf("other session: %v, %v; want still valid", id, err) + } +} + +func TestSessionExpiresAfterTTL(t *testing.T) { + f := newFixture(t, time.Second) + issued, err := f.svc.Login(f.ctx, "alice", pw) + if err != nil { + t.Fatal(err) + } + if _, err := f.svc.Authenticate(f.ctx, issued.Token); err != nil { + t.Fatalf("fresh session: %v", err) + } + time.Sleep(time.Until(issued.ExpiresAt) + 100*time.Millisecond) + if _, err := f.svc.Authenticate(f.ctx, issued.Token); !errors.Is(err, auth.ErrUnauthenticated) { + t.Fatalf("expired session: err = %v, want ErrUnauthenticated", err) + } +} diff --git a/internal/auth/session.go b/internal/auth/session.go new file mode 100644 index 00000000..daedb116 --- /dev/null +++ b/internal/auth/session.go @@ -0,0 +1,39 @@ +// Package auth authenticates callers. +// +// A login with Basic credentials creates a server-side session and returns an +// opaque bearer token; every protected request presents the token, and logout +// deletes the session, which revokes the token at once. The store keeps only +// the SHA-256 of a token: a token is a random 256-bit value, so a fast digest +// cannot be reversed by guessing, and a leaked store yields no usable tokens. +// Passwords need the opposite treatment (a slow KDF), see platform/password. +package auth + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "time" + + "github.com/google/uuid" +) + +type Session struct { + TokenHash string + UserID uuid.UUID + ExpiresAt time.Time +} + +// newToken returns 32 random bytes in base64url: 43 characters, 256 bits. +func newToken() string { + raw := make([]byte, 32) + // Since Go 1.24 crypto/rand.Read never returns an error. + _, _ = rand.Read(raw) + return base64.RawURLEncoding.EncodeToString(raw) +} + +// HashToken is the key under which a token's session is stored. +func HashToken(token string) string { + sum := sha256.Sum256([]byte(token)) + return hex.EncodeToString(sum[:]) +} diff --git a/internal/auth/store/valkey.go b/internal/auth/store/valkey.go new file mode 100644 index 00000000..ff56ad66 --- /dev/null +++ b/internal/auth/store/valkey.go @@ -0,0 +1,64 @@ +// Package store holds the session store implementations. +package store + +import ( + "context" + "errors" + "fmt" + + "github.com/google/uuid" + "github.com/redis/go-redis/v9" + + "messenger/internal/auth" + "messenger/internal/platform/apperr" +) + +const keyPrefix = "session:" + +// Sessions keeps one key per session, session:, holding the +// owner's id. Valkey expires the key itself, so no cleanup job is needed. +type Sessions struct { + client *redis.Client +} + +var _ auth.Repository = (*Sessions)(nil) + +func NewSessions(client *redis.Client) *Sessions { + return &Sessions{client: client} +} + +// Create sets an absolute expiry (PXAT): a restart of the store replays it +// from the append-only file and does not extend the session. +func (r *Sessions) Create(ctx context.Context, s auth.Session) error { + err := r.client.Do(ctx, "SET", keyPrefix+s.TokenHash, s.UserID.String(), "PXAT", s.ExpiresAt.UnixMilli()).Err() + if err != nil { + return unavailable(err) + } + return nil +} + +func (r *Sessions) UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) { + raw, err := r.client.Get(ctx, keyPrefix+tokenHash).Result() + if errors.Is(err, redis.Nil) { + return uuid.Nil, auth.ErrSessionNotFound + } + if err != nil { + return uuid.Nil, unavailable(err) + } + id, err := uuid.Parse(raw) + if err != nil { + return uuid.Nil, fmt.Errorf("session holds a malformed user id: %w", err) + } + return id, nil +} + +func (r *Sessions) Delete(ctx context.Context, tokenHash string) error { + if err := r.client.Del(ctx, keyPrefix+tokenHash).Err(); err != nil { + return unavailable(err) + } + return nil +} + +func unavailable(err error) error { + return apperr.Wrap(apperr.KindUnavailable, "session store unavailable", err) +} diff --git a/internal/platform/config/config.go b/internal/platform/config/config.go index 03961ddb..30100b71 100644 --- a/internal/platform/config/config.go +++ b/internal/platform/config/config.go @@ -23,6 +23,8 @@ type Config struct { ShutdownTimeout time.Duration MaxBodyBytes int64 DatabaseURL string + SessionStoreURL string + SessionTTL time.Duration } // Lookup reads one variable, reporting whether it was set; os.LookupEnv @@ -37,6 +39,7 @@ func Load(lookup Lookup) (Config, error) { LogLevel: slog.LevelInfo, ShutdownTimeout: 8 * time.Second, MaxBodyBytes: 64 * 1024, + SessionTTL: time.Hour, } if v, ok := lookup("APP_HOST"); ok { if strings.TrimSpace(v) == "" { @@ -75,10 +78,20 @@ func Load(lookup Lookup) (Config, error) { } cfg.MaxBodyBytes = n } + if v, ok := lookup("SESSION_TTL_SECONDS"); ok { + n, err := strconv.ParseUint(v, 10, 32) + if err != nil || n == 0 { + return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive whole number of seconds, got %q", v) + } + cfg.SessionTTL = time.Duration(n) * time.Second + } var err error if cfg.DatabaseURL, err = secret(lookup, "DATABASE_URL_FILE"); err != nil { return Config{}, err } + if cfg.SessionStoreURL, err = secret(lookup, "SESSION_STORE_URL_FILE"); err != nil { + return Config{}, err + } return cfg, nil } diff --git a/internal/platform/config/config_test.go b/internal/platform/config/config_test.go index af2881ab..2288dcd2 100644 --- a/internal/platform/config/config_test.go +++ b/internal/platform/config/config_test.go @@ -27,12 +27,18 @@ func secretFile(t *testing.T, content string) string { return path } -const databaseURL = "postgres://app:s3cret@db:5432/messenger" +const ( + databaseURL = "postgres://app:s3cret@db:5432/messenger" + sessionStoreURL = "redis://:s3cret@valkey:6379/0" +) // required holds the variables without defaults, merged with extra. func required(t *testing.T, extra map[string]string) map[string]string { t.Helper() - values := map[string]string{"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n")} + values := map[string]string{ + "DATABASE_URL_FILE": secretFile(t, databaseURL+"\n"), + "SESSION_STORE_URL_FILE": secretFile(t, sessionStoreURL+"\r\n"), + } for k, v := range extra { values[k] = v } @@ -53,26 +59,33 @@ func TestDefaults(t *testing.T) { if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 { t.Fatalf("cfg = %+v", cfg) } - if cfg.DatabaseURL != databaseURL { - t.Fatalf("DatabaseURL = %q, want the file content without the newline", cfg.DatabaseURL) + if cfg.DatabaseURL != databaseURL || cfg.SessionStoreURL != sessionStoreURL { + t.Fatalf("urls = %q, %q; want the file contents without line endings", cfg.DatabaseURL, cfg.SessionStoreURL) + } + if cfg.SessionTTL != time.Hour { + t.Fatalf("SessionTTL = %v, want 1h", cfg.SessionTTL) } } func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) { - cases := map[string]map[string]string{ - "unset": nil, - "empty path": {"DATABASE_URL_FILE": ""}, - "no file": {"DATABASE_URL_FILE": filepath.Join(t.TempDir(), "missing")}, - "blank file": {"DATABASE_URL_FILE": secretFile(t, " \r\n")}, - } - for name, values := range cases { - _, err := config.Load(env(values)) - if err == nil { - t.Errorf("%s: want error", name) - continue + for _, key := range []string{"DATABASE_URL_FILE", "SESSION_STORE_URL_FILE"} { + cases := map[string]func(map[string]string){ + "unset": func(v map[string]string) { delete(v, key) }, + "empty path": func(v map[string]string) { v[key] = "" }, + "no file": func(v map[string]string) { v[key] = filepath.Join(t.TempDir(), "missing") }, + "blank file": func(v map[string]string) { v[key] = secretFile(t, " \r\n") }, } - if !strings.Contains(err.Error(), "DATABASE_URL_FILE") || strings.Contains(err.Error(), "s3cret") { - t.Errorf("%s: error must name the variable and hide the secret: %v", name, err) + for name, breakIt := range cases { + values := required(t, nil) + breakIt(values) + _, err := config.Load(env(values)) + if err == nil { + t.Errorf("%s %s: want error", key, name) + continue + } + if !strings.Contains(err.Error(), key) || strings.Contains(err.Error(), "s3cret") { + t.Errorf("%s %s: error must name the variable and hide the secret: %v", key, name, err) + } } } } @@ -85,6 +98,7 @@ func TestOverrides(t *testing.T) { "APP_LOG_LEVEL": "debug", "APP_SHUTDOWN_TIMEOUT": "3s", "APP_MAX_BODY_BYTES": "1024", + "SESSION_TTL_SECONDS": "300", }))) if err != nil { t.Fatalf("Load error: %v", err) @@ -92,7 +106,7 @@ func TestOverrides(t *testing.T) { if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.LogLevel != slog.LevelDebug { t.Fatalf("cfg = %+v", cfg) } - if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 { + if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 || cfg.SessionTTL != 5*time.Minute { t.Fatalf("cfg = %+v", cfg) } } @@ -108,6 +122,10 @@ func TestInvalidValuesAreReported(t *testing.T) { "body zero": {"APP_MAX_BODY_BYTES": "0"}, "body text": {"APP_MAX_BODY_BYTES": "big"}, "empty host": {"APP_HOST": ""}, + "ttl zero": {"SESSION_TTL_SECONDS": "0"}, + "ttl negative": {"SESSION_TTL_SECONDS": "-5"}, + "ttl text": {"SESSION_TTL_SECONDS": "hour"}, + "ttl fraction": {"SESSION_TTL_SECONDS": "1.5"}, } for name, values := range cases { if _, err := config.Load(env(required(t, values))); err == nil { diff --git a/internal/platform/valkey/valkey.go b/internal/platform/valkey/valkey.go new file mode 100644 index 00000000..15a4c0af --- /dev/null +++ b/internal/platform/valkey/valkey.go @@ -0,0 +1,44 @@ +// Package valkey connects to the Valkey session store. +// +// Timeouts are constants: every authenticated request reads a session, so +// they bound how long a request can wait when the store is slow. Retries are +// disabled for the same reason: a request that cannot reach the store fails +// fast with 503 instead of waiting through a backoff. +package valkey + +import ( + "context" + "errors" + "fmt" + "time" + + "github.com/redis/go-redis/v9" +) + +const ( + dialTimeout = 2 * time.Second + ioTimeout = time.Second + poolSize = 10 + noRetries = -1 +) + +// NewClient connects to url (redis:// scheme) and pings the server. Errors +// never include the URL, which carries the password. +func NewClient(ctx context.Context, url string) (*redis.Client, error) { + opts, err := redis.ParseURL(url) + if err != nil { + return nil, errors.New("session store url is invalid") + } + opts.DialTimeout = dialTimeout + opts.ReadTimeout = ioTimeout + opts.WriteTimeout = ioTimeout + opts.PoolSize = poolSize + opts.MaxRetries = noRetries + + client := redis.NewClient(opts) + if err := client.Ping(ctx).Err(); err != nil { + _ = client.Close() + return nil, fmt.Errorf("ping session store at %s: %w", opts.Addr, err) + } + return client, nil +} diff --git a/internal/platform/valkey/valkey_test.go b/internal/platform/valkey/valkey_test.go new file mode 100644 index 00000000..af13203f --- /dev/null +++ b/internal/platform/valkey/valkey_test.go @@ -0,0 +1,51 @@ +package valkey_test + +import ( + "context" + "strings" + "testing" + "time" + + "messenger/internal/platform/valkey" + "messenger/internal/testenv" +) + +func TestNewClientPingsAndBoundsWaiting(t *testing.T) { + ctx := context.Background() + kv, err := testenv.StartValkey(ctx) + if err != nil { + t.Fatal(err) + } + defer kv.Close() + + client, err := valkey.NewClient(ctx, kv.URL) + if err != nil { + t.Fatalf("NewClient: %v", err) + } + defer client.Close() + opts := client.Options() + if opts.DialTimeout != 2*time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second { + t.Fatalf("timeouts = %v/%v/%v, want 2s/1s/1s", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout) + } + // go-redis stores the requested -1 ("no retries") as 0 after init; its + // default would be 3. + if opts.PoolSize != 10 || opts.MaxRetries != 0 { + t.Fatalf("PoolSize = %d, MaxRetries = %d; want 10 and 0 (fail fast)", opts.PoolSize, opts.MaxRetries) + } +} + +func TestNewClientErrorsNeverEchoTheURL(t *testing.T) { + ctx := context.Background() + for name, url := range map[string]string{ + "malformed": "redis://:s3cret@host:port/x", + "unreachable": "redis://:s3cret@127.0.0.1:1/0", + } { + _, err := valkey.NewClient(ctx, url) + if err == nil { + t.Fatalf("%s: want error", name) + } + if strings.Contains(err.Error(), "s3cret") { + t.Fatalf("%s: error leaks the password: %v", name, err) + } + } +} diff --git a/internal/user/service.go b/internal/user/service.go index 7a78448a..312474e9 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -68,16 +68,6 @@ func (s *Service) GetByID(ctx context.Context, id uuid.UUID) (User, error) { return s.repo.GetByID(ctx, id) } -// Exists serves the identification middleware; it is the single-id form of -// ExistingIDs so existence is answered by one code path. -func (s *Service) Exists(ctx context.Context, id uuid.UUID) (bool, error) { - found, err := s.ExistingIDs(ctx, []uuid.UUID{id}) - if err != nil { - return false, err - } - return len(found) == 1, nil -} - // ExistingIDs serves the chats module when it validates member lists. func (s *Service) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { if len(ids) == 0 { diff --git a/internal/user/service_test.go b/internal/user/service_test.go index de6d11eb..99aecf4a 100644 --- a/internal/user/service_test.go +++ b/internal/user/service_test.go @@ -44,9 +44,6 @@ func TestRegisterStoresUserWithPasswordHash(t *testing.T) { if got != u { t.Fatalf("GetByID = %+v, want %+v", got, u) } - if ok, err := svc.Exists(ctx, u.ID); err != nil || !ok { - t.Fatalf("Exists = %v, %v; want true", ok, err) - } } func TestRegisterRejectsInvalidInputAndStoresNothing(t *testing.T) { @@ -129,9 +126,6 @@ func TestGetByIDUnknownIsNotFound(t *testing.T) { if !errors.Is(err, user.ErrNotFound) || apperr.KindOf(err) != apperr.KindNotFound { t.Fatalf("err = %v, want ErrNotFound with KindNotFound", err) } - if ok, err := svc.Exists(context.Background(), ident.New()); err != nil || ok { - t.Fatalf("Exists = %v, %v; want false, nil", ok, err) - } } func TestExistingIDsReturnsOnlyRegistered(t *testing.T) { diff --git a/tools/concurrency/client.go b/tools/concurrency/client.go index 46c2c74b..5f915761 100644 --- a/tools/concurrency/client.go +++ b/tools/concurrency/client.go @@ -20,7 +20,7 @@ func newClient(baseURL string) *client { return &client{base: baseURL, http: &http.Client{Timeout: 10 * time.Second}} } -func (c *client) post(path, actor string, body, out any) error { +func (c *client) post(path, token string, body, out any) error { raw, err := json.Marshal(body) if err != nil { return err @@ -30,12 +30,28 @@ func (c *client) post(path, actor string, body, out any) error { return err } req.Header.Set("Content-Type", "application/json") - return c.do(req, actor, http.StatusCreated, out) + return c.do(req, token, http.StatusCreated, out) +} + +// login opens a session with Basic credentials and returns its bearer token. +func (c *client) login(username, password string) (string, error) { + req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/auth/sessions", nil) + if err != nil { + return "", err + } + req.SetBasicAuth(username, password) + var session struct { + Token string `json:"token"` + } + if err := c.do(req, "", http.StatusCreated, &session); err != nil { + return "", err + } + return session.Token, nil } // get requests the largest page the contract allows, continuing from cursor // when it is set. -func (c *client) get(path, actor, cursor string, out any) error { +func (c *client) get(path, token, cursor string, out any) error { query := url.Values{"limit": {"100"}} if cursor != "" { query.Set("cursor", cursor) @@ -44,12 +60,12 @@ func (c *client) get(path, actor, cursor string, out any) error { if err != nil { return err } - return c.do(req, actor, http.StatusOK, out) + return c.do(req, token, http.StatusOK, out) } -func (c *client) do(req *http.Request, actor string, want int, out any) error { - if actor != "" { - req.Header.Set("X-User-Id", actor) +func (c *client) do(req *http.Request, token string, want int, out any) error { + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) } resp, err := c.http.Do(req) if err != nil { diff --git a/tools/concurrency/main.go b/tools/concurrency/main.go index 11ef2e39..0ed1e54e 100644 --- a/tools/concurrency/main.go +++ b/tools/concurrency/main.go @@ -89,22 +89,22 @@ func run(baseURL string, clients, perClient int) (Report, error) { api := newClient(baseURL) runID := uuid.NewString()[:8] - members, err := registerClients(api, runID, clients) + members, tokens, err := registerClients(api, runID, clients) if err != nil { return report, err } - room, err := createChat(api, runID, members) + room, err := createChat(api, runID, members, tokens[0]) if err != nil { return report, err } started := time.Now() - created, failed := sendConcurrently(baseURL, room.ID, members, perClient) + created, failed := sendConcurrently(baseURL, room.ID, tokens, perClient) report.ElapsedSeconds = time.Since(started).Seconds() report.Created = len(created) report.Failed = failed - listed, pages, err := readHistory(api, room.ID, members[0]) + listed, pages, err := readHistory(api, room.ID, tokens[0]) if err != nil { return report, err } @@ -124,27 +124,34 @@ func run(baseURL string, clients, perClient int) (Report, error) { // clientPassword is shared by the synthetic clients of one run. const clientPassword = "concurrency-check-password" -func registerClients(api *client, runID string, n int) ([]string, error) { - ids := make([]string, 0, n) +// registerClients registers and logs in n clients, returning their ids and +// bearer tokens in the same order. +func registerClients(api *client, runID string, n int) (ids, tokens []string, err error) { for i := range n { var created user.Response + username := fmt.Sprintf("load_%s_%d", runID, i) body := map[string]string{ - "username": fmt.Sprintf("load_%s_%d", runID, i), + "username": username, "display_name": fmt.Sprintf("Load client %d", i), "password": clientPassword, } if err := api.post("/api/v1/users", "", body, &created); err != nil { - return nil, fmt.Errorf("register client %d: %w", i, err) + return nil, nil, fmt.Errorf("register client %d: %w", i, err) + } + token, err := api.login(username, clientPassword) + if err != nil { + return nil, nil, fmt.Errorf("log in client %d: %w", i, err) } ids = append(ids, created.ID) + tokens = append(tokens, token) } - return ids, nil + return ids, tokens, nil } -func createChat(api *client, runID string, members []string) (chat.ChatResponse, error) { +func createChat(api *client, runID string, members []string, creatorToken string) (chat.ChatResponse, error) { var created chat.ChatResponse body := map[string]any{"title": "Concurrency check " + runID, "member_ids": members} - if err := api.post("/api/v1/chats", members[0], body, &created); err != nil { + if err := api.post("/api/v1/chats", creatorToken, body, &created); err != nil { return chat.ChatResponse{}, fmt.Errorf("create chat: %w", err) } return created, nil @@ -152,23 +159,23 @@ func createChat(api *client, runID string, members []string) (chat.ChatResponse, // sendConcurrently gives every member its own HTTP client and releases them // together, so the server sees genuinely parallel writes to one chat. -func sendConcurrently(baseURL, chatID string, members []string, perClient int) (map[string]struct{}, int) { +func sendConcurrently(baseURL, chatID string, tokens []string, perClient int) (map[string]struct{}, int) { var mu sync.Mutex - created := make(map[string]struct{}, len(members)*perClient) + created := make(map[string]struct{}, len(tokens)*perClient) failed := 0 var wg sync.WaitGroup start := make(chan struct{}) - for i, actor := range members { + for i, token := range tokens { wg.Add(1) - go func(n int, actor string) { + go func(n int, token string) { defer wg.Done() own := newClient(baseURL) <-start for m := range perClient { var msg chat.MessageResponse body := map[string]string{"text": fmt.Sprintf("client %d message %d", n, m)} - err := own.post("/api/v1/chats/"+chatID+"/messages", actor, body, &msg) + err := own.post("/api/v1/chats/"+chatID+"/messages", token, body, &msg) mu.Lock() if err != nil { failed++ @@ -177,20 +184,20 @@ func sendConcurrently(baseURL, chatID string, members []string, perClient int) ( } mu.Unlock() } - }(i, actor) + }(i, token) } close(start) wg.Wait() return created, failed } -func readHistory(api *client, chatID, actor string) ([]string, int, error) { +func readHistory(api *client, chatID, token string) ([]string, int, error) { var ids []string pages := 0 cursor := "" for { var page pagination.PageResponse[chat.MessageResponse] - if err := api.get("/api/v1/chats/"+chatID+"/messages", actor, cursor, &page); err != nil { + if err := api.get("/api/v1/chats/"+chatID+"/messages", token, cursor, &page); err != nil { return nil, pages, fmt.Errorf("read history: %w", err) } pages++ diff --git a/tools/concurrency/main_test.go b/tools/concurrency/main_test.go index e26a9d79..eb607c3d 100644 --- a/tools/concurrency/main_test.go +++ b/tools/concurrency/main_test.go @@ -8,28 +8,39 @@ import ( "net/http/httptest" "os" "testing" + "time" "messenger/internal/app" "messenger/internal/testenv" ) -var db *testenv.DB +var ( + db *testenv.DB + kv *testenv.KV +) func TestMain(m *testing.M) { + ctx := context.Background() var err error - db, err = testenv.StartPostgres(context.Background()) - if err != nil { + if db, err = testenv.StartPostgres(ctx); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if kv, err = testenv.StartValkey(ctx); err != nil { + db.Close() fmt.Fprintln(os.Stderr, err) os.Exit(1) } code := m.Run() + kv.Close() db.Close() os.Exit(code) } func TestRunAgainstLiveServer(t *testing.T) { log := slog.New(slog.NewTextHandler(io.Discard, nil)) - srv := httptest.NewServer(app.New(app.Deps{Log: log, DB: db.Pool, MaxBodyBytes: 64 * 1024}).Engine) + a := app.New(app.Deps{Log: log, DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 64 * 1024}) + srv := httptest.NewServer(a.Engine) defer srv.Close() report, err := run(srv.URL, 10, 10) -- 2.54.0 From f6acdf91e087a03424545758c01d7d4ac7eb2924 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:13:04 +0300 Subject: [PATCH 10/21] =?UTF-8?q?=D0=9E=D0=B3=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=87=D0=B8=D1=82=D1=8C=20=D0=BE=D0=B6=D0=B8=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B5=D0=B9=20=D0=B8=20=D0=BE=D1=82=D0=B2=D0=B5?= =?UTF-8?q?=D1=87=D0=B0=D1=82=D1=8C=20503=20=D0=BF=D1=80=D0=B8=20=D0=B8?= =?UTF-8?q?=D1=85=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/app.go | 18 +++- internal/app/failure_test.go | 100 ++++++++++++++++++++ internal/chat/store/postgres.go | 22 +++-- internal/health/handler.go | 34 ++++++- internal/platform/httpx/httpx_test.go | 20 ++++ internal/platform/httpx/middleware.go | 13 +++ internal/platform/postgres/postgres.go | 39 ++++++++ internal/platform/postgres/postgres_test.go | 42 ++++++++ internal/testenv/testenv.go | 36 +++++++ internal/user/store/postgres.go | 10 +- 10 files changed, 313 insertions(+), 21 deletions(-) create mode 100644 internal/app/failure_test.go diff --git a/internal/app/app.go b/internal/app/app.go index 6722ee20..6653c8c5 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -4,6 +4,7 @@ package app import ( + "context" "log/slog" "time" @@ -34,19 +35,32 @@ type Deps struct { Sessions *redis.Client SessionTTL time.Duration MaxBodyBytes int64 + // RequestTimeout bounds every API request; zero means the default. + RequestTimeout time.Duration } +// defaultRequestTimeout stays below the server's 10s write timeout, so a +// request that gives up on a dependency can still write its 503. +const defaultRequestTimeout = 5 * time.Second + func New(d Deps) *App { users := user.NewService(userstore.NewUsers(d.DB)) chats := chat.NewService(chatstore.NewChats(d.DB), chatstore.NewMessages(d.DB), users) engine := httpx.NewEngine(d.Log, d.MaxBodyBytes) - probes := health.New() + probes := health.New( + health.Check{Name: "postgres", Probe: d.DB.Ping}, + health.Check{Name: "valkey", Probe: func(ctx context.Context) error { return d.Sessions.Ping(ctx).Err() }}, + ) probes.Register(engine) sessions := auth.NewService(users, authstore.NewSessions(d.Sessions), d.SessionTTL) - public := engine.Group("/api/v1") + timeout := d.RequestTimeout + if timeout == 0 { + timeout = defaultRequestTimeout + } + public := engine.Group("/api/v1", httpx.Timeout(timeout)) protected := public.Group("", auth.RequireSession(sessions)) auth.NewHandler(sessions).Register(public, protected) user.NewHandler(users).Register(public, protected) diff --git a/internal/app/failure_test.go b/internal/app/failure_test.go new file mode 100644 index 00000000..cadc4ee6 --- /dev/null +++ b/internal/app/failure_test.go @@ -0,0 +1,100 @@ +package app_test + +import ( + "context" + "io" + "log/slog" + "net/http" + "testing" + "time" + + "messenger/internal/app" + "messenger/internal/testenv" +) + +// newIsolatedTester assembles the application over containers of its own, +// so a test can stop or pause them without disturbing the shared ones. +func newIsolatedTester(t *testing.T, requestTimeout time.Duration) (*tester, *testenv.DB, *testenv.KV) { + t.Helper() + ctx := context.Background() + pg, err := testenv.StartPostgres(ctx) + if err != nil { + t.Fatal(err) + } + t.Cleanup(pg.Close) + vk, err := testenv.StartValkey(ctx) + if err != nil { + t.Fatal(err) + } + t.Cleanup(vk.Close) + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + a := app.New(app.Deps{ + Log: log, + DB: pg.Pool, + Sessions: vk.Client, + SessionTTL: time.Hour, + MaxBodyBytes: 64 * 1024, + RequestTimeout: requestTimeout, + }) + return &tester{t: t, a: a}, pg, vk +} + +// Without the session store no caller can be authenticated: the API answers +// 503, never 401 (the token is not at fault) and never as an anonymous user. +// Registration does not need the store and keeps working. +func TestValkeyOutageAnswers503(t *testing.T) { + ts, _, vk := newIsolatedTester(t, 5*time.Second) + alice := ts.user("alice", "Алиса") + room := ts.chat(alice.token, "t", alice.ID) + + if err := vk.Stop(context.Background()); err != nil { + t.Fatal(err) + } + + ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+room.ID, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "") + + body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword} + ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "") +} + +func TestPostgresOutageAnswers503(t *testing.T) { + ts, pg, _ := newIsolatedTester(t, 5*time.Second) + alice := ts.user("alice", "Алиса") + + if err := pg.Stop(context.Background()); err != nil { + t.Fatal(err) + } + + ts.expect(ts.do(http.MethodGet, "/api/v1/chats", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable") + body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword} + ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusServiceUnavailable, "unavailable") + ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable") +} + +// A database that accepts connections but never answers must not hold the +// request: the request deadline turns it into a 503. +func TestHungPostgresAnswers503WithinTheRequestTimeout(t *testing.T) { + ts, pg, _ := newIsolatedTester(t, 300*time.Millisecond) + alice := ts.user("alice", "Алиса") + + ctx := context.Background() + if err := pg.Pause(ctx); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = pg.Unpause(ctx) }) + + for _, path := range []string{"/api/v1/chats", "/health/ready"} { + start := time.Now() + ts.expect(ts.do(http.MethodGet, path, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") + if elapsed := time.Since(start); elapsed > 2*time.Second { + t.Fatalf("%s took %v with a hung database, want it bounded", path, elapsed) + } + } +} diff --git a/internal/chat/store/postgres.go b/internal/chat/store/postgres.go index 76d7b9c8..65affa06 100644 --- a/internal/chat/store/postgres.go +++ b/internal/chat/store/postgres.go @@ -4,7 +4,8 @@ // greater than the nil UUID, so one query serves the first page and the // following ones, and the condition stays a range the index can scan. Ids // are UUIDv7 and created_at derives from them, so ordering by id is the -// contract order (created_at, id). +// contract order (created_at, id). Errors pass through postgres.Classify, so +// an unreachable database surfaces as unavailability. package store import ( @@ -17,6 +18,7 @@ import ( "github.com/jackc/pgx/v5/pgxpool" "messenger/internal/chat" + "messenger/internal/platform/postgres" ) type Chats struct { @@ -34,7 +36,7 @@ const chatWithMembers = `c.id, c.title, c.created_at, ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)` func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error { - return pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error { + return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error { if _, err := tx.Exec(ctx, `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`, c.ID, c.Title, c.CreatedAt); err != nil { @@ -46,7 +48,7 @@ func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) return fmt.Errorf("insert chat members: %w", err) } return nil - }) + })) } func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) { @@ -61,7 +63,7 @@ func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (cha return chat.ChatWithMembers{}, chat.ErrNotFound } if err != nil { - return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", err) + return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", postgres.Classify(err)) } return c, nil } @@ -72,7 +74,7 @@ func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, e `SELECT EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, chatID, userID).Scan(&ok) if err != nil { - return false, fmt.Errorf("check membership: %w", err) + return false, fmt.Errorf("check membership: %w", postgres.Classify(err)) } return ok, nil } @@ -88,13 +90,13 @@ func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid. LIMIT $3`, userID, after, limit) if err != nil { - return nil, fmt.Errorf("select chats: %w", err) + return nil, fmt.Errorf("select chats: %w", postgres.Classify(err)) } chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.ChatWithMembers, error) { return scanChat(row) }) if err != nil { - return nil, fmt.Errorf("read chats: %w", err) + return nil, fmt.Errorf("read chats: %w", postgres.Classify(err)) } return chats, nil } @@ -123,7 +125,7 @@ func (r *Messages) Append(ctx context.Context, m chat.Message) error { `INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)`, m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt) if err != nil { - return fmt.Errorf("insert message: %w", err) + return fmt.Errorf("insert message: %w", postgres.Classify(err)) } return nil } @@ -140,7 +142,7 @@ func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid LIMIT $3`, chatID, after, limit) if err != nil { - return nil, fmt.Errorf("select messages: %w", err) + return nil, fmt.Errorf("select messages: %w", postgres.Classify(err)) } messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) { var m chat.Message @@ -149,7 +151,7 @@ func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid return m, err }) if err != nil { - return nil, fmt.Errorf("read messages: %w", err) + return nil, fmt.Errorf("read messages: %w", postgres.Classify(err)) } return messages, nil } diff --git a/internal/health/handler.go b/internal/health/handler.go index 41a7afa9..9b14e880 100644 --- a/internal/health/handler.go +++ b/internal/health/handler.go @@ -1,11 +1,15 @@ // Package health serves the probes. Liveness only proves the process -// answers; readiness is switched off during shutdown so an orchestrator stops -// routing new traffic before the listener closes. +// answers. Readiness means the process can serve the synchronous API: it is +// switched off during shutdown, so an orchestrator stops routing new traffic +// before the listener closes, and it fails while a critical dependency is +// unreachable. package health import ( + "context" "net/http" "sync/atomic" + "time" "github.com/gin-gonic/gin" @@ -13,16 +17,28 @@ import ( "messenger/internal/platform/httpx" ) +// checkTimeout is shared by all checks of one probe, so the probe answers +// within it even when every dependency hangs; the Docker healthcheck allows +// two seconds. +const checkTimeout = time.Second + +// Check is a dependency the API cannot serve requests without. +type Check struct { + Name string + Probe func(ctx context.Context) error +} + type Handler struct { - ready atomic.Bool + ready atomic.Bool + checks []Check } type status struct { Status string `json:"status"` } -func New() *Handler { - h := &Handler{} +func New(checks ...Check) *Handler { + h := &Handler{checks: checks} h.ready.Store(true) return h } @@ -49,5 +65,13 @@ func (h *Handler) readyz(c *gin.Context) { httpx.Error(c, apperr.Unavailable("service is shutting down")) return } + ctx, cancel := context.WithTimeout(c.Request.Context(), checkTimeout) + defer cancel() + for _, check := range h.checks { + if err := check.Probe(ctx); err != nil { + httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, check.Name+" is unavailable", err)) + return + } + } httpx.JSON(c, http.StatusOK, status{Status: "ok"}) } diff --git a/internal/platform/httpx/httpx_test.go b/internal/platform/httpx/httpx_test.go index f57fdedf..16ac1318 100644 --- a/internal/platform/httpx/httpx_test.go +++ b/internal/platform/httpx/httpx_test.go @@ -307,6 +307,26 @@ func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) { assertError(t, rec, eb, http.StatusInternalServerError, "internal_error") } +// Everything below Timeout sees one deadline, so waiting on a dependency +// ends with the request instead of holding the client. +func TestTimeoutBoundsTheRequestContext(t *testing.T) { + e := newEngine(t, nil) + e.GET("/slow", httpx.Timeout(50*time.Millisecond), func(c *gin.Context) { + select { + case <-c.Request.Context().Done(): + httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, "gave up", c.Request.Context().Err())) + case <-time.After(5 * time.Second): + httpx.JSON(c, http.StatusOK, gin.H{}) + } + }) + start := time.Now() + rec, eb := do(t, e, http.MethodGet, "/slow", "", nil) + assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable") + if elapsed := time.Since(start); elapsed > time.Second { + t.Fatalf("request took %v, want it cut at the 50ms deadline", elapsed) + } +} + func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any { t.Helper() var entry map[string]any diff --git a/internal/platform/httpx/middleware.go b/internal/platform/httpx/middleware.go index 724b1830..d153a980 100644 --- a/internal/platform/httpx/middleware.go +++ b/internal/platform/httpx/middleware.go @@ -1,6 +1,7 @@ package httpx import ( + "context" "crypto/rand" "encoding/hex" "fmt" @@ -93,6 +94,18 @@ func recovery() gin.HandlerFunc { } } +// Timeout bounds the whole request with one deadline: waiting for a pooled +// connection, SQL statements and session lookups all observe it, so a hung +// dependency ends in a 503 within d instead of holding the client. +func Timeout(d time.Duration) gin.HandlerFunc { + return func(c *gin.Context) { + ctx, cancel := context.WithTimeout(c.Request.Context(), d) + defer cancel() + c.Request = c.Request.WithContext(ctx) + c.Next() + } +} + // bodyLimit caps request bodies; exceeding it surfaces from the JSON // decoder as *http.MaxBytesError. func bodyLimit(maxBytes int64) gin.HandlerFunc { diff --git a/internal/platform/postgres/postgres.go b/internal/platform/postgres/postgres.go index 8ab3507b..e075fe29 100644 --- a/internal/platform/postgres/postgres.go +++ b/internal/platform/postgres/postgres.go @@ -9,6 +9,8 @@ import ( "context" "errors" "fmt" + "io" + "net" "strconv" "time" @@ -18,6 +20,7 @@ import ( "github.com/pressly/goose/v3" "github.com/pressly/goose/v3/lock" + "messenger/internal/platform/apperr" "messenger/migrations" ) @@ -71,6 +74,42 @@ func Migrate(ctx context.Context, pool *pgxpool.Pool) error { return nil } +// Classify marks a failure to reach the database as apperr.KindUnavailable, +// which the API answers with 503 instead of 500; any other error is returned +// unchanged. Unreachable means: the connection could not be made or broke, +// the request deadline passed, or the server refused work (PostgreSQL error +// classes 08 connection exception, 53 insufficient resources, 57 operator +// intervention such as shutdown or statement timeout). +func Classify(err error) error { + if err == nil || !unreachable(err) { + return err + } + return apperr.Wrap(apperr.KindUnavailable, "database unavailable", err) +} + +func unreachable(err error) bool { + if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok { + switch class := pgErr.Code[:min(2, len(pgErr.Code))]; class { + case "08", "53", "57": + return true + } + return false + } + if _, ok := errors.AsType[*pgconn.ConnectError](err); ok { + return true + } + if _, ok := errors.AsType[net.Error](err); ok { + return true + } + return errors.Is(err, context.DeadlineExceeded) || + errors.Is(err, context.Canceled) || + errors.Is(err, io.EOF) || + errors.Is(err, io.ErrUnexpectedEOF) || + errors.Is(err, net.ErrClosed) || + pgconn.Timeout(err) || + pgconn.SafeToRetry(err) +} + // IsUniqueViolation reports whether err is a unique violation of the named // constraint. func IsUniqueViolation(err error, constraint string) bool { diff --git a/internal/platform/postgres/postgres_test.go b/internal/platform/postgres/postgres_test.go index 68f67ea5..24ee4b48 100644 --- a/internal/platform/postgres/postgres_test.go +++ b/internal/platform/postgres/postgres_test.go @@ -4,9 +4,14 @@ import ( "context" "errors" "fmt" + "io" + "net" "os" "testing" + "github.com/jackc/pgx/v5/pgconn" + + "messenger/internal/platform/apperr" "messenger/internal/platform/postgres" "messenger/internal/testenv" ) @@ -133,3 +138,40 @@ func TestIsUniqueViolation(t *testing.T) { t.Fatal("non-database error recognised as a violation") } } + +func TestClassifyMarksUnreachableDatabaseAsUnavailable(t *testing.T) { + unavailable := map[string]error{ + "admin shutdown": &pgconn.PgError{Code: "57P01"}, + "statement timeout": &pgconn.PgError{Code: "57014"}, + "connection failure": &pgconn.PgError{Code: "08006"}, + "too many connections": &pgconn.PgError{Code: "53300"}, + "deadline": fmt.Errorf("query: %w", context.DeadlineExceeded), + "network": &net.OpError{Op: "read", Net: "tcp", Err: errors.New("connection reset")}, + "unexpected eof": fmt.Errorf("read: %w", io.ErrUnexpectedEOF), + } + for name, err := range unavailable { + got := postgres.Classify(err) + if apperr.KindOf(got) != apperr.KindUnavailable || !errors.Is(got, err) { + t.Errorf("%s: Classify = %v (kind %v), want KindUnavailable keeping the cause", name, got, apperr.KindOf(got)) + } + } + for name, err := range map[string]error{ + "unique violation": &pgconn.PgError{Code: "23505"}, + "plain": errors.New("boom"), + "nil": nil, + } { + if got := postgres.Classify(err); got != err { + t.Errorf("%s: Classify = %v, want the error unchanged", name, got) + } + } +} + +func TestClassifyRealConnectFailure(t *testing.T) { + _, err := postgres.NewPool(context.Background(), "postgres://u:p@127.0.0.1:1/db?connect_timeout=1") + if err == nil { + t.Fatal("NewPool to a closed port must fail") + } + if apperr.KindOf(postgres.Classify(err)) != apperr.KindUnavailable { + t.Fatalf("connect failure classified as %v: %v", apperr.KindOf(postgres.Classify(err)), err) + } +} diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 90974eda..04c9f42d 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -10,6 +10,7 @@ import ( "runtime" "sync" "testing" + "time" "github.com/jackc/pgx/v5/pgxpool" "github.com/redis/go-redis/v9" @@ -94,6 +95,35 @@ func (d *DB) Reset(t testing.TB) { } } +// Stop stops the container, as when the database goes down. +func (d *DB) Stop(ctx context.Context) error { + timeout := 5 * time.Second + return d.container.Stop(ctx, &timeout) +} + +// Pause freezes the server: connections stay open but nothing answers, as +// with a hung database. +func (d *DB) Pause(ctx context.Context) error { + return withDocker(ctx, func(cli *testcontainers.DockerClient) error { + return cli.ContainerPause(ctx, d.container.GetContainerID()) + }) +} + +func (d *DB) Unpause(ctx context.Context) error { + return withDocker(ctx, func(cli *testcontainers.DockerClient) error { + return cli.ContainerUnpause(ctx, d.container.GetContainerID()) + }) +} + +func withDocker(ctx context.Context, fn func(cli *testcontainers.DockerClient) error) error { + cli, err := testcontainers.NewDockerClientWithOpts(ctx) + if err != nil { + return err + } + defer cli.Close() + return fn(cli) +} + func (d *DB) Close() { if d.Pool != nil { d.Pool.Close() @@ -136,6 +166,12 @@ func (k *KV) Reset(t testing.TB) { } } +// Stop stops the container, as when the session store goes down. +func (k *KV) Stop(ctx context.Context) error { + timeout := 5 * time.Second + return k.container.Stop(ctx, &timeout) +} + func (k *KV) Close() { if k.Client != nil { _ = k.Client.Close() diff --git a/internal/user/store/postgres.go b/internal/user/store/postgres.go index 4bfda146..df5a5135 100644 --- a/internal/user/store/postgres.go +++ b/internal/user/store/postgres.go @@ -1,4 +1,6 @@ // Package store holds the repository implementations of the users module. +// Errors pass through postgres.Classify, so an unreachable database surfaces +// as unavailability rather than as an internal error. package store import ( @@ -36,7 +38,7 @@ func (r *Users) Create(ctx context.Context, u user.User) error { return user.ErrUsernameTaken } if err != nil { - return fmt.Errorf("insert user: %w", err) + return fmt.Errorf("insert user: %w", postgres.Classify(err)) } return nil } @@ -52,11 +54,11 @@ func (r *Users) GetByUsername(ctx context.Context, username string) (user.User, func (r *Users) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { rows, err := r.pool.Query(ctx, `SELECT id FROM users WHERE id = ANY($1)`, ids) if err != nil { - return nil, fmt.Errorf("select user ids: %w", err) + return nil, fmt.Errorf("select user ids: %w", postgres.Classify(err)) } found, err := pgx.CollectRows(rows, pgx.RowTo[uuid.UUID]) if err != nil { - return nil, fmt.Errorf("read user ids: %w", err) + return nil, fmt.Errorf("read user ids: %w", postgres.Classify(err)) } return found, nil } @@ -68,7 +70,7 @@ func (r *Users) one(ctx context.Context, query string, arg any) (user.User, erro return user.User{}, user.ErrNotFound } if err != nil { - return user.User{}, fmt.Errorf("select user: %w", err) + return user.User{}, fmt.Errorf("select user: %w", postgres.Classify(err)) } u.CreatedAt = u.CreatedAt.UTC() return u, nil -- 2.54.0 From 7f599cb25eecefb7c0cfb34b45f3af1bb2eb39e8 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:21:03 +0300 Subject: [PATCH 11/21] =?UTF-8?q?=D0=A1=D0=BE=D0=B1=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20=D1=81=D1=82=D0=B5=D0=BD=D0=B4=20=D0=B8=D0=B7=20API,?= =?UTF-8?q?=20PostgreSQL=20=D0=B8=20Valkey=20=D1=81=20=D1=85=D1=83=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF=D1=83?= =?UTF-8?q?=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/app.yml | 15 ++++---- Dockerfile | 2 +- Makefile | 32 +++++++--------- compose.yaml | 76 +++++++++++++++++++++++++++++++++++-- infra/valkey/valkey.conf | 17 +++++++++ scripts/init-secrets.sh | 42 ++++++++++++++++++++ scripts/restart.cmd | 5 +++ scripts/restart.sh | 9 +++++ student-tests/lifecycle.sh | 62 ------------------------------ student-tests/wait-ready.sh | 17 --------- 10 files changed, 167 insertions(+), 110 deletions(-) create mode 100644 infra/valkey/valkey.conf create mode 100755 scripts/init-secrets.sh create mode 100644 scripts/restart.cmd create mode 100755 scripts/restart.sh delete mode 100644 student-tests/lifecycle.sh delete mode 100644 student-tests/wait-ready.sh diff --git a/.github/workflows/app.yml b/.github/workflows/app.yml index dc5c3033..a27b1206 100644 --- a/.github/workflows/app.yml +++ b/.github/workflows/app.yml @@ -8,7 +8,6 @@ defaults: shell: bash env: BASE_URL: http://localhost:8080 - CONTAINER: mtusi-messenger-lab2 jobs: unit: runs-on: ubuntu-latest @@ -30,22 +29,22 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.12' - - run: docker build -t mtusi-messenger:lab2 . - - run: docker run -d --name "$CONTAINER" -p 127.0.0.1:8080:8080 --env-file .env.example mtusi-messenger:lab2 - - run: sh student-tests/wait-ready.sh + - run: make secrets + - run: docker compose up -d --build --wait - run: mkdir -p evidence-ci - name: Public smoke run: make test PYTHON=python BASE_URL="$BASE_URL" | tee evidence-ci/smoke.txt + - name: Persistence across a restart of the API and both stores + run: make test-persistence PYTHON=python BASE_URL="$BASE_URL" ACTION_SCRIPT=scripts/restart.sh | tee evidence-ci/persistence.txt - name: Concurrency check run: go run ./tools/concurrency --base-url "$BASE_URL" --output evidence-ci/concurrency.json - - name: SIGTERM and restart - run: sh student-tests/lifecycle.sh | tee evidence-ci/lifecycle.txt - name: Container logs if: always() - run: docker logs "$CONTAINER" > evidence-ci/container.log 2>&1 || true + run: docker compose logs > evidence-ci/compose.log 2>&1 || true - name: Cleanup + # The CI stand is disposable, so its volumes go too. if: always() - run: docker rm -f "$CONTAINER" || true + run: docker compose down -v || true - uses: actions/upload-artifact@v4 if: always() with: diff --git a/Dockerfile b/Dockerfile index 42d4357a..b8bc70b1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,6 @@ ENV APP_HOST=0.0.0.0 \ APP_PORT=8080 \ APP_ENV=lab EXPOSE 8080 -HEALTHCHECK --interval=10s --timeout=2s --start-period=3s --retries=3 \ +HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --retries=3 \ CMD wget -qO- "http://127.0.0.1:${APP_PORT:-8080}/health/ready" >/dev/null || exit 1 ENTRYPOINT ["/usr/local/bin/server"] diff --git a/Makefile b/Makefile index 3cb2500e..d1d37188 100644 --- a/Makefile +++ b/Makefile @@ -28,12 +28,9 @@ test-persistence: # --------------------------------------------------------------------------- # Application targets (Go implementation). The targets above are the skeleton. # --------------------------------------------------------------------------- -IMAGE ?= mtusi-messenger:lab2 -CONTAINER ?= mtusi-messenger-lab2 -PUBLISH ?= 127.0.0.1:8080 EVIDENCE_DIR ?= evidence -.PHONY: build vet unit up down docker-build docker-run docker-stop docker-logs wait-ready concurrency lifecycle +.PHONY: build vet unit secrets up down destroy logs concurrency explain build: go build -o bin/server ./cmd/server @@ -41,32 +38,29 @@ build: vet: go vet ./... +# The tests start disposable PostgreSQL and Valkey containers: Docker must run. unit: go test -race -count=1 ./... +secrets: + sh scripts/init-secrets.sh + up: docker compose up -d --build --wait +# Stops and removes the containers; volumes and therefore data are kept. down: docker compose down -docker-build: - docker build -t "$(IMAGE)" . +# Deletes the volumes too: every user, chat, message and session of the stand. +destroy: + docker compose down -v -docker-run: - docker run -d --name "$(CONTAINER)" -p "$(PUBLISH):8080" --env-file .env "$(IMAGE)" - -docker-stop: - docker stop -t 10 "$(CONTAINER)" && docker rm "$(CONTAINER)" - -docker-logs: - docker logs "$(CONTAINER)" - -wait-ready: - BASE_URL="$(BASE_URL)" sh student-tests/wait-ready.sh +logs: + docker compose logs concurrency: go run ./tools/concurrency --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/concurrency.json" -lifecycle: - BASE_URL="$(BASE_URL)" CONTAINER="$(CONTAINER)" sh student-tests/lifecycle.sh +explain: + docker compose exec -T postgres psql -U messenger -d messenger -X -q -v ON_ERROR_STOP=1 -f - < scripts/explain-history.sql diff --git a/compose.yaml b/compose.yaml index 13cdcb58..b21072f2 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,12 +1,82 @@ +# Local stand: the API over PostgreSQL (users, chats, messages) and Valkey +# (sessions). Only the API is published, on loopback. Credentials are files in +# ./secrets created by `make secrets`; named volumes keep the data across +# restarts, and only `make destroy` removes them. services: api: build: . image: mtusi-messenger:lab2 - # Fixed name so the Makefile targets and student-tests/lifecycle.sh address - # the same container whether it was started by compose or by docker run. + # Fixed name for docker logs/inspect in the report. container_name: mtusi-messenger-lab2 - env_file: .env + env_file: + - path: .env + required: false + environment: + DATABASE_URL_FILE: /run/secrets/database_url + SESSION_STORE_URL_FILE: /run/secrets/session_store_url + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + secrets: + - database_url + - session_store_url + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_healthy # Loopback only; override with PUBLISH=127.0.0.1:18080 when 8080 is busy. ports: - "${PUBLISH:-127.0.0.1:8080}:8080" stop_grace_period: 10s + + postgres: + image: postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 + environment: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + # Since PostgreSQL 18 the image keeps PGDATA in a versioned directory + # below /var/lib/postgresql, so the volume is mounted on the parent. + volumes: + - pgdata:/var/lib/postgresql + healthcheck: + test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 10s + # A clean shutdown writes a checkpoint instead of leaving crash recovery + # to the next start. + stop_grace_period: 30s + + valkey: + image: valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11 + command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"] + secrets: + - valkey_auth + volumes: + - ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro + - valkeydata:/data + healthcheck: + # valkey-cli exits 0 even on NOAUTH, so the reply itself is checked. + test: ["CMD-SHELL", "VALKEYCLI_AUTH=\"$$(sed -n 's/^requirepass //p' /run/secrets/valkey_auth)\" valkey-cli ping | grep -q PONG"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + stop_grace_period: 10s + +volumes: + pgdata: + valkeydata: + +secrets: + database_url: + file: ./secrets/database_url + session_store_url: + file: ./secrets/session_store_url + postgres_password: + file: ./secrets/postgres_password + valkey_auth: + file: ./secrets/valkey_auth.conf diff --git a/infra/valkey/valkey.conf b/infra/valkey/valkey.conf new file mode 100644 index 00000000..f417d667 --- /dev/null +++ b/infra/valkey/valkey.conf @@ -0,0 +1,17 @@ +# Valkey is the only store of user sessions, so it is their source of truth. +# Every write is fsynced before the reply: an acknowledged login survives a +# crash or a restart. Only logins and logouts write; session lookups are reads +# and pay nothing for this. +appendonly yes +appendfsync always +dir /data + +# The append-only file already persists everything; RDB snapshots are off. +save "" + +# Sessions are never evicted to free memory: a full store refuses new logins +# (503) instead of silently logging users out. +maxmemory-policy noeviction + +# requirepass comes from the compose secret, never from the repository. +include /run/secrets/valkey_auth diff --git a/scripts/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 00000000..ae7df48b --- /dev/null +++ b/scripts/init-secrets.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# Creates the local credentials of the compose stand in ./secrets, which git +# ignores. Existing secrets are kept: PostgreSQL applies its password only +# when the volume is first initialised, so a new password would lock the API +# out of an existing database. To start over: make destroy, then delete +# ./secrets and run this again. +set -eu +cd "$(dirname "$0")/.." + +files="postgres_password database_url valkey_auth.conf session_store_url" +present=0 +for f in $files; do + if [ -e "secrets/$f" ]; then + present=$((present + 1)) + fi +done +if [ "$present" -eq 4 ]; then + echo "secrets already exist in ./secrets, kept" + exit 0 +fi +if [ "$present" -ne 0 ]; then + echo "./secrets is incomplete: delete it (and run make destroy if the stand holds data), then run again" >&2 + exit 1 +fi + +# 24 random bytes as hex: URL-safe without escaping. +random() { + od -An -tx1 -N24 /dev/urandom | tr -d ' \n' +} +pg=$(random) +kv=$(random) + +# The directory is private; the files stay readable for the unprivileged +# users inside the containers, which see them through bind mounts. +umask 022 +mkdir -p secrets +chmod 700 secrets +printf '%s\n' "$pg" > secrets/postgres_password +printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$pg" > secrets/database_url +printf 'requirepass %s\n' "$kv" > secrets/valkey_auth.conf +printf 'redis://:%s@valkey:6379/0\n' "$kv" > secrets/session_store_url +echo "created ./secrets" diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 00000000..5f86f138 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,5 @@ +@echo off +rem Windows entry point of scripts/restart.sh: tests/resilience.py starts the +rem hook without a shell, and Windows cannot execute a .sh file directly. +sh "%~dp0restart.sh" +exit /b %errorlevel% diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..11591f9c --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# Restart hook of `make test-persistence` (tests/resilience.py): stops the +# API, PostgreSQL and Valkey gracefully and starts the same containers again. +# Volumes and data are kept and nothing is re-seeded. The test hides the +# output, so failures surface through the exit code only. +set -eu +cd "$(dirname "$0")/.." +docker compose stop +docker compose up -d --wait diff --git a/student-tests/lifecycle.sh b/student-tests/lifecycle.sh deleted file mode 100644 index 0eda2169..00000000 --- a/student-tests/lifecycle.sh +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/sh -# Lifecycle proof for the in-memory design: -# 1. create a user, a chat and a message, read the message back; -# 2. stop the container with SIGTERM and record how the process shut down; -# 3. start the same container again and show the old X-User-Id is gone (401). -# Usage: BASE_URL=http://localhost:8080 CONTAINER=mtusi-messenger-lab2 sh student-tests/lifecycle.sh -set -eu -BASE_URL=${BASE_URL:-http://localhost:8080} -CONTAINER=${CONTAINER:-mtusi-messenger-lab2} -HERE=$(dirname "$0") - -json_field() { # json_field '' key -> first string value of key - printf '%s' "$1" | sed -n "s/.*\"$2\":\"\([^\"]*\)\".*/\1/p" | head -n 1 -} - -echo "== 1. write and read before restart ==" -user=$(curl -sS -X POST "$BASE_URL/api/v1/users" -H 'Content-Type: application/json' \ - -d "{\"username\":\"lifecycle_$(date +%s)\",\"display_name\":\"Lifecycle\"}") -user_id=$(json_field "$user" id) -[ -n "$user_id" ] || { echo "user was not created: $user" >&2; exit 1; } -chat=$(curl -sS -X POST "$BASE_URL/api/v1/chats" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \ - -d "{\"title\":\"lifecycle\",\"member_ids\":[\"$user_id\"]}") -chat_id=$(json_field "$chat" id) -[ -n "$chat_id" ] || { echo "chat was not created: $chat" >&2; exit 1; } -message=$(curl -sS -X POST "$BASE_URL/api/v1/chats/$chat_id/messages" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \ - -d '{"text":"before restart"}') -message_id=$(json_field "$message" id) -[ -n "$message_id" ] || { echo "message was not created: $message" >&2; exit 1; } -history=$(curl -sS "$BASE_URL/api/v1/chats/$chat_id/messages" -H "X-User-Id: $user_id") -case "$history" in - *"$message_id"*) echo "message $message_id is readable before restart" ;; - *) echo "message missing from history: $history" >&2; exit 1 ;; -esac - -echo "== 2. SIGTERM via docker stop (grace 10s) ==" -started=$(date +%s) -docker stop -t 10 "$CONTAINER" >/dev/null -finished=$(date +%s) -exit_code=$(docker inspect -f '{{.State.ExitCode}}' "$CONTAINER") -echo "docker stop took $((finished - started))s wall clock, container exit code $exit_code" -echo "last log lines:" -docker logs --tail 3 "$CONTAINER" 2>&1 | sed 's/^/ /' -if [ "$exit_code" != "0" ]; then - echo "process did not exit cleanly on SIGTERM" >&2 - exit 1 -fi - -echo "== 3. restart the same container: state must be gone ==" -if ! docker start "$CONTAINER" >/dev/null; then - echo "cannot start $CONTAINER again: run it without --rm so the stopped container survives docker stop" >&2 - exit 1 -fi -BASE_URL="$BASE_URL" sh "$HERE/wait-ready.sh" >/dev/null -me_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/users/me" -H "X-User-Id: $user_id") -chat_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/chats/$chat_id" -H "X-User-Id: $user_id") -echo "GET /users/me with the old X-User-Id -> $me_status" -echo "GET /chats/$chat_id with the old X-User-Id -> $chat_status" -if [ "$me_status" != "401" ] || [ "$chat_status" != "401" ]; then - echo "expected 401 for both after restart" >&2 - exit 1 -fi -echo "PASS: state lived only in the process memory and was lost on restart" diff --git a/student-tests/wait-ready.sh b/student-tests/wait-ready.sh deleted file mode 100644 index 001f32a6..00000000 --- a/student-tests/wait-ready.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# Waits until GET /health/ready answers 200 or the attempt budget runs out. -# Usage: BASE_URL=http://localhost:8080 ATTEMPTS=60 sh student-tests/wait-ready.sh -set -eu -BASE_URL=${BASE_URL:-http://localhost:8080} -ATTEMPTS=${ATTEMPTS:-60} -i=0 -while [ "$i" -lt "$ATTEMPTS" ]; do - if curl -sf "$BASE_URL/health/ready" >/dev/null 2>&1; then - echo "ready: $BASE_URL" - exit 0 - fi - i=$((i + 1)) - sleep 0.5 -done -echo "not ready after $ATTEMPTS attempts: $BASE_URL" >&2 -exit 1 -- 2.54.0 From 8f9252a5c1890f9928876f8fcb986845d63830b5 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:25:26 +0300 Subject: [PATCH 12/21] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20(chat=5Fid,=20id)=20=D0=BA=D0=BB=D1=8E=D1=87=D0=BE?= =?UTF-8?q?=D0=BC=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=D0=BD=D0=B8=D0=B9?= =?UTF-8?q?=20=D0=B8=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20?= =?UTF-8?q?=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80=D0=B8=D0=B9=20EXPLAIN=20?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- migrations/00001_init.sql | 12 +++--- scripts/explain-history.sql | 75 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 scripts/explain-history.sql diff --git a/migrations/00001_init.sql b/migrations/00001_init.sql index e19bbf97..400b762e 100644 --- a/migrations/00001_init.sql +++ b/migrations/00001_init.sql @@ -22,17 +22,19 @@ CREATE TABLE chat_members ( -- Lists the chats of one user in (created_at, id) order: chat ids are UUIDv7. CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id); +-- The key is (chat_id, id) and serves the history query directly: +-- WHERE chat_id = $1 AND id > $2 ORDER BY id LIMIT $3. Nothing looks a +-- message up by id alone, and a separate index on id would only cost writes +-- and tempt the planner into scanning every chat's messages in id order. CREATE TABLE messages ( - id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats (id), + id uuid NOT NULL, sender_id uuid NOT NULL REFERENCES users (id), text text NOT NULL, - created_at timestamptz NOT NULL + created_at timestamptz NOT NULL, + PRIMARY KEY (chat_id, id) ); --- Keyset pagination of one chat's history: WHERE chat_id = $1 AND id > $2 ORDER BY id. -CREATE INDEX messages_chat_id_idx ON messages (chat_id, id); - -- +goose Down DROP TABLE messages; DROP TABLE chat_members; diff --git a/scripts/explain-history.sql b/scripts/explain-history.sql new file mode 100644 index 00000000..f455eedd --- /dev/null +++ b/scripts/explain-history.sql @@ -0,0 +1,75 @@ +-- EXPLAIN evidence for the message history query (make explain). +-- +-- Everything runs in one transaction that is rolled back: it seeds 12 000 +-- messages in one chat and 90 000 in 300 other chats, so the plan is measured +-- on a realistic volume without leaving data on the stand. The PREPARE text +-- is the statement of Messages.ListByChat in internal/chat/store/postgres.go; +-- keep them identical. +\set ON_ERROR_STOP on +\pset pager off +BEGIN; + +SELECT uuidv7() AS author \gset +SELECT uuidv7() AS big_chat \gset + +INSERT INTO users (id, username, display_name, password_hash, created_at) +VALUES (:'author', 'explain_' || substr(md5(random()::text), 1, 12), 'EXPLAIN', 'not-a-password-hash', + uuid_extract_timestamp(:'author')); + +CREATE TEMP TABLE noise_chats ON COMMIT DROP AS +SELECT uuidv7() AS id FROM generate_series(1, 300); + +INSERT INTO chats (id, title, created_at) +SELECT :'big_chat'::uuid, 'explain: big chat', uuid_extract_timestamp(:'big_chat') +UNION ALL +SELECT id, 'explain: other chat', uuid_extract_timestamp(id) FROM noise_chats; + +INSERT INTO chat_members (chat_id, user_id) +SELECT id, :'author' FROM chats WHERE title LIKE 'explain:%'; + +-- Rows are inserted ordered by n, so the messages of all chats interleave in +-- the heap as they would under real traffic; ids stay increasing per chat. +INSERT INTO messages (id, chat_id, sender_id, text, created_at) +SELECT id, chat_id, :'author', 'message ' || n, uuid_extract_timestamp(id) +FROM ( + SELECT uuidv7() AS id, chat_id, n + FROM ( + SELECT :'big_chat'::uuid AS chat_id, n FROM generate_series(1, 12000) AS n + UNION ALL + SELECT c.id, n FROM noise_chats AS c, generate_series(1, 300) AS n + ) AS planned + ORDER BY n +) AS generated; + +ANALYZE users, chats, chat_members, messages; + +SELECT (SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS big_chat_messages, + (SELECT count(*) FROM messages) AS all_messages; + +PREPARE history(uuid, uuid, int) AS +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = $1 + AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid) +ORDER BY id +LIMIT $3; + +\echo +\echo == First page: no cursor, limit 50 (+1 row that tells whether a next page exists) +EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', NULL, 51); + +SELECT id AS deep_cursor FROM messages WHERE chat_id = :'big_chat' ORDER BY id OFFSET 10000 LIMIT 1 \gset +\echo +\echo == Deep page: cursor after the 10 001st message of the chat +EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', :'deep_cursor', 51); + +\echo +\echo == For contrast, OFFSET pagination of the same page reads every skipped row +EXPLAIN (ANALYZE, BUFFERS) +SELECT id, chat_id, sender_id, text, created_at +FROM messages +WHERE chat_id = :'big_chat' +ORDER BY id +OFFSET 10001 LIMIT 51; + +ROLLBACK; -- 2.54.0 From 3001507ecb924dc8b31c1530ff2c433ca4c159a3 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:41:36 +0300 Subject: [PATCH 13/21] =?UTF-8?q?=D0=9E=D1=82=D0=B2=D0=B5=D1=87=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20400=20=D0=B8=20401=20=D0=B2=D0=BC=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=BE=20500=20=D0=BD=D0=B0=20NUL=20=D0=B8=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=BD=D1=8B=D0=B9=20UTF-8=20?= =?UTF-8?q?=D0=B2=D0=BE=20=D0=B2=D1=85=D0=BE=D0=B4=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/app_test.go | 2 +- internal/app/auth_test.go | 2 ++ internal/chat/chat.go | 4 ++++ internal/chat/chat_test.go | 4 +++- internal/chat/message.go | 4 ++++ internal/chat/message_test.go | 2 +- internal/user/service.go | 7 +++++++ internal/user/service_test.go | 8 +++++++- internal/user/user.go | 4 ++++ internal/user/user_test.go | 2 +- 10 files changed, 34 insertions(+), 5 deletions(-) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index e27fd8aa..b9ba72b6 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -396,7 +396,7 @@ func TestMessagesEndToEnd(t *testing.T) { first := ts.message(alice.token, room.ID, "Привет, мир 👋") second := ts.message(bob.token, room.ID, "Сообщение Боба") if first.SenderID != alice.ID || second.SenderID != bob.ID { - t.Fatalf("sender must come from X-User-Id: %+v %+v", first, second) + t.Fatalf("sender must come from the bearer token: %+v %+v", first, second) } if first.ChatID != room.ID || first.Text != "Привет, мир 👋" { t.Fatalf("message = %+v", first) diff --git a/internal/app/auth_test.go b/internal/app/auth_test.go index 3ee7d0ec..602d1e2a 100644 --- a/internal/app/auth_test.go +++ b/internal/app/auth_test.go @@ -54,6 +54,8 @@ func TestLoginFailuresAreUniformBasicChallenges(t *testing.T) { "bearer scheme": {"Authorization": "Bearer " + strings.Repeat("A", 43)}, "not base64": {"Authorization": "Basic !!!"}, "no colon": {"Authorization": "Basic " + encoded("alice")}, + "nul username": basic("a\x00b", testPassword), + "non-utf-8 name": basic("\xff\xfe", testPassword), } messages := map[string]string{} for name, headers := range cases { diff --git a/internal/chat/chat.go b/internal/chat/chat.go index 0c1b2e77..c9281351 100644 --- a/internal/chat/chat.go +++ b/internal/chat/chat.go @@ -46,6 +46,10 @@ func NewChat(id uuid.UUID, title string) (Chat, error) { if utf8.RuneCountInString(title) > MaxTitleCodePoints { return Chat{}, apperr.Invalid(fmt.Sprintf("title must be at most %d characters", MaxTitleCodePoints)) } + // PostgreSQL text cannot hold U+0000. + if strings.ContainsRune(title, 0) { + return Chat{}, apperr.Invalid("title must not contain the NUL character") + } return Chat{ID: id, Title: title, CreatedAt: ident.TimeOf(id)}, nil } diff --git a/internal/chat/chat_test.go b/internal/chat/chat_test.go index 829856c9..f63f60e3 100644 --- a/internal/chat/chat_test.go +++ b/internal/chat/chat_test.go @@ -23,7 +23,9 @@ func TestNewChatDerivesCreatedAtFromID(t *testing.T) { } func TestNewChatValidatesTitle(t *testing.T) { - for _, title := range []string{"", " ", strings.Repeat("я", 101)} { + // PostgreSQL text cannot hold U+0000, so a title with it is invalid input, + // not a storage failure. + for _, title := range []string{"", " ", strings.Repeat("я", 101), "nul\x00inside"} { _, err := chat.NewChat(ident.New(), title) if apperr.KindOf(err) != apperr.KindInvalid { t.Errorf("title %q must be rejected as invalid, got %v", title, err) diff --git a/internal/chat/message.go b/internal/chat/message.go index a825c2bc..c28f6d7d 100644 --- a/internal/chat/message.go +++ b/internal/chat/message.go @@ -30,6 +30,10 @@ func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) { if utf8.RuneCountInString(text) > MaxTextCodePoints { return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints)) } + // PostgreSQL text cannot hold U+0000. + if strings.ContainsRune(text, 0) { + return Message{}, apperr.Invalid("text must not contain the NUL character") + } return Message{ ID: id, ChatID: chatID, diff --git a/internal/chat/message_test.go b/internal/chat/message_test.go index 714b3dad..3cb76738 100644 --- a/internal/chat/message_test.go +++ b/internal/chat/message_test.go @@ -28,7 +28,7 @@ func TestNewMessageValidatesTextByCodePoints(t *testing.T) { if _, err := chat.NewMessage(ident.New(), chatID, alice, strings.Repeat("я", 4000)); err != nil { t.Errorf("4000 code points must be accepted: %v", err) } - for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001)} { + for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001), "nul\x00inside"} { _, err := chat.NewMessage(ident.New(), chatID, alice, text) if apperr.KindOf(err) != apperr.KindInvalid { t.Errorf("text %q must be rejected as invalid, got %v", text, err) diff --git a/internal/user/service.go b/internal/user/service.go index 312474e9..708ddb9b 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -45,7 +45,14 @@ var dummyHash = sync.OnceValue(func() string { // Authenticate returns the user whose password matches, or // ErrInvalidCredentials for an unknown username and a wrong password alike. +// The username comes straight from the Basic header, so one that cannot +// exist (including NUL or invalid UTF-8, which the database would reject) +// is answered without a query, but after the same KDF work. func (s *Service) Authenticate(ctx context.Context, username, plain string) (User, error) { + if !usernamePattern.MatchString(username) { + _, _ = password.Verify(dummyHash(), plain) + return User{}, ErrInvalidCredentials + } u, err := s.repo.GetByUsername(ctx, username) if errors.Is(err, ErrNotFound) { _, _ = password.Verify(dummyHash(), plain) diff --git a/internal/user/service_test.go b/internal/user/service_test.go index 99aecf4a..3406ddd8 100644 --- a/internal/user/service_test.go +++ b/internal/user/service_test.go @@ -172,7 +172,13 @@ func TestAuthenticateFailuresLookTheSame(t *testing.T) { } _, wrong := svc.Authenticate(ctx, "alice", pw+"!") _, unknown := svc.Authenticate(ctx, "nobody", pw) - for name, err := range map[string]error{"wrong password": wrong, "unknown user": unknown} { + // Basic credentials are client bytes: a username the database cannot + // even compare must still be a plain credential failure. + _, nul := svc.Authenticate(ctx, "a\x00b", pw) + _, notUTF8 := svc.Authenticate(ctx, "\xff\xfe", pw) + for name, err := range map[string]error{ + "wrong password": wrong, "unknown user": unknown, "nul username": nul, "invalid utf-8 username": notUTF8, + } { if !errors.Is(err, user.ErrInvalidCredentials) || apperr.KindOf(err) != apperr.KindUnauthorized { t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err) } diff --git a/internal/user/user.go b/internal/user/user.go index f890c414..d05e1c43 100644 --- a/internal/user/user.go +++ b/internal/user/user.go @@ -51,6 +51,10 @@ func New(id uuid.UUID, username, displayName string) (User, error) { if utf8.RuneCountInString(displayName) > MaxDisplayNameCodePoints { return User{}, apperr.Invalid(fmt.Sprintf("display_name must be at most %d characters", MaxDisplayNameCodePoints)) } + // PostgreSQL text cannot hold U+0000. + if strings.ContainsRune(displayName, 0) { + return User{}, apperr.Invalid("display_name must not contain the NUL character") + } return User{ ID: id, Username: username, diff --git a/internal/user/user_test.go b/internal/user/user_test.go index ada07ff2..ede9ec52 100644 --- a/internal/user/user_test.go +++ b/internal/user/user_test.go @@ -68,7 +68,7 @@ func TestNewValidatesDisplayNameByCodePoints(t *testing.T) { t.Errorf("display_name %q must be accepted: %v", name, err) } } - invalid := []string{"", " ", "\t\n", strings.Repeat("я", 101)} + invalid := []string{"", " ", "\t\n", strings.Repeat("я", 101), "nul\x00inside"} for _, name := range invalid { _, err := user.New(ident.New(), "alice", name) if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "display_name") { -- 2.54.0 From 405fb076080a070e16abf88cb7485e920118a92b Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:43:23 +0300 Subject: [PATCH 14/21] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BF=D1=80=D0=B8=D1=87=D0=B8=D0=BD?= =?UTF-8?q?=D1=83=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D0=BE=D0=B2=205xx=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=20=D0=BE=D1=82=D0=BA=D0=B0=D0=B7=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/platform/httpx/httpx_test.go | 20 ++++++++++++++++++++ internal/platform/httpx/respond.go | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/platform/httpx/httpx_test.go b/internal/platform/httpx/httpx_test.go index 16ac1318..1bd0d07b 100644 --- a/internal/platform/httpx/httpx_test.go +++ b/internal/platform/httpx/httpx_test.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io" "log/slog" "net/http" @@ -307,6 +308,25 @@ func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) { assertError(t, rec, eb, http.StatusInternalServerError, "internal_error") } +// A 5xx tells the client only the safe message; the operator must still see +// why the dependency failed, so the cause goes to the log. +func TestUnavailableCauseIsLoggedButNotReturned(t *testing.T) { + logs := &bytes.Buffer{} + e := newEngine(t, logs) + e.GET("/down", func(c *gin.Context) { + cause := errors.New("dial tcp 10.0.0.7:5432: connect: connection refused") + httpx.Error(c, fmt.Errorf("select user: %w", apperr.Wrap(apperr.KindUnavailable, "database unavailable", cause))) + }) + rec, eb := do(t, e, http.MethodGet, "/down", "", nil) + assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable") + if strings.Contains(rec.Body.String(), "connection refused") { + t.Fatalf("cause leaked to the client: %s", rec.Body.String()) + } + if !strings.Contains(logs.String(), "connection refused") || !strings.Contains(logs.String(), eb.Error.RequestID) { + t.Fatalf("log must carry the cause and the request id, got:\n%s", logs.String()) + } +} + // Everything below Timeout sees one deadline, so waiting on a dependency // ends with the request instead of holding the client. func TestTimeoutBoundsTheRequestContext(t *testing.T) { diff --git a/internal/platform/httpx/respond.go b/internal/platform/httpx/respond.go index f28ac418..c89e9712 100644 --- a/internal/platform/httpx/respond.go +++ b/internal/platform/httpx/respond.go @@ -1,6 +1,7 @@ package httpx import ( + "errors" "net/http" "time" @@ -52,7 +53,9 @@ func Fail(c *gin.Context, status int, code, message string) { // Error maps an application error to its status. Anything that is not an // application error is logged with its text and reported as an opaque 500, -// so internal details never reach the client. +// so internal details never reach the client. A known error answered with a +// 5xx (a dependency outage) shows the client only its safe message, and its +// cause goes to the log for the operator. func Error(c *gin.Context, err error) { mapped, known := kinds[apperr.KindOf(err)] if !known { @@ -60,6 +63,13 @@ func Error(c *gin.Context, err error) { Fail(c, http.StatusInternalServerError, "internal_error", "internal error") return } + if mapped.status >= http.StatusInternalServerError { + attrs := []any{"request_id", RequestID(c), "error", err.Error()} + if e, ok := errors.AsType[*apperr.Error](err); ok && e.Cause != nil { + attrs = append(attrs, "cause", e.Cause.Error()) + } + logger(c).Error("request failed", attrs...) + } Fail(c, mapped.status, mapped.code, err.Error()) } -- 2.54.0 From f9850a36c43be090ffcf7dbf37ca04c47a3276cb Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:46:01 +0300 Subject: [PATCH 15/21] =?UTF-8?q?=D0=A3=D0=BF=D1=80=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B9,=20=D0=BB=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BC=D0=B8=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B8=20=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D1=81=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B8=D0=BC=20=D0=BA=D0=BB=D0=B8?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=BE=D0=BC=20Valkey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server/main.go | 4 +++- internal/auth/repository.go | 9 +++----- internal/auth/service.go | 13 ++++------- internal/auth/store/valkey.go | 8 +++---- internal/platform/postgres/postgres.go | 24 +++++++++++++-------- internal/platform/postgres/postgres_test.go | 6 +++++- internal/testenv/testenv.go | 9 ++++---- 7 files changed, 39 insertions(+), 34 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index f3ac4f5c..305e4ff2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -42,9 +42,11 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config) error { return err } defer pool.Close() - if err := postgres.Migrate(ctx, pool); err != nil { + applied, err := postgres.Migrate(ctx, pool) + if err != nil { return err } + log.Info("schema ready", "applied_migrations", applied) sessions, err := valkey.NewClient(ctx, cfg.SessionStoreURL) if err != nil { return err diff --git a/internal/auth/repository.go b/internal/auth/repository.go index 1893e795..4a168f59 100644 --- a/internal/auth/repository.go +++ b/internal/auth/repository.go @@ -2,19 +2,16 @@ package auth import ( "context" - "errors" "github.com/google/uuid" ) -// ErrSessionNotFound means the token is unknown, expired or revoked; the -// store cannot tell these apart, and the client must not either. -var ErrSessionNotFound = errors.New("session not found") - type Repository interface { // Create stores the session until its absolute ExpiresAt. Create(ctx context.Context, s Session) error - // UserID returns the owner of a live session or ErrSessionNotFound. + // UserID returns the owner of a live session, or ErrUnauthenticated when + // the token is unknown, expired or revoked: the store cannot tell these + // apart, and the client must not either. UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) // Delete revokes the session; deleting a missing one is not an error. Delete(ctx context.Context, tokenHash string) error diff --git a/internal/auth/service.go b/internal/auth/service.go index 15ef9abb..458a4744 100644 --- a/internal/auth/service.go +++ b/internal/auth/service.go @@ -2,7 +2,6 @@ package auth import ( "context" - "errors" "time" "github.com/google/uuid" @@ -53,15 +52,11 @@ func (s *Service) Login(ctx context.Context, username, password string) (Issued, return Issued{Token: token, ExpiresAt: expiresAt, User: u}, nil } -// Authenticate returns the owner of the token's live session. A store -// failure is returned as is, never as ErrUnauthenticated: an outage must not -// look like a bad token to the client. +// Authenticate returns the owner of the token's live session or +// ErrUnauthenticated. A store failure stays an unavailability, never +// ErrUnauthenticated: an outage must not look like a bad token to the client. func (s *Service) Authenticate(ctx context.Context, token string) (uuid.UUID, error) { - id, err := s.sessions.UserID(ctx, HashToken(token)) - if errors.Is(err, ErrSessionNotFound) { - return uuid.Nil, ErrUnauthenticated - } - return id, err + return s.sessions.UserID(ctx, HashToken(token)) } func (s *Service) Logout(ctx context.Context, token string) error { diff --git a/internal/auth/store/valkey.go b/internal/auth/store/valkey.go index ff56ad66..ad7a36e1 100644 --- a/internal/auth/store/valkey.go +++ b/internal/auth/store/valkey.go @@ -27,10 +27,10 @@ func NewSessions(client *redis.Client) *Sessions { return &Sessions{client: client} } -// Create sets an absolute expiry (PXAT): a restart of the store replays it -// from the append-only file and does not extend the session. +// Create sets an absolute expiry (EXAT/PXAT): a restart of the store replays +// it from the append-only file and does not extend the session. func (r *Sessions) Create(ctx context.Context, s auth.Session) error { - err := r.client.Do(ctx, "SET", keyPrefix+s.TokenHash, s.UserID.String(), "PXAT", s.ExpiresAt.UnixMilli()).Err() + err := r.client.SetArgs(ctx, keyPrefix+s.TokenHash, s.UserID.String(), redis.SetArgs{ExpireAt: s.ExpiresAt}).Err() if err != nil { return unavailable(err) } @@ -40,7 +40,7 @@ func (r *Sessions) Create(ctx context.Context, s auth.Session) error { func (r *Sessions) UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) { raw, err := r.client.Get(ctx, keyPrefix+tokenHash).Result() if errors.Is(err, redis.Nil) { - return uuid.Nil, auth.ErrSessionNotFound + return uuid.Nil, auth.ErrUnauthenticated } if err != nil { return uuid.Nil, unavailable(err) diff --git a/internal/platform/postgres/postgres.go b/internal/platform/postgres/postgres.go index e075fe29..41c61100 100644 --- a/internal/platform/postgres/postgres.go +++ b/internal/platform/postgres/postgres.go @@ -54,24 +54,30 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) { return pool, nil } -// Migrate applies pending migrations. A session advisory lock serialises -// concurrent starts, so several API replicas may call it at once; applied -// versions are recorded, which makes a repeated run a no-op. -func Migrate(ctx context.Context, pool *pgxpool.Pool) error { +// Migrate applies pending migrations and returns the versions it applied. A +// session advisory lock serialises concurrent starts, so several API +// replicas may call it at once; applied versions are recorded, which makes a +// repeated run a no-op. +func Migrate(ctx context.Context, pool *pgxpool.Pool) ([]int64, error) { db := stdlib.OpenDBFromPool(pool) defer db.Close() locker, err := lock.NewPostgresSessionLocker() if err != nil { - return fmt.Errorf("migration lock: %w", err) + return nil, fmt.Errorf("migration lock: %w", err) } provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.FS, goose.WithSessionLocker(locker)) if err != nil { - return fmt.Errorf("load migrations: %w", err) + return nil, fmt.Errorf("load migrations: %w", err) } - if _, err := provider.Up(ctx); err != nil { - return fmt.Errorf("apply migrations: %w", err) + results, err := provider.Up(ctx) + if err != nil { + return nil, fmt.Errorf("apply migrations: %w", err) } - return nil + applied := make([]int64, 0, len(results)) + for _, r := range results { + applied = append(applied, r.Source.Version) + } + return applied, nil } // Classify marks a failure to reach the database as apperr.KindUnavailable, diff --git a/internal/platform/postgres/postgres_test.go b/internal/platform/postgres/postgres_test.go index 24ee4b48..4546b370 100644 --- a/internal/platform/postgres/postgres_test.go +++ b/internal/platform/postgres/postgres_test.go @@ -32,9 +32,13 @@ func TestMain(m *testing.M) { func TestMigrateIsRepeatable(t *testing.T) { ctx := context.Background() - if err := postgres.Migrate(ctx, db.Pool); err != nil { + applied, err := postgres.Migrate(ctx, db.Pool) + if err != nil { t.Fatalf("second migrate: %v", err) } + if len(applied) != 0 { + t.Fatalf("second migrate applied %v, want nothing", applied) + } var version int64 if err := db.Pool.QueryRow(ctx, `SELECT max(version_id) FROM goose_db_version`).Scan(&version); err != nil { t.Fatal(err) diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index 04c9f42d..980d191d 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -19,6 +19,7 @@ import ( tcvalkey "github.com/testcontainers/testcontainers-go/modules/valkey" "messenger/internal/platform/postgres" + "messenger/internal/platform/valkey" ) const ( @@ -66,7 +67,7 @@ func StartPostgres(ctx context.Context) (*DB, error) { db.Close() return nil, err } - if err := postgres.Migrate(ctx, db.Pool); err != nil { + if _, err := postgres.Migrate(ctx, db.Pool); err != nil { db.Close() return nil, err } @@ -150,12 +151,12 @@ func StartValkey(ctx context.Context) (*KV, error) { kv.Close() return nil, fmt.Errorf("valkey address: %w", err) } - opts, err := redis.ParseURL(kv.URL) + // The production client, so outage tests see its fail-fast settings. + kv.Client, err = valkey.NewClient(ctx, kv.URL) if err != nil { kv.Close() - return nil, fmt.Errorf("valkey url: %w", err) + return nil, err } - kv.Client = redis.NewClient(opts) return kv, nil } -- 2.54.0 From bea31f3f0205dd588f2704c286b6469a8f54b878 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:54:26 +0300 Subject: [PATCH 16/21] =?UTF-8?q?=D0=9E=D0=B3=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=87=D0=B8=D1=82=D1=8C=20=D0=B4=D0=BE=D0=B7=D0=B2=D0=BE=D0=BD?= =?UTF-8?q?=20=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B5=D0=B9=20=D0=BE=D0=B4=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D1=81=D0=B5=D0=BA=D1=83=D0=BD=D0=B4=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/platform/postgres/postgres.go | 10 +++++++++- internal/platform/postgres/postgres_test.go | 4 ++++ internal/platform/valkey/valkey.go | 19 ++++++++++++------- internal/platform/valkey/valkey_test.go | 10 ++++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/internal/platform/postgres/postgres.go b/internal/platform/postgres/postgres.go index 41c61100..45f4eda9 100644 --- a/internal/platform/postgres/postgres.go +++ b/internal/platform/postgres/postgres.go @@ -26,7 +26,7 @@ import ( const ( maxConns = 10 - connectTimeout = 3 * time.Second + connectTimeout = time.Second statementTimeout = 5 * time.Second idleInTxTimeout = 10 * time.Second ) @@ -40,6 +40,14 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) { } cfg.MaxConns = maxConns cfg.ConnConfig.ConnectTimeout = connectTimeout + // pgconn applies ConnectTimeout to dialling but resolves the host name + // before that with the caller's context. Docker DNS takes seconds to give + // up on a stopped container, so the lookup gets the same bound. + cfg.ConnConfig.LookupFunc = func(ctx context.Context, host string) ([]string, error) { + ctx, cancel := context.WithTimeout(ctx, connectTimeout) + defer cancel() + return net.DefaultResolver.LookupHost(ctx, host) + } cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10) cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10) diff --git a/internal/platform/postgres/postgres_test.go b/internal/platform/postgres/postgres_test.go index 4546b370..b3b3a917 100644 --- a/internal/platform/postgres/postgres_test.go +++ b/internal/platform/postgres/postgres_test.go @@ -8,6 +8,7 @@ import ( "net" "os" "testing" + "time" "github.com/jackc/pgx/v5/pgconn" @@ -61,6 +62,9 @@ func TestPoolAppliesLimitsAndTimeouts(t *testing.T) { if got := db.Pool.Config().MaxConns; got != 10 { t.Fatalf("MaxConns = %d, want 10", got) } + if got := db.Pool.Config().ConnConfig.ConnectTimeout; got != time.Second { + t.Fatalf("ConnectTimeout = %v, want 1s", got) + } ctx := context.Background() for setting, want := range map[string]string{ "statement_timeout": "5s", diff --git a/internal/platform/valkey/valkey.go b/internal/platform/valkey/valkey.go index 15a4c0af..70c5ba5d 100644 --- a/internal/platform/valkey/valkey.go +++ b/internal/platform/valkey/valkey.go @@ -1,9 +1,12 @@ // Package valkey connects to the Valkey session store. // // Timeouts are constants: every authenticated request reads a session, so -// they bound how long a request can wait when the store is slow. Retries are -// disabled for the same reason: a request that cannot reach the store fails -// fast with 503 instead of waiting through a backoff. +// they bound how long a request can wait when the store is slow. Command +// retries are disabled and a connection is dialled once for the same reason: +// a request that cannot reach the store fails fast with 503 instead of +// waiting through a backoff. On the compose stand Docker DNS takes seconds +// to give up on a stopped container, so one dial attempt is already bounded +// only by dialTimeout. package valkey import ( @@ -16,10 +19,11 @@ import ( ) const ( - dialTimeout = 2 * time.Second - ioTimeout = time.Second - poolSize = 10 - noRetries = -1 + dialTimeout = time.Second + ioTimeout = time.Second + poolSize = 10 + noRetries = -1 + singleAttempt = 1 ) // NewClient connects to url (redis:// scheme) and pings the server. Errors @@ -34,6 +38,7 @@ func NewClient(ctx context.Context, url string) (*redis.Client, error) { opts.WriteTimeout = ioTimeout opts.PoolSize = poolSize opts.MaxRetries = noRetries + opts.DialerRetries = singleAttempt client := redis.NewClient(opts) if err := client.Ping(ctx).Err(); err != nil { diff --git a/internal/platform/valkey/valkey_test.go b/internal/platform/valkey/valkey_test.go index af13203f..efb43ebf 100644 --- a/internal/platform/valkey/valkey_test.go +++ b/internal/platform/valkey/valkey_test.go @@ -24,8 +24,14 @@ func TestNewClientPingsAndBoundsWaiting(t *testing.T) { } defer client.Close() opts := client.Options() - if opts.DialTimeout != 2*time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second { - t.Fatalf("timeouts = %v/%v/%v, want 2s/1s/1s", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout) + if opts.DialTimeout != time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second { + t.Fatalf("timeouts = %v/%v/%v, want 1s each", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout) + } + // go-redis dials up to five times by default; with Docker DNS taking + // seconds to give up on a stopped container that alone would exceed the + // request deadline. + if opts.DialerRetries != 1 { + t.Fatalf("DialerRetries = %d, want a single dial attempt", opts.DialerRetries) } // go-redis stores the requested -1 ("no retries") as 0 after init; its // default would be 3. -- 2.54.0 From ba535c6ee69273c9cd5f1f71c2c528a71106ae0b Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:54:26 +0300 Subject: [PATCH 17/21] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=8F=D1=82=D1=8C=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20PostgreSQL=20=D0=BF=D0=BE=20TCP=20=D0=B8?= =?UTF-8?q?=20=D0=BE=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=B8=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D1=8C=20Valkey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 4 +++- infra/valkey/valkey.conf | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/compose.yaml b/compose.yaml index b21072f2..721398ba 100644 --- a/compose.yaml +++ b/compose.yaml @@ -41,7 +41,9 @@ services: volumes: - pgdata:/var/lib/postgresql healthcheck: - test: ["CMD", "pg_isready", "-U", "messenger", "-d", "messenger"] + # Over TCP: during first initialisation the image runs a temporary + # server on the Unix socket only, which must not count as ready. + test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "messenger", "-d", "messenger"] interval: 5s timeout: 3s retries: 10 diff --git a/infra/valkey/valkey.conf b/infra/valkey/valkey.conf index f417d667..d7e96bff 100644 --- a/infra/valkey/valkey.conf +++ b/infra/valkey/valkey.conf @@ -9,8 +9,10 @@ dir /data # The append-only file already persists everything; RDB snapshots are off. save "" -# Sessions are never evicted to free memory: a full store refuses new logins -# (503) instead of silently logging users out. +# Sessions are never evicted to free memory: once maxmemory is reached the +# store refuses new sessions (login answers 503) instead of silently logging +# users out. A session key takes about a hundred bytes. +maxmemory 256mb maxmemory-policy noeviction # requirepass comes from the compose secret, never from the repository. -- 2.54.0 From 949622ed93f082c436be79fd2e368661ea1c6c61 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:58:55 +0300 Subject: [PATCH 18/21] =?UTF-8?q?=D0=9D=D0=B5=20=D0=BF=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D1=8B=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BA=D0=BB=D0=B8=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D1=83=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D1=82=20=D0=BE=D0=B1=D1=91=D1=80=D1=82=D0=BA=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/platform/httpx/httpx_test.go | 6 ++++-- internal/platform/httpx/respond.go | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/internal/platform/httpx/httpx_test.go b/internal/platform/httpx/httpx_test.go index 1bd0d07b..a745d5a9 100644 --- a/internal/platform/httpx/httpx_test.go +++ b/internal/platform/httpx/httpx_test.go @@ -319,8 +319,10 @@ func TestUnavailableCauseIsLoggedButNotReturned(t *testing.T) { }) rec, eb := do(t, e, http.MethodGet, "/down", "", nil) assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable") - if strings.Contains(rec.Body.String(), "connection refused") { - t.Fatalf("cause leaked to the client: %s", rec.Body.String()) + // Neither the cause nor the wrapping context of internal operations + // ("select user: ...") may reach the client. + if eb.Error.Message != "database unavailable" { + t.Fatalf("client message = %q, want only the application message", eb.Error.Message) } if !strings.Contains(logs.String(), "connection refused") || !strings.Contains(logs.String(), eb.Error.RequestID) { t.Fatalf("log must carry the cause and the request id, got:\n%s", logs.String()) diff --git a/internal/platform/httpx/respond.go b/internal/platform/httpx/respond.go index c89e9712..30f49354 100644 --- a/internal/platform/httpx/respond.go +++ b/internal/platform/httpx/respond.go @@ -53,11 +53,17 @@ func Fail(c *gin.Context, status int, code, message string) { // Error maps an application error to its status. Anything that is not an // application error is logged with its text and reported as an opaque 500, -// so internal details never reach the client. A known error answered with a -// 5xx (a dependency outage) shows the client only its safe message, and its -// cause goes to the log for the operator. +// so internal details never reach the client. An application error shows the +// client only its own message, never the context it was wrapped in on the +// way up; when it is answered with a 5xx (a dependency outage) the full +// chain and the cause go to the log for the operator. func Error(c *gin.Context, err error) { - mapped, known := kinds[apperr.KindOf(err)] + appErr, isApp := errors.AsType[*apperr.Error](err) + var mapped statusCode + known := false + if isApp { + mapped, known = kinds[appErr.Kind] + } if !known { logger(c).Error("request failed", "request_id", RequestID(c), "error", err.Error()) Fail(c, http.StatusInternalServerError, "internal_error", "internal error") @@ -65,12 +71,12 @@ func Error(c *gin.Context, err error) { } if mapped.status >= http.StatusInternalServerError { attrs := []any{"request_id", RequestID(c), "error", err.Error()} - if e, ok := errors.AsType[*apperr.Error](err); ok && e.Cause != nil { - attrs = append(attrs, "cause", e.Cause.Error()) + if appErr.Cause != nil { + attrs = append(attrs, "cause", appErr.Cause.Error()) } logger(c).Error("request failed", attrs...) } - Fail(c, mapped.status, mapped.code, err.Error()) + Fail(c, mapped.status, mapped.code, appErr.Message) } // Timestamp renders RFC 3339 UTC with a fixed millisecond width: Python 3.9 -- 2.54.0 From 956b84f2d7fcda016e9d6377b7830977bf4e3705 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 16:05:24 +0300 Subject: [PATCH 19/21] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=BE=D1=82=D1=87=D1=91=D1=82=20=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D1=82=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=B0=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D0=BE=D0=B9=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/pull_request_template.md | 28 +++--- REPORT.md | 144 +++++++++++++++++++++++++++---- evidence/bootstrap.txt | 63 ++++++++++++++ evidence/concurrency.json | 16 ++++ evidence/dependency-failures.txt | 30 +++++++ evidence/expiry.txt | 12 +++ evidence/explain-history.txt | 61 +++++++++++++ evidence/persistence.txt | 17 ++++ evidence/smoke.txt | 9 ++ evidence/tests.txt | 49 +++++++++++ 10 files changed, 400 insertions(+), 29 deletions(-) create mode 100644 evidence/bootstrap.txt create mode 100644 evidence/concurrency.json create mode 100644 evidence/dependency-failures.txt create mode 100644 evidence/expiry.txt create mode 100644 evidence/explain-history.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/smoke.txt create mode 100644 evidence/tests.txt diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 8ceefa10..610c14a1 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,18 +1,18 @@ ## Лабораторная 2 -Автор / группа: TODO -Целевая оценка (3/4/5): TODO -Трек 5, если заявлен: TODO +Автор / группа: Бакулин, БПИ2301 +Целевая оценка (3/4/5): 4 +Трек 5, если заявлен: не заявлен -Что реализовано и что перенесено: TODO -Команды запуска из чистого clone: TODO -Результаты проверок и ссылка на REPORT.md: TODO -Ограничения / известные ошибки: TODO -Источники и использование ИИ: TODO +Что реализовано и что перенесено: перенесён минимум lab1 (формат ошибок, keyset-пагинация, UUIDv7, graceful shutdown). Пользователи, чаты, участники и сообщения хранятся в PostgreSQL 18 (pgx, миграции goose при старте, атомарное создание чата). Регистрация с паролем (Argon2id), вход через Basic, непрозрачный Bearer-токен, сессии в Valkey с AOF `appendfsync always` и абсолютным сроком, logout. Недоступность PostgreSQL или Valkey даёт 503 с причиной в логе. Стенд в compose с secrets, хук перезапуска, EXPLAIN истории. Подробности и исправления базы lab1 — в REPORT.md. +Команды запуска из чистого clone: `make secrets && make up`, затем `make test`, `make test-persistence ACTION_SCRIPT=scripts/restart.sh`, `make unit` (нужен Docker), `make explain`; удаление данных — `make destroy`. +Результаты проверок и ссылка на REPORT.md: REPORT.md, раздел «Доказательства критериев»; файлы в `evidence/`. +Ограничения / известные ошибки: один host без репликации; потеря Valkey разлогинивает всех; без TLS (lab7); описание семантики пагинации при конкурентных вставках — в REPORT.md. +Источники и использование ИИ: перечислены в REPORT.md. ИИ-ассистент использовался как парный программист: генерация кода, тестов и черновиков документации по моим архитектурным решениям с последующим ревью каждого модуля и переработкой по замечаниям; выбор стека, структура проекта, модель данных, обработка ошибок, логирование и границы модулей определены мной; все проверки воспроизведены мной вручную. -- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. -- [ ] Контракт и публичные тесты не ослаблены. -- [ ] make check и make test выполнены; приложен реальный вывод. -- [ ] Сценарии отказов/безопасности своего уровня воспроизведены. -- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. -- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. +- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами. +- [x] Контракт и публичные тесты не ослаблены. +- [x] make check и make test выполнены; приложен реальный вывод. +- [x] Сценарии отказов/безопасности своего уровня воспроизведены. +- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens. +- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения. diff --git a/REPORT.md b/REPORT.md index e42f2b05..698b6fc5 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,151 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Бакулин, БПИ2301 +- Целевая оценка; выбранный трек 5, если нужен: 4. Уровни 3 и 4 выполнены полностью, трек 5 не заявлен. +- Commit/PR: ветка `solution/bpi2301-bakulin`; ссылка на PR — после публикации. +- Использованные источники, библиотеки и помощь ИИ: документация PostgreSQL 18 (транзакции, EXPLAIN, `uuidv7`, типы даты/времени), PostgreSQL wiki «Don't Do This», документация Valkey (persistence, ACL/requirepass, eviction), RFC 7617 (Basic), RFC 6750 и RFC 7235 (Bearer, `WWW-Authenticate`), RFC 9106 и OWASP Password Storage Cheat Sheet (Argon2id), документация Docker Compose (secrets, healthcheck, `depends_on`). Библиотеки: `github.com/gin-gonic/gin` v1.12.0, `github.com/google/uuid` v1.6.0, `github.com/jackc/pgx/v5` v5.11.0 (драйвер и пул), `github.com/pressly/goose/v3` v3.27.0 (миграции), `github.com/redis/go-redis/v9` v9.23.0 (клиент Valkey), `golang.org/x/crypto/argon2`, в тестах `github.com/testcontainers/testcontainers-go` v0.39.0. ИИ-ассистент использовался как парный программист: генерация кода, тестов и черновиков документации по моим архитектурным решениям с последующим ревью каждого модуля и переработкой по замечаниям; выбор стека, структура проекта, модель данных, обработка ошибок, логирование и границы модулей определены мной; все проверки воспроизведены мной вручную. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Версии, на которых выполнялись проверки: Go 1.27.1 (`go.mod`), Docker Desktop 28.5.1 с Compose v2 (нужен 2.24+ из-за `env_file … required: false`), Python 3.13 для публичных проверок (нужен 3.9+), GNU Make. Образы закреплены digest'ами: `postgres:18.6-alpine3.24`, `valkey/valkey:9.1.2-alpine3.24` в `compose.yaml`, `golang:1.27.1-alpine3.23` и `alpine:3.23` в `Dockerfile`. + +```sh +make secrets # ./secrets: пароли PostgreSQL и Valkey и URL для API; существующие файлы не перезаписываются +make up # docker compose up -d --build --wait: postgres и valkey healthy → api применяет миграции → healthy +make check PYTHON=python +make test PYTHON=python +make test-persistence ACTION_SCRIPT=scripts/restart.sh PYTHON=python # на Windows: ACTION_SCRIPT=scripts/restart.cmd +make unit # собственные тесты; нужен запущенный Docker: тесты поднимают свои PostgreSQL и Valkey +make explain > evidence/explain-history.txt +make concurrency # 10 клиентов × 10 сообщений, отчёт в evidence/concurrency.json +``` + +`.env` не обязателен: все настройки имеют значения по умолчанию, а пути к секретам задаёт `compose.yaml`. Если порт 8080 занят — `PUBLISH=127.0.0.1:18080 make up` и `BASE_URL=http://localhost:18080` для проверок. Короткий TTL для проверки истечения: `SESSION_TTL_SECONDS=2 make up` (Compose пересоздаст только api; вернуть — `make up`). + +Секреты. `scripts/init-secrets.sh` создаёт в `./secrets` (каталог в `.gitignore` и `.dockerignore`) четыре файла: пароль PostgreSQL, DSN для API, `requirepass` для Valkey и URL Valkey для API. Compose монтирует их как secrets в `/run/secrets/*`; API читает их через `DATABASE_URL_FILE` и `SESSION_STORE_URL_FILE`, PostgreSQL — через `POSTGRES_PASSWORD_FILE`, Valkey — через `include` в `infra/valkey/valkey.conf`. Значения секретов не попадают ни в env контейнера (`docker inspect`), ни в образ, ни в логи. Скрипт не перезаписывает существующие файлы: PostgreSQL применяет пароль только при инициализации volume, новый пароль при живом volume заблокировал бы API. + +Bootstrap и миграции. Схема — `migrations/00001_init.sql`, вшита в бинарник через `embed`. API при старте до открытия порта применяет миграции goose под session advisory lock (несколько реплик в lab3 сериализуются на нём), в логе `schema ready applied_migrations=[1]`, повторный старт — `applied_migrations=[]`. Отдельного шага оператора нет. + +Остановка и удаление данных. `docker compose stop` — graceful остановка всех трёх процессов (API дорабатывает запросы, PostgreSQL пишет checkpoint, Valkey дописывает AOF); `make down` — удалить контейнеры с сохранением volumes; `make destroy` (`docker compose down -v`) — **намеренное удаление всех учебных данных**: пользователей, чатов, сообщений и сессий. Для полного сброса затем удалить `./secrets`. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +``` +клиент ──HTTP──> api (Go, gin) ──pgx pool (≤10)──> postgres [volume pgdata] users, chats, chat_members, messages + └─go-redis (≤10)──> valkey [volume valkeydata] session: → user_id +``` + +Источник истины: PostgreSQL — для пользователей, чатов, участников и сообщений; Valkey — единственное хранилище сессий и, значит, их источник истины (кэша в работе нет). Обе зависимости критичны для синхронного пути: без Valkey нельзя аутентифицировать ни один запрос, без PostgreSQL — прочитать или записать данные. `/health/ready` проверяет обе (общий бюджет 1 с) и флаг shutdown; `/health/live` — только процесс. + +Модель данных (`migrations/00001_init.sql`). В схеме только модель данных — `NOT NULL`, PK, FK, `UNIQUE`; правила валидации (форматы, длины) живут в приложении и в БД не дублируются. Время — `timestamptz` (момент времени, в UTC по построению), `created_at` пишет приложение из UUIDv7 id, поэтому порядок `(created_at, id)` совпадает с порядком id. + +| Таблица | Ключи и индексы | Зачем | +| --- | --- | --- | +| users | PK id, `users_username_key UNIQUE (username)` | уникальность username держит constraint, поэтому гонка регистраций безопасна | +| chats | PK id | | +| chat_members | PK (chat_id, user_id), индекс (user_id, chat_id), FK на chats и users | членство и состав чата; список чатов пользователя keyset-ом по второму индексу | +| messages | PK (chat_id, id), FK на chats и users | ключ и есть индекс истории: `WHERE chat_id=$1 AND id>$2 ORDER BY id LIMIT $3` | + +Ключ сообщений — `(chat_id, id)`, отдельного индекса по id нет. Сообщение по одному id API не ищет, а такой индекс стоил бы записи и, как показал EXPLAIN на 102 000 сообщений, уводил планировщик в обход всех сообщений по порядку id с фильтром по чату (15 303 отброшенные строки и 1092 буфера на первой странице). Цена: уникальность id гарантирована внутри чата; id генерирует сервер (UUIDv7, 74 случайных бита), коллизия практически исключена. Время диапазоном тоже не требует отдельного индекса: UUIDv7 содержит время, интервал `[t1, t2)` превращается в диапазон id по тому же ключу. + +Транзакционные границы. Регистрация — один `INSERT`, `23505` по `users_username_key` даёт 409. Создание чата — одна транзакция: `INSERT chats` и `INSERT chat_members … unnest($2::uuid[])`; существование участников проверяется до транзакции одним batch-запросом (400 на неизвестного), FK остаётся страховкой. Отправка сообщения — проверка членства и один `INSERT`. Чтения — одиночные `SELECT` на `READ COMMITTED`. Ограничение (contracts/README:20): id выдаётся до `INSERT`, поэтому коммиты конкурентных отправок могут лечь не в порядке id; читатель, чей cursor уже прошёл больший id, не увидит «опоздавшее» сообщение при продолжении обхода, при чтении с начала оно стоит на своём месте, повторов нет. + +Аутентификация (`internal/auth`). `POST /api/v1/auth/sessions` принимает только `Authorization: Basic` (`Request.BasicAuth` делит строку по первому двоеточию, так что двоеточие в пароле допустимо). Неизвестный пользователь, неверный пароль, username с NUL или не-UTF-8 дают одинаковый 401 с `WWW-Authenticate: Basic realm="messenger", charset="UTF-8"`; для неизвестного username всё равно выполняется KDF на фиктивном хеше, чтобы время ответа не выдавало существование пользователя. Успех — `201 {token, token_type: "Bearer", expires_at, user}`. Токен — 32 случайных байта в base64url (256 бит), непрозрачный. В Valkey хранится ключ `session:` со значением user_id и абсолютным сроком (`SET … PXAT/EXAT`): утечка AOF не даёт рабочих токенов, а рестарт не продлевает сессию. Все остальные endpoint'ы требуют `Authorization: Bearer ` (схема без учёта регистра); отсутствие, Basic, неизвестный, истёкший или отозванный токен — 401 с `WWW-Authenticate: Bearer realm="messenger"`. `X-User-Id` не читается вообще. `DELETE /api/v1/auth/sessions/current` удаляет ключ и отвечает 204, повтор — 401. Срок жизни задаёт `SESSION_TTL_SECONDS` и фиксируется при входе; скользящего продления нет, иначе `expires_at` в ответе был бы ложью. Пароли — Argon2id (`m=19 MiB, t=2, p=1`, соль 16 байт) в PHC-строке; параметры читаются из строки, поэтому их можно поднять без инвалидации старых хешей. + +Valkey (`infra/valkey/valkey.conf`): `appendonly yes`, `appendfsync always` — запись сессии подтверждается после fsync, как COMMIT в PostgreSQL; пишут только вход и выход, чтение сессии fsync не трогает. `save ""` — RDB не нужен рядом с AOF. `maxmemory 256mb` с `noeviction` — сессии никогда не вытесняются: при заполнении новые входы получают 503, а не молча разлогинивают пользователей. Пароль Valkey задан: иначе любой процесс в docker-сети мог бы записать `session:` и подделать сессию. + +Отказы и таймауты. Пул PostgreSQL ограничен 10 соединениями, `statement_timeout=5s`, `idle_in_transaction_session_timeout=10s`, подключение и поиск имени хоста — 1 с. Клиент Valkey: пул 10, дозвон 1 с одной попыткой, чтение и запись 1 с, повторы команд выключены. Каждый запрос к `/api` ограничен дедлайном 5 с (меньше `WriteTimeout` сервера 10 с, чтобы 503 успел записаться). Недоступность зависимости (отказ соединения, таймаут, классы ошибок PostgreSQL 08/53/57) превращается в `503 unavailable`; клиенту уходит только безопасное сообщение (`database unavailable`, `session store unavailable`), причина пишется в лог строкой `request failed` с `request_id` и `cause`. Аноним при недоступном Valkey не пропускается: ответ 503, а не 401 и не 200. Регистрация от Valkey не зависит и продолжает работать. + +Код и что изменилось относительно lab1: + +``` +cmd/server конфигурация, пул PostgreSQL, миграции, клиент Valkey, listener, сигналы +migrations/ SQL-миграции goose, вшиты в бинарник +internal/app composition root (Deps: пул, клиент Valkey, TTL, таймаут запроса) +internal/server http.Server и graceful shutdown; принимает http.Handler и флаг readiness +internal/auth session.go, repository.go, service.go, handler.go, middleware.go; store/valkey.go +internal/user + пароль и Authenticate; store/postgres.go (inmem удалён) +internal/chat участники в каноническом порядке по id; store/postgres.go (inmem удалён) +internal/health live/ready с проверками зависимостей +internal/platform/ config (+ *_FILE, SESSION_TTL_SECONDS), postgres (пул, Migrate, Classify), + valkey (клиент), password (Argon2id), httpx (+ WithUser, Timeout), apperr, ident, pagination +internal/testenv PostgreSQL и Valkey в testcontainers для тестов; в бинарник не входит +tools/concurrency чёрный ящик гонок, теперь с паролем и Bearer +``` + +Перенесено из lab1 без изменений: формат ошибок и `X-Request-Id`, keyset-пагинация и cursor, UUIDv7 и вывод `created_at` из id, access log по уровням, graceful shutdown. Изменено по объявленному переходу 1 → 2: регистрация требует `password`, `X-User-Id` заменён Bearer-сессией, Basic только на входе. Исправления базы: `WithUser` переехал из `auth` в `httpx` (иначе цикл импортов `auth ↔ user`), `server.Run` больше не зависит от сборки приложения, `member_ids` всегда в порядке id (контракт считает их множеством). Удалены in-memory хранилища и сценарий `student-tests/lifecycle.sh` (он проверял потерю данных после рестарта — в lab2 нужно обратное, это проверяет `make test-persistence`). + +Тесты. Сервисы, хранилища и HTTP тестируются против настоящих PostgreSQL и Valkey (testcontainers-go, один контейнер на пакет, `TRUNCATE` всех таблиц и `FLUSHDB` перед каждым тестом); чистая логика (валидация, cursor, конфигурация, Argon2id) — обычными юнит-тестами. testcontainers закреплён на v0.39.0: с v0.40 он запрашивает публикацию порта с `HostPort "0"`, которую Docker Desktop 28.5.1 на Windows не выполняет; goose — на v3.27.0 из-за совместимости транзитивной зависимости с этой версией. + +Соответствие `.env.example` реальной конфигурации: + +| Переменная | `.env.example` | Реализация | По умолчанию | +| --- | --- | --- | --- | +| APP_HOST, APP_PORT, APP_ENV | да | как в lab1 | 0.0.0.0, 8080, lab | +| SESSION_TTL_SECONDS | да | срок сессии, целое > 0 | 3600 | +| DATABASE_URL_FILE | да | файл с DSN PostgreSQL, обязателен | `/run/secrets/database_url` в compose | +| SESSION_STORE | да (`postgres`) | не читается: сессии всегда в Valkey | — | +| CACHE_URL_FILE | да | не используется: кэша нет; имя оставлено для кэша, если он появится | — | +| SESSION_STORE_URL_FILE | нет | файл с URL Valkey, обязателен | `/run/secrets/session_store_url` в compose | +| APP_LOG_LEVEL, APP_SHUTDOWN_TIMEOUT, APP_MAX_BODY_BYTES | нет | как в lab1 | info, 8s, 65536 | + +Ошибка в любой переменной или нечитаемый файл секрета останавливают процесс на старте с кодом 2; сообщение называет переменную, но не значение. + +Роли стенда: + +| Роль | Сервис | Версия | Health/readiness | Volume | Сеть/порт | Credentials | +| --- | --- | --- | --- | --- | --- | --- | +| api | api (`mtusi-messenger-lab2`) | сборка из исходников | `/health/ready` (PG + Valkey) | — | `127.0.0.1:8080` | secrets `database_url`, `session_store_url` | +| postgres | postgres | 18.6-alpine3.24 @digest | `pg_isready -h 127.0.0.1` | `pgdata:/var/lib/postgresql` | только внутренняя сеть | secret `postgres_password` | +| redis_or_valkey | valkey | 9.1.2-alpine3.24 @digest | `valkey-cli ping` с паролем | `valkeydata:/data` (AOF) | только внутренняя сеть | secret `valkey_auth` | + +CI. `.github/workflows/app.yml`: unit-тесты с race detector (на runner нужен Docker для testcontainers), затем `make secrets`, `docker compose up --build --wait`, smoke, persistence с `scripts/restart.sh`, concurrency, логи и `down -v` (стенд CI одноразовый). Skeleton-job `contracts.yml` не менялся. ## Доказательства критериев +Все файлы сняты на одном стенде, поднятом с пустых volumes (дата, commit и параметры хоста в заголовке каждого файла). + | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make check`, `make test` | оба PASS | evidence/smoke.txt | +| Минимум lab1 с переходом на Bearer | `make test` (сценарий lab2), `TestProtectedRoutesAcceptOnlyBearer` и остальные HTTP-тесты `internal/app` | PASS | evidence/smoke.txt, evidence/tests.txt | +| Данные и действующие сессии переживают рестарт API и хранилищ без удаления volumes | `make test-persistence ACTION_SCRIPT=scripts/restart.cmd` (`restart.sh`: `docker compose stop` + `up -d --wait`) | PASS тем же токеном; `StartedAt` всех трёх контейнеров изменился | evidence/persistence.txt | +| PostgreSQL: пользователи, чаты, участники, сообщения; PK/FK, UNIQUE username; миграции с нуля | `make destroy && make up`, `\dt`, `\di` | `schema ready applied_migrations=[1]`, 4 таблицы и индексы | evidence/bootstrap.txt | +| Повторный запуск миграций без дублирования | рестарт api; `TestMigrateIsRepeatable` | `applied_migrations=[]` | evidence/bootstrap.txt, evidence/tests.txt | +| Атомарное создание чата и его rollback | `TestCreateRollsBackWhenMemberInsertFails` | после ошибки FK нет ни строки чата, ни участников | evidence/tests.txt | +| Basic только на входе, Bearer на остальных, `X-User-Id` не даёт доступ | `TestProtectedRoutesAcceptOnlyBearer`, `TestLoginFailuresAreUniformBasicChallenges` | 401 с нужным `WWW-Authenticate`; `X-User-Id` игнорируется | evidence/tests.txt | +| Срок жизни и отзыв сессии | `TestSessionExpiresAfterTTL`, `TestLogoutRevokesOnlyTheCurrentToken`; стенд с `SESSION_TTL_SECONDS=2` | через 3 с 401 Bearer; logout 204, повтор 401 | evidence/expiry.txt, evidence/tests.txt | +| Пароли через password KDF | `TestRegisterStoresUserWithPasswordHash` | в БД `$argon2id$v=19$m=19456,t=2,p=1$…` | evidence/tests.txt | +| Secrets/volumes не в Git | `git ls-files secrets .env` | пусто; `secrets/` в `.gitignore` и `.dockerignore` | — | +| Valkey реально используется для сессий с TTL и persistence | `CONFIG GET`; остановка/запуск Valkey с тем же токеном | `appendonly yes`, `appendfsync always`, `noeviction`; после рестарта Valkey токен работает | evidence/bootstrap.txt, evidence/dependency-failures.txt | +| Параметризованные запросы, ограниченный пул и таймауты | код `store/postgres.go` (только `$n`), `TestPoolAppliesLimitsAndTimeouts`, `TestTimeoutBoundsTheRequestContext` | `MaxConns=10`, `statement_timeout=5s`, connect 1 с, дедлайн запроса | evidence/tests.txt | +| Гонка регистрации одного username | `TestConcurrentRegistrationOfOneUsernameHasOneWinner` | 10 параллельных попыток: ровно 1 успех, 9 × 409, одна строка | evidence/tests.txt | +| Индекс истории обоснован EXPLAIN на ≥10 000 сообщений, без полного scan на страницу | `make explain` (102 000 сообщений, 12 000 в целевом чате, транзакция с ROLLBACK) | `Index Scan using messages_pkey`, `Index Cond (chat_id = … AND id > …)`, 51 строка; 51 буфер на первой странице и 6 на глубокой, 0.05–0.1 мс; OFFSET той же страницы — сортировка 10 052 строк, 495 буферов, 4.6 мс | evidence/explain-history.txt | +| Автопроверки expiry, logout, неверного пароля, недоступности Redis/БД | `TestSessionExpiresAfterTTL`, `TestLogoutRevokesOnlyTheCurrentToken`, `TestLoginFailuresAreUniformBasicChallenges`, `TestValkeyOutageAnswers503`, `TestPostgresOutageAnswers503`, `TestHungPostgresAnswers503WithinTheRequestTimeout` | все PASS; при отказе — 503, никогда 200 или 401 | evidence/tests.txt, evidence/dependency-failures.txt | +| Гонки отправки сообщений | `make concurrency` | 100/100 созданы, перечислены и уникальны, порядок по id | evidence/concurrency.json | +| Выбранный трек 5 | не заявлен | — | — | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Стенд: Windows 11 Pro, Docker Desktop 28.5.1 (4 CPU, 3.8 GiB RAM для Linux-VM), без лимитов CPU/RAM на контейнеры, loopback-сеть. Нагрузочные измерения не проводились (трек 5 не заявлен); ниже проверки отказов уровня 4. | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Полный рестарт: `docker compose stop` и `up -d --wait` (хук persistence) | те же данные и тот же токен | PASS, все три процесса перезапущены | весь тест с хуком ~15 с; потерь нет | +| `docker compose stop valkey` | 503 на защищённые запросы и вход, ready 503, регистрация работает | 503 за 1.2 с (дозвон ограничен 1 с; DNS Docker для остановленного контейнера отвечает ~4 с), `POST /users` 201, в логе `cause: dial tcp: lookup valkey: i/o timeout` | после `start` тот же токен 200: сессии восстановлены из AOF | +| `docker compose stop postgres` | 503 на операции с данными, ready 503 | 503 за 0.2 с, в логе причина подключения | после `start` 200 без перезапуска API: пул переподключается | +| PostgreSQL на паузе (`docker pause`, тест) | запрос не висит дольше дедлайна | 503 при дедлайне 300 мс, ready 503 | — | +| `SESSION_TTL_SECONDS=2` | сессия истекает | 200 сразу, 401 Bearer через 3 с | — | +| Ввод, который PostgreSQL не может хранить: NUL в `display_name`/`title`/`text`, NUL или не-UTF-8 в Basic username | 400 / 401, не 500 | 400 и 401 (проверка в приложении) | — | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Один Docker-host и по одному экземпляру PostgreSQL и Valkey: отказ host или диска теряет сервис; репликации и бэкапов нет. `appendfsync always` защищает подтверждённые сессии от падения процесса Valkey, но не от потери volume. +- Потеря или `FLUSHALL` Valkey разлогинивает всех: сессии существуют только там. Данные пользователей при этом не теряются, нужен повторный вход. +- Valkey — вторая критичная зависимость: при его отказе защищённый API отвечает 503, хотя PostgreSQL жив. Это осознанная цена хранения сессий вне БД. +- Argon2id тратит 19 MiB на вход или регистрацию; ограничения частоты входа нет (429 в контракте факультативен), всплеск входов нагружает CPU и память API. +- Семантика пагинации при конкурентных вставках описана выше: сообщение, закоммиченное позже сообщения с большим id, может быть пропущено продолжением уже идущего обхода. +- Уникальность id сообщения гарантирована в пределах чата; поиск сообщения по одному id потребует отдельного индекса. +- HTTP без TLS допустим только на локальном стенде (Basic передаёт пароль в открытом виде); TLS — в lab7. API ходит в PostgreSQL пользователем `messenger`, которого создаёт образ (он суперпользователь); отдельная роль без DDL и прав суперпользователя — задача lab7. +- `scripts/restart.cmd` нужен только на Windows: `tests/resilience.py` запускает хук без shell, а Windows не исполняет `.sh` напрямую. +- Дальше: трек 5 по согласованности кэша истории (отдельный экземпляр Valkey под кэш, имя переменной `CACHE_URL_FILE` зарезервировано) либо подготовка к lab3 (реплики API используют общие сессии в Valkey уже сейчас). diff --git a/evidence/bootstrap.txt b/evidence/bootstrap.txt new file mode 100644 index 00000000..5546e177 --- /dev/null +++ b/evidence/bootstrap.txt @@ -0,0 +1,63 @@ +# Bootstrap from empty volumes: make destroy && make secrets up +# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 + +$ make up +Network mtusi2026lab2_default Created +Volume mtusi2026lab2_pgdata Created +Volume mtusi2026lab2_valkeydata Created +Container mtusi2026lab2-valkey-1 Created +Container mtusi2026lab2-postgres-1 Created +Container mtusi-messenger-lab2 Created +Container mtusi2026lab2-postgres-1 Started +Container mtusi2026lab2-valkey-1 Started +Container mtusi2026lab2-postgres-1 Healthy +Container mtusi2026lab2-valkey-1 Healthy +Container mtusi-messenger-lab2 Started +Container mtusi2026lab2-postgres-1 Healthy +Container mtusi2026lab2-valkey-1 Healthy +Container mtusi-messenger-lab2 Healthy + +$ docker compose ps +SERVICE IMAGE STATUS PORTS +api mtusi-messenger:lab2 Up 6 seconds (healthy) 127.0.0.1:8080->8080/tcp +postgres postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 Up 11 seconds (healthy) 5432/tcp +valkey valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11 Up 11 seconds (healthy) 6379/tcp + +$ docker compose logs api | grep -E "schema ready|starting" +{"time":"2026-10-06T13:00:02.672577874Z","level":"INFO","msg":"schema ready","applied_migrations":[1]} +{"time":"2026-10-06T13:00:02.689453662Z","level":"INFO","msg":"starting","env":"lab","addr":"0.0.0.0:8080","shutdown_timeout":"8s","session_ttl":"1h0m0s"} + +$ psql \dt and \di (schema created by the migration) + List of tables + Schema | Name | Type | Owner +--------+------------------+-------+----------- + public | chat_members | table | messenger + public | chats | table | messenger + public | goose_db_version | table | messenger + public | messages | table | messenger + public | users | table | messenger +(5 rows) + + List of indexes + Schema | Name | Type | Owner | Table +--------+----------------------------+-------+-----------+------------------ + public | chat_members_pkey | index | messenger | chat_members + public | chat_members_user_chat_idx | index | messenger | chat_members + public | chats_pkey | index | messenger | chats + public | goose_db_version_pkey | index | messenger | goose_db_version + public | messages_pkey | index | messenger | messages + public | users_pkey | index | messenger | users + public | users_username_key | index | messenger | users +(7 rows) + + +$ valkey-cli CONFIG GET (persistence of the session store) +appendonly yes +appendfsync always +save +maxmemory 268435456 +maxmemory-policy noeviction + +$ second start: migrations are a no-op +{"time":"2026-10-06T13:00:12.891361865Z","level":"INFO","msg":"schema ready","applied_migrations":[]} diff --git a/evidence/concurrency.json b/evidence/concurrency.json new file mode 100644 index 00000000..1ec0437c --- /dev/null +++ b/evidence/concurrency.json @@ -0,0 +1,16 @@ +{ + "base_url": "http://localhost:8080", + "started_at": "2026-10-06T13:01:23.3690872Z", + "clients": 10, + "per_client": 10, + "expected_messages": 100, + "created_201": 100, + "failed_posts": 0, + "listed_messages": 100, + "unique_ids": 100, + "created_but_not_listed": 0, + "history_ordered_by_id": true, + "pages_read": 1, + "elapsed_seconds": 0.1081132, + "passed": true +} diff --git a/evidence/dependency-failures.txt b/evidence/dependency-failures.txt new file mode 100644 index 00000000..749c7691 --- /dev/null +++ b/evidence/dependency-failures.txt @@ -0,0 +1,30 @@ +# Dependency outages on the stand (request deadline 5 s, dial/connect 1 s) +# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 + +baseline GET /users/me: 200 + +$ docker compose stop valkey +{"error":{"code":"unavailable","message":"session store unavailable","request_id":"6dc8e40f8ed60f3d3e32c65da449744e"}} + + -> 503 in 1.223578s +POST /auth/sessions: 503 in 1.235624s +POST /users (PostgreSQL only): 201 +{"error":{"code":"unavailable","message":"valkey is unavailable","request_id":"99a17ea0c0423c3142e004f27e09f739"}} + + -> 503 +api log: +{"time":"2026-10-06T13:01:03.639113985Z","level":"ERROR","msg":"request failed","request_id":"99a17ea0c0423c3142e004f27e09f739","error":"valkey is unavailable","cause":"dial tcp: lookup valkey: i/o timeout"} +after docker compose start valkey, same token: 200 (session survived via AOF) + +$ docker compose stop postgres +{"error":{"code":"unavailable","message":"database unavailable","request_id":"eeb05d0a51641fb655e0f516f3704b67"}} + + -> 503 in 0.230062s +{"error":{"code":"unavailable","message":"postgres is unavailable","request_id":"1df2b60f6d15c835ed4549601fba01d5"}} + + -> 503 +api log: +{"time":"2026-10-06T13:01:10.362999264Z","level":"ERROR","msg":"request failed","request_id":"1df2b60f6d15c835ed4549601fba01d5","error":"postgres is unavailable","cause":"failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout"} +after docker compose start postgres: 200 +GET /health/ready: 200 diff --git a/evidence/expiry.txt b/evidence/expiry.txt new file mode 100644 index 00000000..bf39d1b2 --- /dev/null +++ b/evidence/expiry.txt @@ -0,0 +1,12 @@ +# Session expiry with SESSION_TTL_SECONDS=2 +# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 + +api SESSION_TTL_SECONDS=2 +GET /users/me right after login: 200 +GET /users/me 3 s later: +HTTP/1.1 401 Unauthorized +Www-Authenticate: Bearer realm="messenger" +{"error":{"code":"unauthorized","message":"a valid bearer token is required","request_id":"30f35aed484df88d7c07e7ee17e2a92a"}} + +api restored with SESSION_TTL_SECONDS=3600 diff --git a/evidence/explain-history.txt b/evidence/explain-history.txt new file mode 100644 index 00000000..53f5ca27 --- /dev/null +++ b/evidence/explain-history.txt @@ -0,0 +1,61 @@ +# EXPLAIN (ANALYZE, BUFFERS) of the history query: make explain +# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 + + big_chat_messages | all_messages +-------------------+-------------- + 12000 | 102003 +(1 row) + + +== First page: no cursor, limit 50 (+1 row that tells whether a next page exists) + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=0.42..23.86 rows=51 width=67) (actual time=0.020..0.077 rows=51.00 loops=1) + Buffers: shared hit=51 + -> Index Scan using messages_pkey on messages (cost=0.42..5535.89 rows=12045 width=67) (actual time=0.018..0.072 rows=51.00 loops=1) + Index Cond: ((chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) AND (id > '00000000-0000-0000-0000-000000000000'::uuid)) + Index Searches: 1 + Buffers: shared hit=51 + Planning: + Buffers: shared hit=17 + Planning Time: 0.327 ms + Execution Time: 0.101 ms +(10 rows) + + +== Deep page: cursor after the 10 001st message of the chat + QUERY PLAN +---------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=0.42..185.00 rows=51 width=67) (actual time=0.021..0.033 rows=51.00 loops=1) + Buffers: shared hit=6 + -> Index Scan using messages_pkey on messages (cost=0.42..655.49 rows=181 width=67) (actual time=0.020..0.029 rows=51.00 loops=1) + Index Cond: ((chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) AND (id > '01a1114d-e03e-797e-bdd0-3cb5b8cb3079'::uuid)) + Index Searches: 1 + Buffers: shared hit=6 + Planning Time: 0.093 ms + Execution Time: 0.050 ms +(8 rows) + + +== For contrast, OFFSET pagination of the same page reads every skipped row + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=2837.94..2838.07 rows=51 width=67) (actual time=4.491..4.496 rows=51.00 loops=1) + Buffers: shared hit=495 + -> Sort (cost=2812.94..2843.06 rows=12047 width=67) (actual time=3.874..4.232 rows=10052.00 loops=1) + Sort Key: id + Sort Method: quicksort Memory: 1978kB + Buffers: shared hit=495 + -> Bitmap Heap Scan on messages (cost=585.78..1996.37 rows=12047 width=67) (actual time=0.422..1.796 rows=12000.00 loops=1) + Recheck Cond: (chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) + Heap Blocks: exact=363 + Buffers: shared hit=495 + -> Bitmap Index Scan on messages_pkey (cost=0.00..582.77 rows=12047 width=0) (actual time=0.386..0.386 rows=12000.00 loops=1) + Index Cond: (chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) + Index Searches: 1 + Buffers: shared hit=132 + Planning Time: 0.043 ms + Execution Time: 4.629 ms +(16 rows) + diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..a5372f01 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,17 @@ +# Persistence: make test-persistence ACTION_SCRIPT=scripts/restart.cmd (scripts/restart.sh on Linux) +# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 + +== containers before +/mtusi-messenger-lab2 started=2026-10-06T13:00:12.720916013Z status=running +/mtusi2026lab2-postgres-1 started=2026-10-06T12:59:56.582818179Z status=running +/mtusi2026lab2-valkey-1 started=2026-10-06T12:59:56.579702461Z status=running + +$ make test-persistence ACTION_SCRIPT=scripts/restart.cmd PYTHON=python +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +== containers after (every StartedAt changed: all three processes were restarted, volumes kept) +/mtusi-messenger-lab2 started=2026-10-06T13:00:28.600983618Z status=running +/mtusi2026lab2-postgres-1 started=2026-10-06T13:00:22.758610805Z status=running +/mtusi2026lab2-valkey-1 started=2026-10-06T13:00:22.751213945Z status=running diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..f76f8fd2 --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,9 @@ +# Public checks: make check, make test +# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 + +$ make check PYTHON=python +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. + +$ make test PYTHON=python +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/tests.txt b/evidence/tests.txt new file mode 100644 index 00000000..6a942d96 --- /dev/null +++ b/evidence/tests.txt @@ -0,0 +1,49 @@ +# Automated tests against real PostgreSQL and Valkey (testcontainers): go test -race -v +# date: 2026-10-06T13:02:19Z, commit: 949622e (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1 +# full suite: make unit (go test -race -count=1 ./...), 16 packages ok + +--- PASS: TestRegisterUserShapeAndConflicts (0.31s) +--- PASS: TestConcurrentSendsProduceExactlyUniqueMessages (1.92s) +--- PASS: TestLoginReturnsBearerSession (0.35s) +--- PASS: TestLoginFailuresAreUniformBasicChallenges (0.43s) +--- PASS: TestLoginAcceptsColonAndUnicodePasswords (0.14s) +--- PASS: TestProtectedRoutesAcceptOnlyBearer (0.26s) +--- PASS: TestLogoutRevokesOnlyTheCurrentToken (0.19s) +--- PASS: TestSessionExpiresAfterTTL (1.35s) +--- PASS: TestValkeyOutageAnswers503 (3.98s) +--- PASS: TestPostgresOutageAnswers503 (4.06s) +--- PASS: TestHungPostgresAnswers503WithinTheRequestTimeout (5.45s) +ok messenger/internal/app 29.152s +--- PASS: TestLoginIssuesOpaqueTokenBackedByStoredSession (0.20s) +--- PASS: TestLoginWithBadCredentialsCreatesNoSession (0.30s) +--- PASS: TestAuthenticateResolvesTheOwner (0.27s) +--- PASS: TestLogoutRevokesOnlyThatSession (0.36s) +--- PASS: TestSessionExpiresAfterTTL (1.34s) +ok messenger/internal/auth 13.245s +--- PASS: TestCreateRollsBackWhenMemberInsertFails (0.08s) +--- PASS: TestConcurrentAppendsKeepEveryMessage (0.21s) +ok messenger/internal/chat/store 9.320s +--- PASS: TestKindOfDefaultsToInternal (0.00s) +ok messenger/internal/platform/apperr 1.572s +--- PASS: TestDefaults (0.02s) +--- PASS: TestSecretFilesAreRequiredAndNeverEchoed (0.07s) +--- PASS: TestOverrides (0.02s) +--- PASS: TestInvalidValuesAreReported (0.09s) +ok messenger/internal/platform/config 1.801s +--- PASS: TestUnavailableCauseIsLoggedButNotReturned (0.06s) +--- PASS: TestTimeoutBoundsTheRequestContext (0.05s) +ok messenger/internal/platform/httpx 5.748s +--- PASS: TestMigrateIsRepeatable (0.01s) +--- PASS: TestPoolAppliesLimitsAndTimeouts (0.01s) +--- PASS: TestClassifyMarksUnreachableDatabaseAsUnavailable (0.00s) +--- PASS: TestClassifyRealConnectFailure (0.00s) +ok messenger/internal/platform/postgres 9.271s +--- PASS: TestRegisterStoresUserWithPasswordHash (0.11s) +--- PASS: TestRegisterRejectsInvalidInputAndStoresNothing (0.02s) +--- PASS: TestRegisterReportsDuplicateAsConflict (0.21s) +--- PASS: TestConcurrentRegistrationOfOneUsernameHasOneWinner (0.30s) +--- PASS: TestExistingIDsReturnsOnlyRegistered (0.27s) +--- PASS: TestAuthenticateChecksPassword (0.16s) +--- PASS: TestAuthenticateFailuresLookTheSame (0.39s) +ok messenger/internal/user 10.562s -- 2.54.0 From 7aa86d36bae04237e706e212b46ac1f668b0a6c0 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Wed, 21 Oct 2026 15:08:09 +0300 Subject: [PATCH 20/21] =?UTF-8?q?=D0=A3=D1=87=D0=B5=D1=81=D1=82=D1=8C=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D1=8C=D1=8E:=20=D1=87=D0=B8=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20timestamptz=20=D0=B2=20UTC,=20=D0=B2=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=B8=D1=82=D1=8C=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BB=D0=BE=D0=BD=D0=BE=D0=BA,=20=D0=BD=D0=B5?= =?UTF-8?q?=20=D1=82=D1=80=D0=B5=D0=B1=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=BD=D0=BE=D0=BD=D0=B8=D1=87=D0=B5=D1=81=D0=BA?= =?UTF-8?q?=D1=83=D1=8E=20PHC-=D1=81=D1=82=D1=80=D0=BE=D0=BA=D1=83,=20?= =?UTF-8?q?=D1=83=D1=82=D0=BE=D1=87=D0=BD=D0=B8=D1=82=D1=8C=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BC=D0=B5=D1=80=D1=8B=20=D0=BE=D1=82=D0=BA=D0=B0=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 4 ++-- internal/chat/store/postgres.go | 12 ++++-------- internal/platform/httpx/identity.go | 4 +--- internal/platform/password/password.go | 2 +- internal/platform/postgres/postgres.go | 19 ++++++++++++++++--- internal/platform/postgres/postgres_test.go | 10 ++++++++++ internal/user/store/postgres.go | 9 +++------ 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/REPORT.md b/REPORT.md index 698b6fc5..26584046 100644 --- a/REPORT.md +++ b/REPORT.md @@ -132,8 +132,8 @@ CI. `.github/workflows/app.yml`: unit-тесты с race detector (на runner | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | | Полный рестарт: `docker compose stop` и `up -d --wait` (хук persistence) | те же данные и тот же токен | PASS, все три процесса перезапущены | весь тест с хуком ~15 с; потерь нет | -| `docker compose stop valkey` | 503 на защищённые запросы и вход, ready 503, регистрация работает | 503 за 1.2 с (дозвон ограничен 1 с; DNS Docker для остановленного контейнера отвечает ~4 с), `POST /users` 201, в логе `cause: dial tcp: lookup valkey: i/o timeout` | после `start` тот же токен 200: сессии восстановлены из AOF | -| `docker compose stop postgres` | 503 на операции с данными, ready 503 | 503 за 0.2 с, в логе причина подключения | после `start` 200 без перезапуска API: пул переподключается | +| `docker compose stop valkey` | 503 на защищённые запросы и вход, ready 503, регистрация работает | 503 за 1.2 с (дозвон ограничен 1 с; без ограничения поиск имени остановленного контейнера в DNS Docker не укладывается в дедлайн запроса 5 с), `POST /users` 201, в логе `cause: dial tcp: lookup valkey: i/o timeout` | после `start` тот же токен 200: сессии восстановлены из AOF | +| `docker compose stop postgres` | 503 на операции с данными, ready 503 | первый запрос — 503 за 0.2 с (соединение из пула уже разорвано), следующие — за 1.2 с (новое соединение ждёт поиск имени, ограниченный 1 с); в логе причина подключения | после `start` 200 без перезапуска API: пул переподключается | | PostgreSQL на паузе (`docker pause`, тест) | запрос не висит дольше дедлайна | 503 при дедлайне 300 мс, ready 503 | — | | `SESSION_TTL_SECONDS=2` | сессия истекает | 200 сразу, 401 Bearer через 3 с | — | | Ввод, который PostgreSQL не может хранить: NUL в `display_name`/`title`/`text`, NUL или не-UTF-8 в Basic username | 400 / 401, не 500 | 400 и 401 (проверка в приложении) | — | diff --git a/internal/chat/store/postgres.go b/internal/chat/store/postgres.go index 65affa06..e3634a38 100644 --- a/internal/chat/store/postgres.go +++ b/internal/chat/store/postgres.go @@ -31,10 +31,6 @@ func NewChats(pool *pgxpool.Pool) *Chats { return &Chats{pool: pool} } -// chatWithMembers selects the projection from a chats row aliased c. -const chatWithMembers = `c.id, c.title, c.created_at, - ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)` - func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error { return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error { if _, err := tx.Exec(ctx, @@ -53,7 +49,8 @@ func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) { row := r.pool.QueryRow(ctx, ` - SELECT `+chatWithMembers+` + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id) FROM chats c WHERE c.id = $1 AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, @@ -81,7 +78,8 @@ func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, e func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) { rows, err := r.pool.Query(ctx, ` - SELECT `+chatWithMembers+` + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id) FROM chat_members own JOIN chats c ON c.id = own.chat_id WHERE own.user_id = $1 @@ -106,7 +104,6 @@ func scanChat(row pgx.Row) (chat.ChatWithMembers, error) { if err := row.Scan(&c.ID, &c.Title, &c.CreatedAt, &c.MemberIDs); err != nil { return chat.ChatWithMembers{}, err } - c.CreatedAt = c.CreatedAt.UTC() return c, nil } @@ -147,7 +144,6 @@ func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) { var m chat.Message err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt) - m.CreatedAt = m.CreatedAt.UTC() return m, err }) if err != nil { diff --git a/internal/platform/httpx/identity.go b/internal/platform/httpx/identity.go index 6a4b6fcf..a90fd50c 100644 --- a/internal/platform/httpx/identity.go +++ b/internal/platform/httpx/identity.go @@ -10,9 +10,7 @@ import ( const keyUserID = "messenger.user_id" // SetUserID records the authenticated caller for the handlers behind the -// authentication middleware and for the access log. It lives here rather -// than in the auth package because the access log needs it and the business -// modules must not depend on auth. +// authentication middleware and for the access log. func SetUserID(c *gin.Context, id uuid.UUID) { c.Set(keyUserID, id) } diff --git a/internal/platform/password/password.go b/internal/platform/password/password.go index 2fc2c7a7..7c22fdde 100644 --- a/internal/platform/password/password.go +++ b/internal/platform/password/password.go @@ -54,7 +54,7 @@ func Verify(encoded, plain string) (bool, error) { var memory, time uint32 var parallelism uint8 if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, ¶llelism); err != nil || - memory == 0 || time == 0 || parallelism == 0 || params(memory, time, parallelism) != parts[3] { + memory == 0 || time == 0 || parallelism == 0 { return false, ErrMalformed } salt, err := b64.DecodeString(parts[4]) diff --git a/internal/platform/postgres/postgres.go b/internal/platform/postgres/postgres.go index 45f4eda9..616caf62 100644 --- a/internal/platform/postgres/postgres.go +++ b/internal/platform/postgres/postgres.go @@ -14,7 +14,9 @@ import ( "strconv" "time" + "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/stdlib" "github.com/pressly/goose/v3" @@ -40,9 +42,10 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) { } cfg.MaxConns = maxConns cfg.ConnConfig.ConnectTimeout = connectTimeout - // pgconn applies ConnectTimeout to dialling but resolves the host name - // before that with the caller's context. Docker DNS takes seconds to give - // up on a stopped container, so the lookup gets the same bound. + // Unlike net.Dialer, pgx resolves the host before ConnectTimeout starts. + // Docker DNS takes seconds to give up on a stopped container: unbounded, + // every request would wait out its deadline and log only "context + // deadline exceeded" instead of the failed lookup. cfg.ConnConfig.LookupFunc = func(ctx context.Context, host string) ([]string, error) { ctx, cancel := context.WithTimeout(ctx, connectTimeout) defer cancel() @@ -50,6 +53,16 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) { } cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10) cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10) + // The application keeps every time in UTC; pgx would otherwise scan + // timestamptz into time.Local. + cfg.AfterConnect = func(_ context.Context, conn *pgx.Conn) error { + conn.TypeMap().RegisterType(&pgtype.Type{ + Name: "timestamptz", + OID: pgtype.TimestamptzOID, + Codec: &pgtype.TimestamptzCodec{ScanLocation: time.UTC}, + }) + return nil + } pool, err := pgxpool.NewWithConfig(ctx, cfg) if err != nil { diff --git a/internal/platform/postgres/postgres_test.go b/internal/platform/postgres/postgres_test.go index b3b3a917..c3a14873 100644 --- a/internal/platform/postgres/postgres_test.go +++ b/internal/platform/postgres/postgres_test.go @@ -31,6 +31,16 @@ func TestMain(m *testing.M) { os.Exit(code) } +func TestPoolScansTimestamptzInUTC(t *testing.T) { + var got time.Time + if err := db.Pool.QueryRow(context.Background(), `SELECT now()`).Scan(&got); err != nil { + t.Fatal(err) + } + if got.Location() != time.UTC { + t.Fatalf("timestamptz scanned in %v, want UTC", got.Location()) + } +} + func TestMigrateIsRepeatable(t *testing.T) { ctx := context.Background() applied, err := postgres.Migrate(ctx, db.Pool) diff --git a/internal/user/store/postgres.go b/internal/user/store/postgres.go index df5a5135..d8974847 100644 --- a/internal/user/store/postgres.go +++ b/internal/user/store/postgres.go @@ -16,8 +16,6 @@ import ( "messenger/internal/user" ) -const userColumns = `id, username, display_name, password_hash, created_at` - type Users struct { pool *pgxpool.Pool } @@ -32,7 +30,7 @@ func NewUsers(pool *pgxpool.Pool) *Users { // concurrent registrations of one username cannot both succeed. func (r *Users) Create(ctx context.Context, u user.User) error { _, err := r.pool.Exec(ctx, - `INSERT INTO users (`+userColumns+`) VALUES ($1, $2, $3, $4, $5)`, + `INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)`, u.ID, u.Username, u.DisplayName, u.PasswordHash, u.CreatedAt) if postgres.IsUniqueViolation(err, "users_username_key") { return user.ErrUsernameTaken @@ -44,11 +42,11 @@ func (r *Users) Create(ctx context.Context, u user.User) error { } func (r *Users) GetByID(ctx context.Context, id uuid.UUID) (user.User, error) { - return r.one(ctx, `SELECT `+userColumns+` FROM users WHERE id = $1`, id) + return r.one(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = $1`, id) } func (r *Users) GetByUsername(ctx context.Context, username string) (user.User, error) { - return r.one(ctx, `SELECT `+userColumns+` FROM users WHERE username = $1`, username) + return r.one(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1`, username) } func (r *Users) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) { @@ -72,6 +70,5 @@ func (r *Users) one(ctx context.Context, query string, arg any) (user.User, erro if err != nil { return user.User{}, fmt.Errorf("select user: %w", postgres.Classify(err)) } - u.CreatedAt = u.CreatedAt.UTC() return u, nil } -- 2.54.0 From bb5981f52229dec3b6d0ed5bb29f6fcb52ab6378 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Wed, 21 Oct 2026 15:15:37 +0300 Subject: [PATCH 21/21] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B4=D0=BE=D0=BA=D0=B0=D0=B7=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D1=82=D0=B2=D0=B0=20=D0=BD=D0=B0=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D1=8C=D1=8E,=20=D1=81=D0=B2=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=81=20=D0=BD=D0=B8=D0=BC=D0=B8=20=D0=BE=D1=82?= =?UTF-8?q?=D1=87=D1=91=D1=82=20=D0=B8=20=D0=B2=D1=8B=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B1=D0=B0=D0=B7=D1=8B=20=D0=B2=20?= =?UTF-8?q?=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD=D0=B5=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/pull_request_template.md | 2 +- REPORT.md | 9 ++-- evidence/bootstrap.txt | 16 +++--- evidence/concurrency.json | 4 +- evidence/dependency-failures.txt | 43 ++++++++------- evidence/expiry.txt | 4 +- evidence/explain-history.txt | 38 +++++++------- evidence/persistence.txt | 14 ++--- evidence/smoke.txt | 7 ++- evidence/tests.txt | 89 ++++++++++++++++---------------- 10 files changed, 119 insertions(+), 107 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 610c14a1..b40cdac7 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -4,7 +4,7 @@ Целевая оценка (3/4/5): 4 Трек 5, если заявлен: не заявлен -Что реализовано и что перенесено: перенесён минимум lab1 (формат ошибок, keyset-пагинация, UUIDv7, graceful shutdown). Пользователи, чаты, участники и сообщения хранятся в PostgreSQL 18 (pgx, миграции goose при старте, атомарное создание чата). Регистрация с паролем (Argon2id), вход через Basic, непрозрачный Bearer-токен, сессии в Valkey с AOF `appendfsync always` и абсолютным сроком, logout. Недоступность PostgreSQL или Valkey даёт 503 с причиной в логе. Стенд в compose с secrets, хук перезапуска, EXPLAIN истории. Подробности и исправления базы lab1 — в REPORT.md. +Что реализовано и что перенесено: перенесён минимум lab1 (формат ошибок, keyset-пагинация, UUIDv7, graceful shutdown). Пользователи, чаты, участники и сообщения хранятся в PostgreSQL 18 (pgx, миграции goose при старте, атомарное создание чата). Регистрация с паролем (Argon2id), вход через Basic, непрозрачный Bearer-токен, сессии в Valkey с AOF `appendfsync always` и абсолютным сроком, logout. Недоступность PostgreSQL или Valkey даёт 503 с причиной в логе. Стенд в compose с secrets, хук перезапуска, EXPLAIN истории. Исправления базы lab1: `WithUser` перенесён из `auth` в `httpx` (иначе цикл импортов `auth ↔ user`), `server.Run` не зависит от сборки приложения, `member_ids` всегда в порядке id; in-memory хранилища удалены. Подробности — в REPORT.md. Команды запуска из чистого clone: `make secrets && make up`, затем `make test`, `make test-persistence ACTION_SCRIPT=scripts/restart.sh`, `make unit` (нужен Docker), `make explain`; удаление данных — `make destroy`. Результаты проверок и ссылка на REPORT.md: REPORT.md, раздел «Доказательства критериев»; файлы в `evidence/`. Ограничения / известные ошибки: один host без репликации; потеря Valkey разлогинивает всех; без TLS (lab7); описание семантики пагинации при конкурентных вставках — в REPORT.md. diff --git a/REPORT.md b/REPORT.md index 26584046..c87e5a23 100644 --- a/REPORT.md +++ b/REPORT.md @@ -13,6 +13,7 @@ make secrets # ./secrets: пароли PostgreSQL и Valkey и URL для API; существующие файлы не перезаписываются make up # docker compose up -d --build --wait: postgres и valkey healthy → api применяет миграции → healthy make check PYTHON=python +make validate PYTHON=python # полная проверка OpenAPI; нужен pip install -r requirements-dev.txt make test PYTHON=python make test-persistence ACTION_SCRIPT=scripts/restart.sh PYTHON=python # на Windows: ACTION_SCRIPT=scripts/restart.cmd make unit # собственные тесты; нужен запущенный Docker: тесты поднимают свои PostgreSQL и Valkey @@ -107,7 +108,7 @@ CI. `.github/workflows/app.yml`: unit-тесты с race detector (на runner | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | `make check`, `make test` | оба PASS | evidence/smoke.txt | +| Публичный smoke | `make check`, `make validate`, `make test` | все PASS | evidence/smoke.txt | | Минимум lab1 с переходом на Bearer | `make test` (сценарий lab2), `TestProtectedRoutesAcceptOnlyBearer` и остальные HTTP-тесты `internal/app` | PASS | evidence/smoke.txt, evidence/tests.txt | | Данные и действующие сессии переживают рестарт API и хранилищ без удаления volumes | `make test-persistence ACTION_SCRIPT=scripts/restart.cmd` (`restart.sh`: `docker compose stop` + `up -d --wait`) | PASS тем же токеном; `StartedAt` всех трёх контейнеров изменился | evidence/persistence.txt | | PostgreSQL: пользователи, чаты, участники, сообщения; PK/FK, UNIQUE username; миграции с нуля | `make destroy && make up`, `\dt`, `\di` | `schema ready applied_migrations=[1]`, 4 таблицы и индексы | evidence/bootstrap.txt | @@ -120,7 +121,7 @@ CI. `.github/workflows/app.yml`: unit-тесты с race detector (на runner | Valkey реально используется для сессий с TTL и persistence | `CONFIG GET`; остановка/запуск Valkey с тем же токеном | `appendonly yes`, `appendfsync always`, `noeviction`; после рестарта Valkey токен работает | evidence/bootstrap.txt, evidence/dependency-failures.txt | | Параметризованные запросы, ограниченный пул и таймауты | код `store/postgres.go` (только `$n`), `TestPoolAppliesLimitsAndTimeouts`, `TestTimeoutBoundsTheRequestContext` | `MaxConns=10`, `statement_timeout=5s`, connect 1 с, дедлайн запроса | evidence/tests.txt | | Гонка регистрации одного username | `TestConcurrentRegistrationOfOneUsernameHasOneWinner` | 10 параллельных попыток: ровно 1 успех, 9 × 409, одна строка | evidence/tests.txt | -| Индекс истории обоснован EXPLAIN на ≥10 000 сообщений, без полного scan на страницу | `make explain` (102 000 сообщений, 12 000 в целевом чате, транзакция с ROLLBACK) | `Index Scan using messages_pkey`, `Index Cond (chat_id = … AND id > …)`, 51 строка; 51 буфер на первой странице и 6 на глубокой, 0.05–0.1 мс; OFFSET той же страницы — сортировка 10 052 строк, 495 буферов, 4.6 мс | evidence/explain-history.txt | +| Индекс истории обоснован EXPLAIN на ≥10 000 сообщений, без полного scan на страницу | `make explain` (102 000 сообщений, 12 000 в целевом чате, транзакция с ROLLBACK) | `Index Scan using messages_pkey`, `Index Cond (chat_id = … AND id > …)`, 51 строка; 51 буфер на первой странице и 6 на глубокой, 0.05–0.1 мс; OFFSET той же страницы — сортировка 10 052 строк, 495 буферов, 6.5 мс | evidence/explain-history.txt | | Автопроверки expiry, logout, неверного пароля, недоступности Redis/БД | `TestSessionExpiresAfterTTL`, `TestLogoutRevokesOnlyTheCurrentToken`, `TestLoginFailuresAreUniformBasicChallenges`, `TestValkeyOutageAnswers503`, `TestPostgresOutageAnswers503`, `TestHungPostgresAnswers503WithinTheRequestTimeout` | все PASS; при отказе — 503, никогда 200 или 401 | evidence/tests.txt, evidence/dependency-failures.txt | | Гонки отправки сообщений | `make concurrency` | 100/100 созданы, перечислены и уникальны, порядок по id | evidence/concurrency.json | | Выбранный трек 5 | не заявлен | — | — | @@ -132,8 +133,8 @@ CI. `.github/workflows/app.yml`: unit-тесты с race detector (на runner | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | | Полный рестарт: `docker compose stop` и `up -d --wait` (хук persistence) | те же данные и тот же токен | PASS, все три процесса перезапущены | весь тест с хуком ~15 с; потерь нет | -| `docker compose stop valkey` | 503 на защищённые запросы и вход, ready 503, регистрация работает | 503 за 1.2 с (дозвон ограничен 1 с; без ограничения поиск имени остановленного контейнера в DNS Docker не укладывается в дедлайн запроса 5 с), `POST /users` 201, в логе `cause: dial tcp: lookup valkey: i/o timeout` | после `start` тот же токен 200: сессии восстановлены из AOF | -| `docker compose stop postgres` | 503 на операции с данными, ready 503 | первый запрос — 503 за 0.2 с (соединение из пула уже разорвано), следующие — за 1.2 с (новое соединение ждёт поиск имени, ограниченный 1 с); в логе причина подключения | после `start` 200 без перезапуска API: пул переподключается | +| `docker compose stop valkey` | 503 на защищённые запросы и вход, ready 503, регистрация работает | 503 за 1.0 с (дозвон ограничен 1 с; без ограничения поиск имени остановленного контейнера в DNS Docker не укладывается в дедлайн запроса 5 с), `POST /users` 201, в логе `cause: dial tcp: lookup valkey: i/o timeout` | после `start` тот же токен 200: сессии восстановлены из AOF | +| `docker compose stop postgres` | 503 на операции с данными, ready 503 | первый запрос — 503 за 0.02 с (PostgreSQL при остановке закрыл соединения пула, в логе `57P01`), следующие — за 1.0 с (новое соединение ждёт поиск имени, ограниченный 1 с, в логе `hostname resolving error … i/o timeout`) | после `start` 200 без перезапуска API: пул переподключается | | PostgreSQL на паузе (`docker pause`, тест) | запрос не висит дольше дедлайна | 503 при дедлайне 300 мс, ready 503 | — | | `SESSION_TTL_SECONDS=2` | сессия истекает | 200 сразу, 401 Bearer через 3 с | — | | Ввод, который PostgreSQL не может хранить: NUL в `display_name`/`title`/`text`, NUL или не-UTF-8 в Basic username | 400 / 401, не 500 | 400 и 401 (проверка в приложении) | — | diff --git a/evidence/bootstrap.txt b/evidence/bootstrap.txt index 5546e177..04de2b36 100644 --- a/evidence/bootstrap.txt +++ b/evidence/bootstrap.txt @@ -1,5 +1,5 @@ # Bootstrap from empty volumes: make destroy && make secrets up -# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) # host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 $ make up @@ -9,10 +9,10 @@ Volume mtusi2026lab2_valkeydata Created Container mtusi2026lab2-valkey-1 Created Container mtusi2026lab2-postgres-1 Created Container mtusi-messenger-lab2 Created -Container mtusi2026lab2-postgres-1 Started Container mtusi2026lab2-valkey-1 Started -Container mtusi2026lab2-postgres-1 Healthy +Container mtusi2026lab2-postgres-1 Started Container mtusi2026lab2-valkey-1 Healthy +Container mtusi2026lab2-postgres-1 Healthy Container mtusi-messenger-lab2 Started Container mtusi2026lab2-postgres-1 Healthy Container mtusi2026lab2-valkey-1 Healthy @@ -21,12 +21,12 @@ Container mtusi-messenger-lab2 Healthy $ docker compose ps SERVICE IMAGE STATUS PORTS api mtusi-messenger:lab2 Up 6 seconds (healthy) 127.0.0.1:8080->8080/tcp -postgres postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 Up 11 seconds (healthy) 5432/tcp -valkey valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11 Up 11 seconds (healthy) 6379/tcp +postgres postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 Up 12 seconds (healthy) 5432/tcp +valkey valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11 Up 12 seconds (healthy) 6379/tcp $ docker compose logs api | grep -E "schema ready|starting" -{"time":"2026-10-06T13:00:02.672577874Z","level":"INFO","msg":"schema ready","applied_migrations":[1]} -{"time":"2026-10-06T13:00:02.689453662Z","level":"INFO","msg":"starting","env":"lab","addr":"0.0.0.0:8080","shutdown_timeout":"8s","session_ttl":"1h0m0s"} +{"time":"2026-10-21T12:10:07.644733332Z","level":"INFO","msg":"schema ready","applied_migrations":[1]} +{"time":"2026-10-21T12:10:07.674799548Z","level":"INFO","msg":"starting","env":"lab","addr":"0.0.0.0:8080","shutdown_timeout":"8s","session_ttl":"1h0m0s"} $ psql \dt and \di (schema created by the migration) List of tables @@ -60,4 +60,4 @@ maxmemory 268435456 maxmemory-policy noeviction $ second start: migrations are a no-op -{"time":"2026-10-06T13:00:12.891361865Z","level":"INFO","msg":"schema ready","applied_migrations":[]} +{"time":"2026-10-21T12:10:18.658799511Z","level":"INFO","msg":"schema ready","applied_migrations":[]} diff --git a/evidence/concurrency.json b/evidence/concurrency.json index 1ec0437c..7cf49b4f 100644 --- a/evidence/concurrency.json +++ b/evidence/concurrency.json @@ -1,6 +1,6 @@ { "base_url": "http://localhost:8080", - "started_at": "2026-10-06T13:01:23.3690872Z", + "started_at": "2026-10-21T12:11:30.6825994Z", "clients": 10, "per_client": 10, "expected_messages": 100, @@ -11,6 +11,6 @@ "created_but_not_listed": 0, "history_ordered_by_id": true, "pages_read": 1, - "elapsed_seconds": 0.1081132, + "elapsed_seconds": 0.0793356, "passed": true } diff --git a/evidence/dependency-failures.txt b/evidence/dependency-failures.txt index 749c7691..9f891034 100644 --- a/evidence/dependency-failures.txt +++ b/evidence/dependency-failures.txt @@ -1,30 +1,37 @@ -# Dependency outages on the stand (request deadline 5 s, dial/connect 1 s) -# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# Dependency outages on the stand (request deadline 5 s, dial/connect and host lookup 1 s, readiness budget 1 s) +# date: 2026-10-21T12:14:15Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) # host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 baseline GET /users/me: 200 $ docker compose stop valkey -{"error":{"code":"unavailable","message":"session store unavailable","request_id":"6dc8e40f8ed60f3d3e32c65da449744e"}} +client sees only a safe message: +{"error":{"code":"unavailable","message":"session store unavailable","request_id":"dcf58dc8b038da34fbffaa398be2ae19"}} - -> 503 in 1.223578s -POST /auth/sessions: 503 in 1.235624s -POST /users (PostgreSQL only): 201 -{"error":{"code":"unavailable","message":"valkey is unavailable","request_id":"99a17ea0c0423c3142e004f27e09f739"}} - - -> 503 -api log: -{"time":"2026-10-06T13:01:03.639113985Z","level":"ERROR","msg":"request failed","request_id":"99a17ea0c0423c3142e004f27e09f739","error":"valkey is unavailable","cause":"dial tcp: lookup valkey: i/o timeout"} + GET /users/me: 503 in 1.029945s, request_id 409bc8d095cd5c0aa8bff3a5a6103336 + POST /auth/sessions: 503 in 1.067333s, request_id b80c3cf8d77205be7d145d366e3eba7c + POST /users (PostgreSQL only): 201 + GET /health/ready: 503 +api log, by request_id: + 409bc8d095cd5c0aa8bff3a5a6103336: session store unavailable <- dial tcp: lookup valkey: i/o timeout + b80c3cf8d77205be7d145d366e3eba7c: session store unavailable <- dial tcp: lookup valkey: i/o timeout after docker compose start valkey, same token: 200 (session survived via AOF) $ docker compose stop postgres -{"error":{"code":"unavailable","message":"database unavailable","request_id":"eeb05d0a51641fb655e0f516f3704b67"}} +first request: the pooled connection was closed by the server on shutdown + GET /chats: 503 in 0.022848s, request_id 484577a2014613ccc2cc0af7a110147b +next requests: a new connection waits for the host lookup, bounded by 1 s + GET /chats: 503 in 1.018873s, request_id 68b40ce6917aac8cae5a3a06f53f0d3f + GET /chats: 503 in 1.019858s, request_id d085caa2d09226ceebe5b9aad55cbc2b + GET /chats: 503 in 1.016325s, request_id e646903eb0912bf1c2b75a3dee3c302e +client sees only a safe message: +{"error":{"code":"unavailable","message":"database unavailable","request_id":"0dc9f28ccba988caededb452faec06ac"}} - -> 503 in 0.230062s -{"error":{"code":"unavailable","message":"postgres is unavailable","request_id":"1df2b60f6d15c835ed4549601fba01d5"}} - - -> 503 -api log: -{"time":"2026-10-06T13:01:10.362999264Z","level":"ERROR","msg":"request failed","request_id":"1df2b60f6d15c835ed4549601fba01d5","error":"postgres is unavailable","cause":"failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout"} + GET /health/ready: 503 +api log, by request_id: + 484577a2014613ccc2cc0af7a110147b: select chats: database unavailable <- FATAL: terminating connection due to administrator command (SQLSTATE 57P01) + 68b40ce6917aac8cae5a3a06f53f0d3f: select chats: database unavailable <- failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout + d085caa2d09226ceebe5b9aad55cbc2b: select chats: database unavailable <- failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout + e646903eb0912bf1c2b75a3dee3c302e: select chats: database unavailable <- failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout after docker compose start postgres: 200 GET /health/ready: 200 diff --git a/evidence/expiry.txt b/evidence/expiry.txt index bf39d1b2..520e930d 100644 --- a/evidence/expiry.txt +++ b/evidence/expiry.txt @@ -1,5 +1,5 @@ # Session expiry with SESSION_TTL_SECONDS=2 -# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) # host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 api SESSION_TTL_SECONDS=2 @@ -7,6 +7,6 @@ GET /users/me right after login: 200 GET /users/me 3 s later: HTTP/1.1 401 Unauthorized Www-Authenticate: Bearer realm="messenger" -{"error":{"code":"unauthorized","message":"a valid bearer token is required","request_id":"30f35aed484df88d7c07e7ee17e2a92a"}} +{"error":{"code":"unauthorized","message":"a valid bearer token is required","request_id":"e191d8f08a51fced88868d6fac0d8674"}} api restored with SESSION_TTL_SECONDS=3600 diff --git a/evidence/explain-history.txt b/evidence/explain-history.txt index 53f5ca27..67d358de 100644 --- a/evidence/explain-history.txt +++ b/evidence/explain-history.txt @@ -1,5 +1,5 @@ # EXPLAIN (ANALYZE, BUFFERS) of the history query: make explain -# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) # host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 big_chat_messages | all_messages @@ -11,51 +11,51 @@ == First page: no cursor, limit 50 (+1 row that tells whether a next page exists) QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------ - Limit (cost=0.42..23.86 rows=51 width=67) (actual time=0.020..0.077 rows=51.00 loops=1) + Limit (cost=0.42..23.92 rows=51 width=67) (actual time=0.027..0.060 rows=51.00 loops=1) Buffers: shared hit=51 - -> Index Scan using messages_pkey on messages (cost=0.42..5535.89 rows=12045 width=67) (actual time=0.018..0.072 rows=51.00 loops=1) - Index Cond: ((chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) AND (id > '00000000-0000-0000-0000-000000000000'::uuid)) + -> Index Scan using messages_pkey on messages (cost=0.42..5526.02 rows=11988 width=67) (actual time=0.026..0.056 rows=51.00 loops=1) + Index Cond: ((chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) AND (id > '00000000-0000-0000-0000-000000000000'::uuid)) Index Searches: 1 Buffers: shared hit=51 Planning: Buffers: shared hit=17 - Planning Time: 0.327 ms - Execution Time: 0.101 ms + Planning Time: 0.211 ms + Execution Time: 0.097 ms (10 rows) == Deep page: cursor after the 10 001st message of the chat QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- - Limit (cost=0.42..185.00 rows=51 width=67) (actual time=0.021..0.033 rows=51.00 loops=1) + Limit (cost=0.42..184.60 rows=51 width=67) (actual time=0.018..0.030 rows=51.00 loops=1) Buffers: shared hit=6 - -> Index Scan using messages_pkey on messages (cost=0.42..655.49 rows=181 width=67) (actual time=0.020..0.029 rows=51.00 loops=1) - Index Cond: ((chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) AND (id > '01a1114d-e03e-797e-bdd0-3cb5b8cb3079'::uuid)) + -> Index Scan using messages_pkey on messages (cost=0.42..650.48 rows=180 width=67) (actual time=0.018..0.026 rows=51.00 loops=1) + Index Cond: ((chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) AND (id > '01a15e5f-9557-7527-8fb0-c493cb1229d4'::uuid)) Index Searches: 1 Buffers: shared hit=6 - Planning Time: 0.093 ms - Execution Time: 0.050 ms + Planning Time: 0.071 ms + Execution Time: 0.047 ms (8 rows) == For contrast, OFFSET pagination of the same page reads every skipped row QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------- - Limit (cost=2837.94..2838.07 rows=51 width=67) (actual time=4.491..4.496 rows=51.00 loops=1) + Limit (cost=2832.42..2832.54 rows=51 width=67) (actual time=6.369..6.376 rows=51.00 loops=1) Buffers: shared hit=495 - -> Sort (cost=2812.94..2843.06 rows=12047 width=67) (actual time=3.874..4.232 rows=10052.00 loops=1) + -> Sort (cost=2807.41..2837.39 rows=11989 width=67) (actual time=5.728..6.109 rows=10052.00 loops=1) Sort Key: id Sort Method: quicksort Memory: 1978kB Buffers: shared hit=495 - -> Bitmap Heap Scan on messages (cost=585.78..1996.37 rows=12047 width=67) (actual time=0.422..1.796 rows=12000.00 loops=1) - Recheck Cond: (chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) + -> Bitmap Heap Scan on messages (cost=585.33..1995.19 rows=11989 width=67) (actual time=0.717..2.712 rows=12000.00 loops=1) + Recheck Cond: (chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) Heap Blocks: exact=363 Buffers: shared hit=495 - -> Bitmap Index Scan on messages_pkey (cost=0.00..582.77 rows=12047 width=0) (actual time=0.386..0.386 rows=12000.00 loops=1) - Index Cond: (chat_id = '01a1114d-de5d-7f2e-8176-990d37b9797e'::uuid) + -> Bitmap Index Scan on messages_pkey (cost=0.00..582.34 rows=11989 width=0) (actual time=0.672..0.673 rows=12000.00 loops=1) + Index Cond: (chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) Index Searches: 1 Buffers: shared hit=132 - Planning Time: 0.043 ms - Execution Time: 4.629 ms + Planning Time: 0.041 ms + Execution Time: 6.524 ms (16 rows) diff --git a/evidence/persistence.txt b/evidence/persistence.txt index a5372f01..3c4ca59a 100644 --- a/evidence/persistence.txt +++ b/evidence/persistence.txt @@ -1,17 +1,17 @@ # Persistence: make test-persistence ACTION_SCRIPT=scripts/restart.cmd (scripts/restart.sh on Linux) -# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) # host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 == containers before -/mtusi-messenger-lab2 started=2026-10-06T13:00:12.720916013Z status=running -/mtusi2026lab2-postgres-1 started=2026-10-06T12:59:56.582818179Z status=running -/mtusi2026lab2-valkey-1 started=2026-10-06T12:59:56.579702461Z status=running +/mtusi-messenger-lab2 started=2026-10-21T12:10:18.43319842Z status=running +/mtusi2026lab2-postgres-1 started=2026-10-21T12:10:01.058712538Z status=running +/mtusi2026lab2-valkey-1 started=2026-10-21T12:10:01.069796526Z status=running $ make test-persistence ACTION_SCRIPT=scripts/restart.cmd PYTHON=python Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. == containers after (every StartedAt changed: all three processes were restarted, volumes kept) -/mtusi-messenger-lab2 started=2026-10-06T13:00:28.600983618Z status=running -/mtusi2026lab2-postgres-1 started=2026-10-06T13:00:22.758610805Z status=running -/mtusi2026lab2-valkey-1 started=2026-10-06T13:00:22.751213945Z status=running +/mtusi-messenger-lab2 started=2026-10-21T12:10:34.891259448Z status=running +/mtusi2026lab2-postgres-1 started=2026-10-21T12:10:28.977896441Z status=running +/mtusi2026lab2-valkey-1 started=2026-10-21T12:10:28.995400471Z status=running diff --git a/evidence/smoke.txt b/evidence/smoke.txt index f76f8fd2..08c97ee0 100644 --- a/evidence/smoke.txt +++ b/evidence/smoke.txt @@ -1,9 +1,12 @@ -# Public checks: make check, make test -# date: 2026-10-06T12:59:03Z, commit: 949622e (branch solution/bpi2301-bakulin) +# Public checks: make check, make validate, make test +# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) # host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 $ make check PYTHON=python PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +$ make validate PYTHON=python # after pip install -r requirements-dev.txt +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. + $ make test PYTHON=python PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/tests.txt b/evidence/tests.txt index 6a942d96..e4729f57 100644 --- a/evidence/tests.txt +++ b/evidence/tests.txt @@ -1,49 +1,50 @@ # Automated tests against real PostgreSQL and Valkey (testcontainers): go test -race -v -# date: 2026-10-06T13:02:19Z, commit: 949622e (branch solution/bpi2301-bakulin) -# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1 -# full suite: make unit (go test -race -count=1 ./...), 16 packages ok +# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin) +# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1 +# full suite: make unit (go test -race -count=1 ./...), 16 packages ok, 0 failed ---- PASS: TestRegisterUserShapeAndConflicts (0.31s) ---- PASS: TestConcurrentSendsProduceExactlyUniqueMessages (1.92s) ---- PASS: TestLoginReturnsBearerSession (0.35s) ---- PASS: TestLoginFailuresAreUniformBasicChallenges (0.43s) ---- PASS: TestLoginAcceptsColonAndUnicodePasswords (0.14s) ---- PASS: TestProtectedRoutesAcceptOnlyBearer (0.26s) ---- PASS: TestLogoutRevokesOnlyTheCurrentToken (0.19s) ---- PASS: TestSessionExpiresAfterTTL (1.35s) ---- PASS: TestValkeyOutageAnswers503 (3.98s) ---- PASS: TestPostgresOutageAnswers503 (4.06s) ---- PASS: TestHungPostgresAnswers503WithinTheRequestTimeout (5.45s) -ok messenger/internal/app 29.152s ---- PASS: TestLoginIssuesOpaqueTokenBackedByStoredSession (0.20s) ---- PASS: TestLoginWithBadCredentialsCreatesNoSession (0.30s) ---- PASS: TestAuthenticateResolvesTheOwner (0.27s) ---- PASS: TestLogoutRevokesOnlyThatSession (0.36s) ---- PASS: TestSessionExpiresAfterTTL (1.34s) -ok messenger/internal/auth 13.245s ---- PASS: TestCreateRollsBackWhenMemberInsertFails (0.08s) ---- PASS: TestConcurrentAppendsKeepEveryMessage (0.21s) -ok messenger/internal/chat/store 9.320s ---- PASS: TestKindOfDefaultsToInternal (0.00s) -ok messenger/internal/platform/apperr 1.572s ---- PASS: TestDefaults (0.02s) ---- PASS: TestSecretFilesAreRequiredAndNeverEchoed (0.07s) ---- PASS: TestOverrides (0.02s) ---- PASS: TestInvalidValuesAreReported (0.09s) -ok messenger/internal/platform/config 1.801s ---- PASS: TestUnavailableCauseIsLoggedButNotReturned (0.06s) +--- PASS: TestRegisterUserShapeAndConflicts (0.18s) +--- PASS: TestConcurrentSendsProduceExactlyUniqueMessages (0.97s) +--- PASS: TestLoginReturnsBearerSession (0.14s) +--- PASS: TestLoginFailuresAreUniformBasicChallenges (0.26s) +--- PASS: TestLoginAcceptsColonAndUnicodePasswords (0.08s) +--- PASS: TestProtectedRoutesAcceptOnlyBearer (0.17s) +--- PASS: TestLogoutRevokesOnlyTheCurrentToken (0.16s) +--- PASS: TestSessionExpiresAfterTTL (1.29s) +--- PASS: TestValkeyOutageAnswers503 (3.75s) +--- PASS: TestPostgresOutageAnswers503 (3.83s) +--- PASS: TestHungPostgresAnswers503WithinTheRequestTimeout (5.34s) +ok messenger/internal/app 30.321s +--- PASS: TestLoginIssuesOpaqueTokenBackedByStoredSession (0.11s) +--- PASS: TestLoginWithBadCredentialsCreatesNoSession (0.21s) +--- PASS: TestAuthenticateResolvesTheOwner (0.15s) +--- PASS: TestLogoutRevokesOnlyThatSession (0.14s) +--- PASS: TestSessionExpiresAfterTTL (1.19s) +ok messenger/internal/auth 15.854s +ok messenger/internal/chat 12.655s [no tests to run] +--- PASS: TestConcurrentAppendsKeepEveryMessage (0.17s) +ok messenger/internal/chat/store 13.188s +ok messenger/internal/platform/apperr 1.627s [no tests to run] +ok messenger/internal/platform/config 1.627s [no tests to run] +--- PASS: TestUnavailableCauseIsLoggedButNotReturned (0.03s) --- PASS: TestTimeoutBoundsTheRequestContext (0.05s) -ok messenger/internal/platform/httpx 5.748s ---- PASS: TestMigrateIsRepeatable (0.01s) +ok messenger/internal/platform/httpx 2.942s +ok messenger/internal/platform/ident 1.656s [no tests to run] +ok messenger/internal/platform/pagination 1.778s [no tests to run] +ok messenger/internal/platform/password 1.641s [no tests to run] +--- PASS: TestMigrateIsRepeatable (0.02s) --- PASS: TestPoolAppliesLimitsAndTimeouts (0.01s) --- PASS: TestClassifyMarksUnreachableDatabaseAsUnavailable (0.00s) ---- PASS: TestClassifyRealConnectFailure (0.00s) -ok messenger/internal/platform/postgres 9.271s ---- PASS: TestRegisterStoresUserWithPasswordHash (0.11s) ---- PASS: TestRegisterRejectsInvalidInputAndStoresNothing (0.02s) ---- PASS: TestRegisterReportsDuplicateAsConflict (0.21s) ---- PASS: TestConcurrentRegistrationOfOneUsernameHasOneWinner (0.30s) ---- PASS: TestExistingIDsReturnsOnlyRegistered (0.27s) ---- PASS: TestAuthenticateChecksPassword (0.16s) ---- PASS: TestAuthenticateFailuresLookTheSame (0.39s) -ok messenger/internal/user 10.562s +--- PASS: TestClassifyRealConnectFailure (0.01s) +ok messenger/internal/platform/postgres 9.771s +ok messenger/internal/platform/valkey 2.782s [no tests to run] +ok messenger/internal/server 3.156s [no tests to run] +ok messenger/internal/testenv 2.767s [no tests to run] +--- PASS: TestRegisterStoresUserWithPasswordHash (0.08s) +--- PASS: TestRegisterRejectsInvalidInputAndStoresNothing (0.01s) +--- PASS: TestRegisterReportsDuplicateAsConflict (0.14s) +--- PASS: TestConcurrentRegistrationOfOneUsernameHasOneWinner (0.24s) +--- PASS: TestExistingIDsReturnsOnlyRegistered (0.16s) +--- PASS: TestAuthenticateChecksPassword (0.14s) +--- PASS: TestAuthenticateFailuresLookTheSame (0.32s) +ok messenger/internal/user 12.868s -- 2.54.0