forked from mtusi2026/mtusi2026lab5
Перенёс приложение из lab4, добавил очередь заданий, outbox и worker
This commit is contained in:
+19
@@ -0,0 +1,19 @@
|
||||
FROM golang:1.26-alpine@sha256:c95332c2af86b6d89b91bd0500f4b9529ccbd090a0d1855c6d1ceaa142ae8615 AS build
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY *.go ./
|
||||
COPY migrations ./migrations
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -tags nodynamic -trimpath -ldflags="-s -w" -o /out/messenger .
|
||||
|
||||
FROM alpine:3.21@sha256:ce64758a109eb420d874a118f87920e625e12d3634e03b4a5573fd9f6e5d3507
|
||||
RUN adduser -D -u 10001 app
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/messenger /app/messenger
|
||||
USER 10001
|
||||
EXPOSE 8080
|
||||
ENV APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab
|
||||
STOPSIGNAL SIGTERM
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=3 \
|
||||
CMD wget -qO- http://127.0.0.1:8080/health/ready >/dev/null || exit 1
|
||||
ENTRYPOINT ["/app/messenger"]
|
||||
+530
@@ -0,0 +1,530 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"hash"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const (
|
||||
objectPrefix = "attachments/"
|
||||
stagingPrefix = "staging/"
|
||||
probeHead = 256 << 10
|
||||
tailLen = 12
|
||||
)
|
||||
|
||||
var (
|
||||
pngMagic = []byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}
|
||||
pngTail = []byte{0, 0, 0, 0, 'I', 'E', 'N', 'D', 0xae, 0x42, 0x60, 0x82}
|
||||
jpegMagic = []byte{0xff, 0xd8, 0xff}
|
||||
jpegTail = []byte{0xff, 0xd9}
|
||||
)
|
||||
|
||||
type limits struct {
|
||||
maxBytes int64
|
||||
maxPixels int64
|
||||
uploadSlots int
|
||||
maxAttempts int
|
||||
}
|
||||
|
||||
type attachment struct {
|
||||
ID, ChatID, OwnerID, Filename, ContentType, SHA256, Key, Status string
|
||||
ErrorCode *string
|
||||
Size int64
|
||||
Created, Updated time.Time
|
||||
Variants []variant
|
||||
}
|
||||
|
||||
type variant struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
Size int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
key string
|
||||
}
|
||||
|
||||
func (x *attachment) json() map[string]any {
|
||||
variants := x.Variants
|
||||
if variants == nil {
|
||||
variants = []variant{}
|
||||
}
|
||||
return map[string]any{
|
||||
"id": x.ID,
|
||||
"chat_id": x.ChatID,
|
||||
"owner_id": x.OwnerID,
|
||||
"filename": x.Filename,
|
||||
"content_type": x.ContentType,
|
||||
"size_bytes": x.Size,
|
||||
"sha256": x.SHA256,
|
||||
"status": x.Status,
|
||||
"variants": variants,
|
||||
"error_code": x.ErrorCode,
|
||||
"created_at": formatTime(x.Created),
|
||||
"updated_at": formatTime(x.Updated),
|
||||
}
|
||||
}
|
||||
|
||||
type probe struct {
|
||||
sum hash.Hash
|
||||
size int64
|
||||
head []byte
|
||||
tail []byte
|
||||
}
|
||||
|
||||
func newProbe() *probe {
|
||||
return &probe{sum: sha256.New()}
|
||||
}
|
||||
|
||||
func (p *probe) Write(b []byte) (int, error) {
|
||||
p.sum.Write(b)
|
||||
p.size += int64(len(b))
|
||||
if room := probeHead - len(p.head); room > 0 {
|
||||
if room > len(b) {
|
||||
room = len(b)
|
||||
}
|
||||
p.head = append(p.head, b[:room]...)
|
||||
}
|
||||
p.tail = append(p.tail, b...)
|
||||
if len(p.tail) > tailLen {
|
||||
p.tail = append(p.tail[:0], p.tail[len(p.tail)-tailLen:]...)
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (p *probe) digest() string {
|
||||
return hex.EncodeToString(p.sum.Sum(nil))
|
||||
}
|
||||
|
||||
var (
|
||||
errNotImage = errors.New("not an image")
|
||||
errTooBig = errors.New("too large")
|
||||
)
|
||||
|
||||
func (p *probe) verify(contentType string, maxPixels int64) error {
|
||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(p.head))
|
||||
if err != nil {
|
||||
return errNotImage
|
||||
}
|
||||
var tail []byte
|
||||
switch contentType {
|
||||
case "image/png":
|
||||
if format != "png" {
|
||||
return errNotImage
|
||||
}
|
||||
tail = pngTail
|
||||
case "image/jpeg":
|
||||
if format != "jpeg" {
|
||||
return errNotImage
|
||||
}
|
||||
tail = jpegTail
|
||||
}
|
||||
if !bytes.HasSuffix(p.tail, tail) {
|
||||
return errNotImage
|
||||
}
|
||||
if int64(cfg.Width)*int64(cfg.Height) > maxPixels {
|
||||
return errTooBig
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func magicMatches(contentType string, head []byte) bool {
|
||||
switch contentType {
|
||||
case "image/png":
|
||||
return bytes.HasPrefix(head, pngMagic)
|
||||
case "image/jpeg":
|
||||
return bytes.HasPrefix(head, jpegMagic)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func validFilename(name string) bool {
|
||||
n := utf8.RuneCountInString(name)
|
||||
if n < 1 || n > 255 || !utf8.ValidString(name) {
|
||||
return false
|
||||
}
|
||||
for _, r := range name {
|
||||
if r == '/' || r == '\\' || r == 0 || unicode.IsControl(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type trackedReader struct {
|
||||
r io.Reader
|
||||
err error
|
||||
}
|
||||
|
||||
func (t *trackedReader) Read(b []byte) (int, error) {
|
||||
n, err := t.r.Read(b)
|
||||
if err != nil && err != io.EOF && t.err == nil {
|
||||
t.err = err
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func isTooLarge(err error) bool {
|
||||
var mbe *http.MaxBytesError
|
||||
return errors.As(err, &mbe)
|
||||
}
|
||||
|
||||
func (a *App) dropObject(key string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := a.store.Remove(ctx, key); err != nil {
|
||||
a.log.Warn("object cleanup failed", "key", key, "err", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleUploadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
reqID := reqIDFrom(r)
|
||||
u, _ := a.authUser(w, r)
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
chatID := r.PathValue("chat_id")
|
||||
if !isUUID(chatID) {
|
||||
a.writeErr(w, reqID, 400, "invalid_request", "invalid chat_id")
|
||||
return
|
||||
}
|
||||
ok, err := a.isMember(r.Context(), chatID, u.ID)
|
||||
if err != nil {
|
||||
a.writeErr(w, reqID, 503, "unavailable", "database unavailable")
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
a.writeErr(w, reqID, 404, "not_found", "chat not found")
|
||||
return
|
||||
}
|
||||
select {
|
||||
case a.uploads <- struct{}{}:
|
||||
defer func() { <-a.uploads }()
|
||||
default:
|
||||
w.Header().Set("Retry-After", "1")
|
||||
a.writeErr(w, reqID, 429, "rate_limited", "too many concurrent uploads")
|
||||
return
|
||||
}
|
||||
filename := r.URL.Query().Get("filename")
|
||||
if !validFilename(filename) {
|
||||
a.writeErr(w, reqID, 400, "invalid_request", "invalid filename")
|
||||
return
|
||||
}
|
||||
contentType := strings.ToLower(strings.TrimSpace(strings.Split(r.Header.Get("Content-Type"), ";")[0]))
|
||||
if contentType != "image/png" && contentType != "image/jpeg" {
|
||||
a.writeErr(w, reqID, 415, "unsupported_media_type", "Content-Type must be image/png or image/jpeg")
|
||||
return
|
||||
}
|
||||
if r.ContentLength > a.limits.maxBytes {
|
||||
a.writeErr(w, reqID, 413, "payload_too_large", "file too large")
|
||||
return
|
||||
}
|
||||
body := &trackedReader{r: http.MaxBytesReader(w, r.Body, a.limits.maxBytes)}
|
||||
head := make([]byte, 16)
|
||||
n, _ := io.ReadFull(body, head)
|
||||
if body.err != nil {
|
||||
if isTooLarge(body.err) {
|
||||
a.writeErr(w, reqID, 413, "payload_too_large", "file too large")
|
||||
} else {
|
||||
a.writeErr(w, reqID, 400, "invalid_request", "upload interrupted")
|
||||
}
|
||||
return
|
||||
}
|
||||
head = head[:n]
|
||||
if !magicMatches(contentType, head) {
|
||||
a.writeErr(w, reqID, 415, "unsupported_media_type", "content does not match image type")
|
||||
return
|
||||
}
|
||||
|
||||
id := newUUID()
|
||||
key := stagingPrefix + id
|
||||
pr := newProbe()
|
||||
size := r.ContentLength
|
||||
if size < 0 {
|
||||
size = -1
|
||||
}
|
||||
src := io.TeeReader(io.MultiReader(bytes.NewReader(head), body), pr)
|
||||
putCtx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
if err := a.store.Put(putCtx, key, src, size, contentType); err != nil {
|
||||
a.dropObject(key)
|
||||
switch {
|
||||
case isTooLarge(body.err):
|
||||
a.writeErr(w, reqID, 413, "payload_too_large", "file too large")
|
||||
case body.err != nil:
|
||||
a.writeErr(w, reqID, 400, "invalid_request", "upload interrupted")
|
||||
default:
|
||||
a.log.Error("s3 put failed", "request_id", reqID, "err", err.Error())
|
||||
a.writeErr(w, reqID, 503, "unavailable", "storage unavailable")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := pr.verify(contentType, a.limits.maxPixels); err != nil {
|
||||
a.dropObject(key)
|
||||
if err == errTooBig {
|
||||
a.writeErr(w, reqID, 413, "payload_too_large", "image too large")
|
||||
} else {
|
||||
a.writeErr(w, reqID, 415, "unsupported_media_type", "invalid image")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
now := nowUTC()
|
||||
att := &attachment{
|
||||
ID: id, ChatID: chatID, OwnerID: u.ID, Filename: filename, ContentType: contentType,
|
||||
SHA256: pr.digest(), Size: pr.size, Status: "queued", Created: now, Updated: now,
|
||||
}
|
||||
if err := a.enqueue(r.Context(), att, key); err != nil {
|
||||
a.dropObject(key)
|
||||
a.log.Error("enqueue failed", "request_id", reqID, "err", err.Error())
|
||||
a.writeErr(w, reqID, 503, "unavailable", "database unavailable")
|
||||
return
|
||||
}
|
||||
if a.fault == "after_commit" {
|
||||
a.log.Error("fault injected", "point", "after_commit", "attachment_id", att.ID)
|
||||
os.Exit(3)
|
||||
}
|
||||
a.writeJSON(w, 202, att.json())
|
||||
}
|
||||
|
||||
func (a *App) enqueue(ctx context.Context, att *attachment, key string) error {
|
||||
ctx, cancel := dbCtx(ctx)
|
||||
defer cancel()
|
||||
tx, err := a.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err := tx.Exec(ctx, `
|
||||
insert into attachments(id, chat_id, owner_id, filename, content_type, size_bytes, sha256, staging_key, status, created_at, updated_at)
|
||||
values($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, 'queued', $9, $9)`,
|
||||
att.ID, att.ChatID, att.OwnerID, att.Filename, att.ContentType, att.Size, att.SHA256, key, att.Created); err != nil {
|
||||
return err
|
||||
}
|
||||
jobID := newUUID()
|
||||
if _, err := tx.Exec(ctx, `
|
||||
insert into jobs(id, attachment_id, pipeline_version, status, attempt, max_attempts, next_attempt_at, created_at, updated_at)
|
||||
values($1::uuid, $2::uuid, $3, 'queued', 1, $4, $5, $5, $5)`,
|
||||
jobID, att.ID, pipelineVersion, a.limits.maxAttempts, att.Created); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
insert into outbox(job_id, attempt, available_at, created_at) values($1::uuid, 1, $2, $2)`, jobID, att.Created); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (a *App) loadAttachment(ctx context.Context, id, userID string) (*attachment, error) {
|
||||
var x attachment
|
||||
err := a.db.QueryRow(ctx, `
|
||||
select a.id::text, a.chat_id::text, a.owner_id::text, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, coalesce(a.object_key, ''), a.status, a.error_code, a.created_at, a.updated_at
|
||||
from attachments a
|
||||
join chat_members m on m.chat_id = a.chat_id and m.user_id = $2::uuid
|
||||
where a.id = $1::uuid`, id, userID).
|
||||
Scan(&x.ID, &x.ChatID, &x.OwnerID, &x.Filename, &x.ContentType, &x.Size, &x.SHA256, &x.Key, &x.Status, &x.ErrorCode, &x.Created, &x.Updated)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if x.Status != "ready" {
|
||||
return &x, nil
|
||||
}
|
||||
rows, err := a.db.Query(ctx, `
|
||||
select name, content_type, size_bytes, width, height, sha256, object_key
|
||||
from attachment_variants where attachment_id = $1::uuid and pipeline_version = $2
|
||||
order by array_position(array['thumbnail', 'crop', 'optimized'], name)`, id, pipelineVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x.Variants, err = pgx.CollectRows(rows, func(row pgx.CollectableRow) (variant, error) {
|
||||
var v variant
|
||||
return v, row.Scan(&v.Name, &v.ContentType, &v.Size, &v.Width, &v.Height, &v.SHA256, &v.key)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &x, nil
|
||||
}
|
||||
|
||||
func (a *App) attachmentFor(w http.ResponseWriter, r *http.Request) *attachment {
|
||||
reqID := reqIDFrom(r)
|
||||
u, _ := a.authUser(w, r)
|
||||
if u == nil {
|
||||
return nil
|
||||
}
|
||||
id := r.PathValue("attachment_id")
|
||||
if !isUUID(id) {
|
||||
a.writeErr(w, reqID, 400, "invalid_request", "invalid attachment_id")
|
||||
return nil
|
||||
}
|
||||
ctx, cancel := dbCtx(r.Context())
|
||||
defer cancel()
|
||||
att, err := a.loadAttachment(ctx, id, u.ID)
|
||||
if err != nil {
|
||||
a.writeErr(w, reqID, 503, "unavailable", "database unavailable")
|
||||
return nil
|
||||
}
|
||||
if att == nil {
|
||||
a.writeErr(w, reqID, 404, "not_found", "attachment not found")
|
||||
return nil
|
||||
}
|
||||
return att
|
||||
}
|
||||
|
||||
func (a *App) handleGetAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
if att := a.attachmentFor(w, r); att != nil {
|
||||
a.writeJSON(w, 200, att.json())
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleDownloadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
reqID := reqIDFrom(r)
|
||||
name := r.URL.Query().Get("variant")
|
||||
if name == "" {
|
||||
name = "original"
|
||||
}
|
||||
if name != "original" && name != "thumbnail" && name != "crop" && name != "optimized" {
|
||||
a.writeErr(w, reqID, 400, "invalid_request", "invalid variant")
|
||||
return
|
||||
}
|
||||
att := a.attachmentFor(w, r)
|
||||
if att == nil {
|
||||
return
|
||||
}
|
||||
if att.Status != "ready" {
|
||||
a.writeErr(w, reqID, 409, "conflict", "attachment is not ready")
|
||||
return
|
||||
}
|
||||
key, contentType := att.Key, att.ContentType
|
||||
if name != "original" {
|
||||
key = ""
|
||||
for _, v := range att.Variants {
|
||||
if v.Name == name {
|
||||
key, contentType = v.key, v.ContentType
|
||||
}
|
||||
}
|
||||
if key == "" {
|
||||
a.writeErr(w, reqID, 404, "not_found", "variant not found")
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||
defer cancel()
|
||||
obj, info, err := a.store.Get(ctx, key)
|
||||
if err != nil {
|
||||
if isNoSuchKey(err) {
|
||||
a.writeErr(w, reqID, 404, "not_found", "attachment not found")
|
||||
return
|
||||
}
|
||||
a.log.Error("s3 get failed", "request_id", reqID, "err", err.Error())
|
||||
a.writeErr(w, reqID, 503, "unavailable", "storage unavailable")
|
||||
return
|
||||
}
|
||||
defer obj.Close()
|
||||
first := make([]byte, 32<<10)
|
||||
n, err := io.ReadFull(obj, first)
|
||||
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||
a.log.Error("s3 read failed", "request_id", reqID, "err", err.Error())
|
||||
a.writeErr(w, reqID, 503, "unavailable", "storage unavailable")
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(info.Size, 10))
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(200)
|
||||
_, _ = w.Write(first[:n])
|
||||
_, _ = io.Copy(w, obj)
|
||||
}
|
||||
|
||||
var keepQueries = map[string]string{
|
||||
objectPrefix: `
|
||||
select k from unnest($1::text[]) k
|
||||
where exists(select 1 from attachments where object_key = k)
|
||||
or exists(select 1 from attachment_variants where object_key = k)`,
|
||||
stagingPrefix: `
|
||||
select k from unnest($1::text[]) k
|
||||
where exists(select 1 from attachments where staging_key = k and (status in ('queued', 'processing')
|
||||
or (status = 'failed' and updated_at > now() - make_interval(secs => $2))))`,
|
||||
}
|
||||
|
||||
func cleanupOrphans(ctx context.Context, db *pgxpool.Pool, store *Store, grace, retention time.Duration, log *slog.Logger) (int, error) {
|
||||
cutoff := time.Now().Add(-grace)
|
||||
removed := 0
|
||||
flush := func(prefix string, keys []string) error {
|
||||
if len(keys) == 0 {
|
||||
return nil
|
||||
}
|
||||
args := []any{keys}
|
||||
if prefix == stagingPrefix {
|
||||
args = append(args, retention.Seconds())
|
||||
}
|
||||
rows, err := db.Query(ctx, keepQueries[prefix], args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kept, err := pgx.CollectRows(rows, pgx.RowTo[string])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
known := map[string]bool{}
|
||||
for _, k := range kept {
|
||||
known[k] = true
|
||||
}
|
||||
for _, k := range keys {
|
||||
if known[k] {
|
||||
continue
|
||||
}
|
||||
if err := store.Remove(ctx, k); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("orphan object removed", "key", k)
|
||||
removed++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, prefix := range []string{objectPrefix, stagingPrefix} {
|
||||
var batch []string
|
||||
for obj := range store.List(ctx, prefix) {
|
||||
if obj.Err != nil {
|
||||
return removed, obj.Err
|
||||
}
|
||||
if obj.LastModified.After(cutoff) {
|
||||
continue
|
||||
}
|
||||
batch = append(batch, obj.Key)
|
||||
if len(batch) == 500 {
|
||||
if err := flush(prefix, batch); err != nil {
|
||||
return removed, err
|
||||
}
|
||||
batch = batch[:0]
|
||||
}
|
||||
}
|
||||
if err := flush(prefix, batch); err != nil {
|
||||
return removed, err
|
||||
}
|
||||
}
|
||||
return removed, nil
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonTime = 2
|
||||
argonMemory = 19456
|
||||
argonThreads = 1
|
||||
argonKeyLen = 32
|
||||
argonSaltLen = 16
|
||||
)
|
||||
|
||||
func hashPassword(password string) (string, error) {
|
||||
salt := make([]byte, argonSaltLen)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen)
|
||||
b64Salt := base64.RawStdEncoding.EncodeToString(salt)
|
||||
b64Hash := base64.RawStdEncoding.EncodeToString(hash)
|
||||
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, argonMemory, argonTime, argonThreads, b64Salt, b64Hash), nil
|
||||
}
|
||||
|
||||
func checkPassword(encoded, password string) (bool, error) {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[1] != "argon2id" {
|
||||
return false, errors.New("bad hash")
|
||||
}
|
||||
var version int
|
||||
if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil {
|
||||
return false, err
|
||||
}
|
||||
var memory, timeCost, threads int
|
||||
for _, p := range strings.Split(parts[3], ",") {
|
||||
kv := strings.SplitN(p, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
return false, errors.New("bad params")
|
||||
}
|
||||
n, err := strconv.Atoi(kv[1])
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
switch kv[0] {
|
||||
case "m":
|
||||
memory = n
|
||||
case "t":
|
||||
timeCost = n
|
||||
case "p":
|
||||
threads = n
|
||||
}
|
||||
}
|
||||
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
want, err := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
got := argon2.IDKey([]byte(password), salt, uint32(timeCost), uint32(memory), uint8(threads), uint32(len(want)))
|
||||
return subtle.ConstantTimeCompare(got, want) == 1, nil
|
||||
}
|
||||
|
||||
func newToken() (string, []byte, string, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", nil, "", err
|
||||
}
|
||||
token := base64.RawURLEncoding.EncodeToString(raw)
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
hash := make([]byte, len(sum))
|
||||
copy(hash, sum[:])
|
||||
return token, hash, hex.EncodeToString(hash), nil
|
||||
}
|
||||
|
||||
func tokenHash(token string) ([]byte, string) {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
hash := make([]byte, len(sum))
|
||||
copy(hash, sum[:])
|
||||
return hash, hex.EncodeToString(hash)
|
||||
}
|
||||
|
||||
func parseBasic(header string) (username, password string, ok bool) {
|
||||
const prefix = "Basic "
|
||||
if !strings.HasPrefix(header, prefix) {
|
||||
return "", "", false
|
||||
}
|
||||
decoded, err := base64.StdEncoding.DecodeString(strings.TrimSpace(header[len(prefix):]))
|
||||
if err != nil {
|
||||
return "", "", false
|
||||
}
|
||||
s := string(decoded)
|
||||
i := strings.IndexByte(s, ':')
|
||||
if i < 0 {
|
||||
return "", "", false
|
||||
}
|
||||
username = s[:i]
|
||||
password = s[i+1:]
|
||||
return username, password, true
|
||||
}
|
||||
|
||||
func parseBearer(header string) (string, bool) {
|
||||
const prefix = "Bearer "
|
||||
if !strings.HasPrefix(header, prefix) {
|
||||
return "", false
|
||||
}
|
||||
tok := strings.TrimSpace(header[len(prefix):])
|
||||
if tok == "" {
|
||||
return "", false
|
||||
}
|
||||
return tok, true
|
||||
}
|
||||
|
||||
func validPassword(p string) bool {
|
||||
n := utf8.RuneCountInString(p)
|
||||
return n >= 12 && n <= 128
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
jobStream = "attachments.jobs"
|
||||
jobGroup = "workers"
|
||||
jobType = "attachment.process.v1"
|
||||
pipelineVersion = 1
|
||||
)
|
||||
|
||||
type jobMessage struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID string `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type Broker struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func openBroker() (*Broker, error) {
|
||||
raw, err := readSecret("BROKER_URL", "BROKER_URL_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opt, err := redis.ParseURL(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opt.DialTimeout = 2 * time.Second
|
||||
opt.ReadTimeout = 3 * time.Second
|
||||
opt.WriteTimeout = 3 * time.Second
|
||||
opt.MaxRetries = 0
|
||||
return &Broker{rdb: redis.NewClient(opt)}, nil
|
||||
}
|
||||
|
||||
func (b *Broker) ensureGroup(ctx context.Context) error {
|
||||
err := b.rdb.XGroupCreateMkStream(ctx, jobStream, jobGroup, "0").Err()
|
||||
if err != nil && !strings.Contains(err.Error(), "BUSYGROUP") {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Broker) publish(ctx context.Context, m jobMessage) error {
|
||||
body, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.rdb.XAdd(ctx, &redis.XAddArgs{Stream: jobStream, Values: map[string]any{"job": string(body)}}).Err()
|
||||
}
|
||||
|
||||
func relayOnce(ctx context.Context, db *pgxpool.Pool, b *Broker) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
rows, err := tx.Query(ctx, `
|
||||
select o.id, o.job_id::text, o.attempt, j.attachment_id::text, j.pipeline_version, j.created_at
|
||||
from outbox o
|
||||
join jobs j on j.id = o.job_id
|
||||
where o.published_at is null and o.available_at <= now()
|
||||
order by o.id
|
||||
limit 50
|
||||
for update of o skip locked`)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var ids []int64
|
||||
var msgs []jobMessage
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var created time.Time
|
||||
m := jobMessage{SchemaVersion: 1, Type: jobType}
|
||||
if err := rows.Scan(&id, &m.JobID, &m.Attempt, &m.AttachmentID, &m.PipelineVersion, &created); err != nil {
|
||||
rows.Close()
|
||||
return 0, err
|
||||
}
|
||||
m.CreatedAt = formatTime(created)
|
||||
ids = append(ids, id)
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
if err := b.ensureGroup(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, m := range msgs {
|
||||
if err := b.publish(ctx, m); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `update outbox set published_at = now() where id = any($1)`, ids); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(ids), tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func runRelay(ctx context.Context, db *pgxpool.Pool, b *Broker, log *slog.Logger) {
|
||||
for ctx.Err() == nil {
|
||||
n, err := relayOnce(ctx, db, b)
|
||||
if err != nil {
|
||||
log.Warn("outbox relay failed", "err", err.Error())
|
||||
time.Sleep(2 * time.Second)
|
||||
continue
|
||||
}
|
||||
if n > 0 {
|
||||
log.Info("outbox published", "count", n)
|
||||
continue
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type cachedSession struct {
|
||||
UserID string `json:"user_id"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
rdb *redis.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func openCache(log *slog.Logger) *Cache {
|
||||
url, err := readSecret("CACHE_URL", "CACHE_URL_FILE")
|
||||
if err != nil {
|
||||
log.Warn("cache disabled", "err", err.Error())
|
||||
return &Cache{log: log}
|
||||
}
|
||||
opt, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
log.Warn("cache parse failed", "err", err.Error())
|
||||
return &Cache{log: log}
|
||||
}
|
||||
opt.DialTimeout = 200 * time.Millisecond
|
||||
opt.ReadTimeout = 200 * time.Millisecond
|
||||
opt.WriteTimeout = 200 * time.Millisecond
|
||||
opt.PoolTimeout = 200 * time.Millisecond
|
||||
rdb := redis.NewClient(opt)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
log.Warn("cache ping failed, will bypass", "err", err.Error())
|
||||
}
|
||||
return &Cache{rdb: rdb, log: log}
|
||||
}
|
||||
|
||||
func (c *Cache) Close() {
|
||||
if c.rdb != nil {
|
||||
_ = c.rdb.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func sessKey(hashHex string) string {
|
||||
return "sess:" + hashHex
|
||||
}
|
||||
|
||||
func (c *Cache) GetSession(ctx context.Context, hashHex string) (*cachedSession, bool) {
|
||||
if c.rdb == nil {
|
||||
return nil, false
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
||||
defer cancel()
|
||||
val, err := c.rdb.Get(ctx, sessKey(hashHex)).Result()
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
var s cachedSession
|
||||
if err := json.Unmarshal([]byte(val), &s); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
if time.Now().UTC().After(s.ExpiresAt) {
|
||||
return nil, false
|
||||
}
|
||||
return &s, true
|
||||
}
|
||||
|
||||
func (c *Cache) SetSession(ctx context.Context, hashHex string, userID string, expiresAt time.Time) {
|
||||
if c.rdb == nil {
|
||||
return
|
||||
}
|
||||
ttl := time.Until(expiresAt)
|
||||
if ttl <= 0 {
|
||||
return
|
||||
}
|
||||
body, err := json.Marshal(cachedSession{UserID: userID, ExpiresAt: expiresAt.UTC()})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := c.rdb.Set(ctx, sessKey(hashHex), body, ttl).Err(); err != nil {
|
||||
c.log.Warn("cache set failed", "err", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) DelSession(ctx context.Context, hashHex string) {
|
||||
if c.rdb == nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := c.rdb.Del(ctx, sessKey(hashHex)).Err(); err != nil {
|
||||
c.log.Warn("cache del failed", "err", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func readSecret(envKey, fileKey string) (string, error) {
|
||||
if v := os.Getenv(envKey); v != "" {
|
||||
return strings.TrimSpace(v), nil
|
||||
}
|
||||
path := os.Getenv(fileKey)
|
||||
if path == "" {
|
||||
return "", fmt.Errorf("%s or %s required", envKey, fileKey)
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(b)), nil
|
||||
}
|
||||
|
||||
func openDB(ctx context.Context) (*pgxpool.Pool, error) {
|
||||
dsn, err := readSecret("DATABASE_URL", "DATABASE_URL_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.MaxConns = 10
|
||||
cfg.MinConns = 2
|
||||
cfg.MaxConnLifetime = 5 * time.Minute
|
||||
cfg.ConnConfig.ConnectTimeout = 3 * time.Second
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := pool.Ping(pingCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, err
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func dbCtx(parent context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(parent, 3*time.Second)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
module mtusi-messenger
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/gen2brain/webp v0.6.4
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/minio/minio-go/v7 v7.4.1
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
|
||||
golang.org/x/crypto v0.58.0
|
||||
golang.org/x/image v0.47.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/ebitengine/purego v0.10.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/tinylib/msgp v1.6.4 // indirect
|
||||
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.24.0 // indirect
|
||||
golang.org/x/sys v0.49.0 // indirect
|
||||
golang.org/x/text v0.43.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
)
|
||||
@@ -0,0 +1,85 @@
|
||||
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/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/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/gen2brain/webp v0.6.4 h1:SUDdmxADOAiPQ+5ylNmuHhuYf2dOi0KgKZHL5vpVCNU=
|
||||
github.com/gen2brain/webp v0.6.4/go.mod h1:iGWMaCSw7t3I/Cv9llzEKmpnR36S8lS8VL/ZVjxU0JE=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
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/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.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
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/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w=
|
||||
github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
||||
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
|
||||
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
|
||||
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.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.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/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
|
||||
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
||||
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
||||
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
|
||||
golang.org/x/image v0.47.0 h1:za/8gVz++PPZqmZJBNSISICN5Rrj5zixP/8K++fgmqo=
|
||||
golang.org/x/image v0.47.0/go.mod h1:IkN5o4A7vq55vXN8FWjjnhg9Rs2GOtffqsSi9mWAKYE=
|
||||
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
|
||||
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
|
||||
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
|
||||
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
|
||||
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
+1092
File diff suppressed because it is too large
Load Diff
+176
@@ -0,0 +1,176 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
"image/draw"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
|
||||
"github.com/gen2brain/webp"
|
||||
"github.com/rwcarlsen/goexif/exif"
|
||||
xdraw "golang.org/x/image/draw"
|
||||
)
|
||||
|
||||
const (
|
||||
jpegQuality = 85
|
||||
webpQuality = 80
|
||||
webpMethod = 4
|
||||
)
|
||||
|
||||
var errInvalidImage = errors.New("invalid image")
|
||||
|
||||
type variantSpec struct {
|
||||
name, contentType, ext string
|
||||
box int
|
||||
crop bool
|
||||
}
|
||||
|
||||
var variantSpecs = []variantSpec{
|
||||
{"thumbnail", "image/jpeg", "jpg", 256, false},
|
||||
{"crop", "image/jpeg", "jpg", 128, true},
|
||||
{"optimized", "image/webp", "webp", 1280, false},
|
||||
}
|
||||
|
||||
type rendered struct {
|
||||
spec variantSpec
|
||||
data []byte
|
||||
width, height int
|
||||
sha256 string
|
||||
}
|
||||
|
||||
func renderVariants(ctx context.Context, data []byte, maxPixels int64) ([]rendered, error) {
|
||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil || cfg.Width < 1 || cfg.Height < 1 {
|
||||
return nil, errInvalidImage
|
||||
}
|
||||
if int64(cfg.Width)*int64(cfg.Height) > maxPixels {
|
||||
return nil, errInvalidImage
|
||||
}
|
||||
src, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, errInvalidImage
|
||||
}
|
||||
orient := 1
|
||||
if format == "jpeg" {
|
||||
orient = orientation(data)
|
||||
}
|
||||
src = shrink(src, 1280)
|
||||
var out []rendered
|
||||
for _, spec := range variantSpecs {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
img := render(src, cfg.Width, cfg.Height, orient, spec)
|
||||
var buf bytes.Buffer
|
||||
if spec.contentType == "image/webp" {
|
||||
err = webp.Encode(&buf, img, webp.Options{Quality: webpQuality, Method: webpMethod})
|
||||
} else {
|
||||
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: jpegQuality})
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sum := sha256.Sum256(buf.Bytes())
|
||||
b := img.Bounds()
|
||||
out = append(out, rendered{spec: spec, data: buf.Bytes(), width: b.Dx(), height: b.Dy(), sha256: hex.EncodeToString(sum[:])})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func orientation(data []byte) int {
|
||||
x, err := exif.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return 1
|
||||
}
|
||||
tag, err := x.Get(exif.Orientation)
|
||||
if err != nil {
|
||||
return 1
|
||||
}
|
||||
v, err := tag.Int(0)
|
||||
if err != nil || v < 1 || v > 8 {
|
||||
return 1
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func fit(w, h, box int) (int, int) {
|
||||
if w <= box && h <= box {
|
||||
return w, h
|
||||
}
|
||||
if w >= h {
|
||||
return box, max(1, h*box/w)
|
||||
}
|
||||
return max(1, w*box/h), box
|
||||
}
|
||||
|
||||
func shrink(src image.Image, limit int) image.Image {
|
||||
for {
|
||||
b := src.Bounds()
|
||||
if max(b.Dx(), b.Dy())/2 < limit {
|
||||
return src
|
||||
}
|
||||
dst := image.NewRGBA(image.Rect(0, 0, max(1, b.Dx()/2), max(1, b.Dy()/2)))
|
||||
xdraw.ApproxBiLinear.Scale(dst, dst.Bounds(), src, b, xdraw.Src, nil)
|
||||
src = dst
|
||||
}
|
||||
}
|
||||
|
||||
func render(src image.Image, sw, sh, orient int, spec variantSpec) *image.RGBA {
|
||||
b := src.Bounds()
|
||||
rect := b
|
||||
var tw, th int
|
||||
if spec.crop {
|
||||
side := min(b.Dx(), b.Dy())
|
||||
x0, y0 := b.Min.X+(b.Dx()-side)/2, b.Min.Y+(b.Dy()-side)/2
|
||||
rect = image.Rect(x0, y0, x0+side, y0+side)
|
||||
tw, th = spec.box, spec.box
|
||||
} else if orient >= 5 {
|
||||
th, tw = fit(sh, sw, spec.box)
|
||||
} else {
|
||||
tw, th = fit(sw, sh, spec.box)
|
||||
}
|
||||
dst := image.NewRGBA(image.Rect(0, 0, tw, th))
|
||||
draw.Draw(dst, dst.Bounds(), image.White, image.Point{}, draw.Src)
|
||||
xdraw.CatmullRom.Scale(dst, dst.Bounds(), src, rect, xdraw.Over, nil)
|
||||
return orientImage(dst, orient)
|
||||
}
|
||||
|
||||
func orientImage(img *image.RGBA, o int) *image.RGBA {
|
||||
if o <= 1 {
|
||||
return img
|
||||
}
|
||||
w, h := img.Bounds().Dx(), img.Bounds().Dy()
|
||||
dw, dh := w, h
|
||||
if o >= 5 {
|
||||
dw, dh = h, w
|
||||
}
|
||||
dst := image.NewRGBA(image.Rect(0, 0, dw, dh))
|
||||
for y := 0; y < h; y++ {
|
||||
for x := 0; x < w; x++ {
|
||||
var nx, ny int
|
||||
switch o {
|
||||
case 2:
|
||||
nx, ny = w-1-x, y
|
||||
case 3:
|
||||
nx, ny = w-1-x, h-1-y
|
||||
case 4:
|
||||
nx, ny = x, h-1-y
|
||||
case 5:
|
||||
nx, ny = y, x
|
||||
case 6:
|
||||
nx, ny = h-1-y, x
|
||||
case 7:
|
||||
nx, ny = h-1-y, w-1-x
|
||||
case 8:
|
||||
nx, ny = y, w-1-x
|
||||
}
|
||||
copy(dst.Pix[dst.PixOffset(nx, ny):dst.PixOffset(nx, ny)+4], img.Pix[img.PixOffset(x, y):img.PixOffset(x, y)+4])
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
|
||||
host := envOr("APP_HOST", "0.0.0.0")
|
||||
port := envOr("APP_PORT", "8080")
|
||||
shutdownSec, _ := strconv.Atoi(envOr("SHUTDOWN_TIMEOUT_SECONDS", "5"))
|
||||
if shutdownSec < 1 {
|
||||
shutdownSec = 5
|
||||
}
|
||||
ttlSec, _ := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600"))
|
||||
if ttlSec < 1 {
|
||||
ttlSec = 3600
|
||||
}
|
||||
|
||||
idemSec, _ := strconv.Atoi(envOr("IDEMPOTENCY_TTL_SECONDS", "86400"))
|
||||
if idemSec < 1 {
|
||||
idemSec = 86400
|
||||
}
|
||||
drainSec, _ := strconv.Atoi(envOr("DRAIN_SECONDS", "3"))
|
||||
|
||||
ctx := context.Background()
|
||||
if len(os.Args) > 1 && os.Args[1] == "s3-init" {
|
||||
if err := s3Init(ctx); err != nil {
|
||||
log.Error("s3 init failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("bucket ready")
|
||||
return
|
||||
}
|
||||
db, err := openDB(ctx)
|
||||
if err != nil {
|
||||
log.Error("database open failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
||||
if err := migrate(ctx, db); err != nil {
|
||||
log.Error("migrate failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("migrations applied")
|
||||
return
|
||||
}
|
||||
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
log.Error("s3 open failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 && (os.Args[1] == "cleanup" || os.Args[1] == "janitor") {
|
||||
runCleanup(ctx, db, store, log, os.Args[1] == "janitor")
|
||||
return
|
||||
}
|
||||
|
||||
maxBytes, _ := strconv.ParseInt(envOr("MAX_UPLOAD_BYTES", "10485760"), 10, 64)
|
||||
maxPixels, _ := strconv.ParseInt(envOr("MAX_IMAGE_PIXELS", "40000000"), 10, 64)
|
||||
maxAttempts := envInt("JOB_MAX_ATTEMPTS", 5)
|
||||
|
||||
if len(os.Args) > 1 && (os.Args[1] == "replay" || os.Args[1] == "failed") {
|
||||
out := json.NewEncoder(os.Stdout)
|
||||
if os.Args[1] == "failed" {
|
||||
items, err := listFailed(ctx, db)
|
||||
if err != nil {
|
||||
log.Error("failed jobs query failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
for _, item := range items {
|
||||
_ = out.Encode(item)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) != 3 || !isUUID(os.Args[2]) {
|
||||
log.Error("usage: replay <attachment_id>")
|
||||
os.Exit(2)
|
||||
}
|
||||
result, err := replayJob(ctx, db, os.Args[2], maxAttempts)
|
||||
if err != nil {
|
||||
log.Error("replay failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
_ = out.Encode(map[string]string{"attachment_id": os.Args[2], "result": result})
|
||||
return
|
||||
}
|
||||
|
||||
broker, err := openBroker()
|
||||
if err != nil {
|
||||
log.Error("broker open failed", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 && os.Args[1] == "worker" {
|
||||
timeout := time.Duration(envInt("JOB_TIMEOUT_SECONDS", 60)) * time.Second
|
||||
owner, _ := os.Hostname()
|
||||
w := &Worker{
|
||||
db: db, store: store, broker: broker, log: log,
|
||||
owner: owner + "-" + newUUID()[:8],
|
||||
timeout: timeout,
|
||||
lease: timeout + 15*time.Second,
|
||||
backoffBase: time.Duration(envInt("JOB_BACKOFF_BASE_MS", 1000)) * time.Millisecond,
|
||||
backoffMax: time.Duration(envInt("JOB_BACKOFF_MAX_MS", 30000)) * time.Millisecond,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
fault: os.Getenv("FAULT_POINT"),
|
||||
}
|
||||
runWorker(db, store, broker, log, w, envInt("WORKER_CONCURRENCY", 2))
|
||||
return
|
||||
}
|
||||
|
||||
cache := openCache(log)
|
||||
defer cache.Close()
|
||||
|
||||
slots := envInt("UPLOAD_CONCURRENCY", 10)
|
||||
app := NewApp(db, cache, log, time.Duration(ttlSec)*time.Second, time.Duration(idemSec)*time.Second, store,
|
||||
limits{maxBytes: maxBytes, maxPixels: maxPixels, uploadSlots: slots, maxAttempts: maxAttempts})
|
||||
relayCtx, stopRelay := context.WithCancel(ctx)
|
||||
defer stopRelay()
|
||||
go runRelay(relayCtx, db, broker, log)
|
||||
|
||||
addr := net.JoinHostPort(host, port)
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: app.Routes(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Info("listening", "addr", addr, "env", envOr("APP_ENV", "lab"), "session_ttl_s", ttlSec)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Error("server error", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
||||
sig := <-stop
|
||||
log.Info("shutdown started", "signal", sig.String())
|
||||
app.SetReady(false)
|
||||
time.Sleep(time.Duration(drainSec) * time.Second)
|
||||
|
||||
shCtx, cancel := context.WithTimeout(context.Background(), time.Duration(shutdownSec)*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shCtx); err != nil {
|
||||
log.Error("shutdown error", "err", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("shutdown finished")
|
||||
}
|
||||
|
||||
func runCleanup(ctx context.Context, db *pgxpool.Pool, store *Store, log *slog.Logger, loop bool) {
|
||||
graceSec, _ := strconv.Atoi(envOr("CLEANUP_GRACE_SECONDS", "600"))
|
||||
intervalSec, _ := strconv.Atoi(envOr("CLEANUP_INTERVAL_SECONDS", "300"))
|
||||
retention := time.Duration(envInt("STAGING_RETENTION_SECONDS", 86400)) * time.Second
|
||||
for {
|
||||
removed, err := cleanupOrphans(ctx, db, store, time.Duration(graceSec)*time.Second, retention, log)
|
||||
if err != nil {
|
||||
log.Error("cleanup failed", "err", err.Error())
|
||||
if !loop {
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
log.Info("cleanup finished", "removed", removed)
|
||||
}
|
||||
if !loop {
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Duration(intervalSec) * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func envInt(key string, def int) int {
|
||||
v, err := strconv.Atoi(os.Getenv(key))
|
||||
if err != nil || v < 1 {
|
||||
return def
|
||||
}
|
||||
return v
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFS embed.FS
|
||||
|
||||
func migrate(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
conn, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Release()
|
||||
if _, err := conn.Exec(ctx, `select pg_advisory_lock(727001)`); err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Exec(context.Background(), `select pg_advisory_unlock(727001)`)
|
||||
|
||||
_, err = conn.Exec(ctx, `
|
||||
create table if not exists schema_migrations (
|
||||
version text primary key,
|
||||
applied_at timestamptz not null
|
||||
)`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("schema_migrations: %w", err)
|
||||
}
|
||||
|
||||
entries, err := migrationFS.ReadDir("migrations")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var names []string
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() && strings.HasSuffix(e.Name(), ".sql") {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
for _, name := range names {
|
||||
var exists bool
|
||||
err := conn.QueryRow(ctx, `select exists(select 1 from schema_migrations where version=$1)`, name).Scan(&exists)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
|
||||
body, err := migrationFS.ReadFile("migrations/" + name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := conn.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, string(body)); err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return fmt.Errorf("migration %s: %w", name, err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `insert into schema_migrations(version, applied_at) values($1, now())`, name); err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
create table users (
|
||||
id uuid primary key,
|
||||
username text not null 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)
|
||||
);
|
||||
|
||||
create index chat_members_user_idx on chat_members (user_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
|
||||
);
|
||||
|
||||
create index messages_history_idx on messages (chat_id, created_at, id);
|
||||
|
||||
create table sessions (
|
||||
token_hash bytea primary key,
|
||||
user_id uuid not null references users(id) on delete cascade,
|
||||
created_at timestamptz not null,
|
||||
expires_at timestamptz not null
|
||||
);
|
||||
@@ -0,0 +1,10 @@
|
||||
create table idempotency_keys (
|
||||
user_id uuid not null references users(id) on delete cascade,
|
||||
method text not null,
|
||||
path text not null,
|
||||
key text not null,
|
||||
body_hash text not null,
|
||||
response jsonb not null,
|
||||
created_at timestamptz not null,
|
||||
primary key (user_id, method, path, key)
|
||||
);
|
||||
@@ -0,0 +1,22 @@
|
||||
create table attachments (
|
||||
id uuid primary key,
|
||||
chat_id uuid not null references chats(id),
|
||||
owner_id uuid not null references users(id),
|
||||
filename text not null,
|
||||
content_type text not null,
|
||||
size_bytes bigint not null,
|
||||
sha256 text not null,
|
||||
object_key text not null unique,
|
||||
created_at timestamptz not null
|
||||
);
|
||||
|
||||
create index attachments_chat_idx on attachments (chat_id);
|
||||
|
||||
create table message_attachments (
|
||||
message_id uuid not null references messages(id),
|
||||
attachment_id uuid not null references attachments(id),
|
||||
position int not null,
|
||||
primary key (message_id, attachment_id)
|
||||
);
|
||||
|
||||
create index message_attachments_attachment_idx on message_attachments (attachment_id);
|
||||
@@ -0,0 +1,60 @@
|
||||
alter table attachments
|
||||
add column status text not null default 'ready',
|
||||
add column error_code text,
|
||||
add column staging_key text,
|
||||
add column updated_at timestamptz;
|
||||
|
||||
update attachments set updated_at = created_at;
|
||||
|
||||
alter table attachments
|
||||
alter column status drop default,
|
||||
alter column updated_at set not null,
|
||||
alter column object_key drop not null;
|
||||
|
||||
create table attachment_variants (
|
||||
attachment_id uuid not null references attachments(id),
|
||||
pipeline_version int not null,
|
||||
name text not null,
|
||||
object_key text not null unique,
|
||||
content_type text not null,
|
||||
size_bytes bigint not null,
|
||||
width int not null,
|
||||
height int not null,
|
||||
sha256 text not null,
|
||||
primary key (attachment_id, pipeline_version, name)
|
||||
);
|
||||
|
||||
create table jobs (
|
||||
id uuid primary key,
|
||||
attachment_id uuid not null unique references attachments(id),
|
||||
pipeline_version int not null,
|
||||
status text not null,
|
||||
attempt int not null,
|
||||
max_attempts int not null,
|
||||
next_attempt_at timestamptz not null,
|
||||
lease_owner text,
|
||||
lease_until timestamptz,
|
||||
last_error text,
|
||||
created_at timestamptz not null,
|
||||
updated_at timestamptz not null
|
||||
);
|
||||
|
||||
create index jobs_lease_idx on jobs (lease_until) where status = 'processing';
|
||||
|
||||
create table outbox (
|
||||
id bigserial primary key,
|
||||
job_id uuid not null references jobs(id),
|
||||
attempt int not null,
|
||||
available_at timestamptz not null,
|
||||
published_at timestamptz,
|
||||
created_at timestamptz not null
|
||||
);
|
||||
|
||||
create index outbox_pending_idx on outbox (available_at) where published_at is null;
|
||||
|
||||
create table job_replays (
|
||||
id bigserial primary key,
|
||||
job_id uuid not null references jobs(id),
|
||||
previous_error text,
|
||||
replayed_at timestamptz not null
|
||||
);
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import "time"
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID string
|
||||
Title string
|
||||
MemberIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
ChatID string
|
||||
SenderID string
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
TokenHash []byte
|
||||
UserID string
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func openStore() (*Store, error) {
|
||||
endpoint, err := url.Parse(envOr("S3_ENDPOINT", "http://s3:9000"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyID, err := readSecret("S3_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret, err := readSecret("S3_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := &http.Transport{
|
||||
DialContext: (&net.Dialer{Timeout: 2 * time.Second}).DialContext,
|
||||
ResponseHeaderTimeout: 10 * time.Second,
|
||||
MaxIdleConnsPerHost: 16,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
}
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(keyID, secret, ""),
|
||||
Secure: endpoint.Scheme == "https",
|
||||
Region: envOr("S3_REGION", "us-east-1"),
|
||||
Transport: transport,
|
||||
MaxRetries: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{client: client, bucket: envOr("S3_BUCKET", "messenger-attachments")}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
PartSize: 5 << 20,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, key string) (*minio.Object, minio.ObjectInfo, error) {
|
||||
obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, minio.ObjectInfo{}, err
|
||||
}
|
||||
info, err := obj.Stat()
|
||||
if err != nil {
|
||||
obj.Close()
|
||||
return nil, minio.ObjectInfo{}, err
|
||||
}
|
||||
return obj, info, nil
|
||||
}
|
||||
|
||||
func (s *Store) Remove(ctx context.Context, key string) error {
|
||||
return s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, prefix string) <-chan minio.ObjectInfo {
|
||||
return s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true})
|
||||
}
|
||||
|
||||
func isNoSuchKey(err error) bool {
|
||||
return minio.ToErrorResponse(err).Code == "NoSuchKey"
|
||||
}
|
||||
|
||||
func s3Init(ctx context.Context) error {
|
||||
s, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
|
||||
defer cancel()
|
||||
for {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err == nil {
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
err = s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: envOr("S3_REGION", "us-east-1")})
|
||||
if err == nil || minio.ToErrorResponse(err).Code == "BucketAlreadyOwnedByYou" {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return err
|
||||
case <-time.After(2 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,448 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"math/rand/v2"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var errSource = errors.New("staging object missing or corrupted")
|
||||
|
||||
type Worker struct {
|
||||
db *pgxpool.Pool
|
||||
store *Store
|
||||
broker *Broker
|
||||
log *slog.Logger
|
||||
owner string
|
||||
timeout time.Duration
|
||||
lease time.Duration
|
||||
backoffBase time.Duration
|
||||
backoffMax time.Duration
|
||||
maxBytes int64
|
||||
maxPixels int64
|
||||
fault string
|
||||
}
|
||||
|
||||
type claimedJob struct {
|
||||
id, attachmentID, owner string
|
||||
attempt int
|
||||
}
|
||||
|
||||
type source struct {
|
||||
stagingKey, contentType, sha256 string
|
||||
size int64
|
||||
}
|
||||
|
||||
func finalKey(attachmentID string) string {
|
||||
return objectPrefix + attachmentID + "/original"
|
||||
}
|
||||
|
||||
func variantKey(attachmentID string, spec variantSpec) string {
|
||||
return fmt.Sprintf("%s%s/v%d/%s.%s", objectPrefix, attachmentID, pipelineVersion, spec.name, spec.ext)
|
||||
}
|
||||
|
||||
func (w *Worker) inject(point string) {
|
||||
if w.fault == point {
|
||||
w.log.Error("fault injected", "point", point)
|
||||
os.Exit(3)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) backoff(attempt int) time.Duration {
|
||||
d := w.backoffBase << (attempt - 1)
|
||||
if d <= 0 || d > w.backoffMax {
|
||||
d = w.backoffMax
|
||||
}
|
||||
return d/2 + rand.N(d/2+1)
|
||||
}
|
||||
|
||||
func (w *Worker) claim(ctx context.Context, jobID string) (*claimedJob, error) {
|
||||
tx, err := w.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
job := &claimedJob{id: jobID, owner: w.owner}
|
||||
err = tx.QueryRow(ctx, `
|
||||
update jobs set status = 'processing', lease_owner = $2,
|
||||
lease_until = now() + make_interval(secs => $3), updated_at = now()
|
||||
where id = $1::uuid and status = 'queued' and next_attempt_at <= now()
|
||||
returning attachment_id::text, attempt`, jobID, w.owner, w.lease.Seconds()).Scan(&job.attachmentID, &job.attempt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `update attachments set status = 'processing', updated_at = now() where id = $1::uuid`, job.attachmentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return job, tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (w *Worker) fetch(ctx context.Context, src source) ([]byte, error) {
|
||||
obj, info, err := w.store.Get(ctx, src.stagingKey)
|
||||
if err != nil {
|
||||
if isNoSuchKey(err) {
|
||||
return nil, errSource
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer obj.Close()
|
||||
if info.Size != src.size || info.Size > w.maxBytes {
|
||||
return nil, errSource
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(obj, w.maxBytes+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sum := sha256.Sum256(data)
|
||||
if int64(len(data)) != src.size || hex.EncodeToString(sum[:]) != src.sha256 {
|
||||
return nil, errSource
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (w *Worker) execute(ctx context.Context, job *claimedJob) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, w.timeout)
|
||||
defer cancel()
|
||||
var src source
|
||||
err := w.db.QueryRow(ctx, `
|
||||
select coalesce(staging_key, ''), content_type, sha256, size_bytes from attachments where id = $1::uuid`,
|
||||
job.attachmentID).Scan(&src.stagingKey, &src.contentType, &src.sha256, &src.size)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := w.fetch(ctx, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
variants, err := renderVariants(ctx, data, w.maxPixels)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
original := finalKey(job.attachmentID)
|
||||
if err := w.store.Put(ctx, original, bytes.NewReader(data), int64(len(data)), src.contentType); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range variants {
|
||||
if err := w.store.Put(ctx, variantKey(job.attachmentID, v.spec), bytes.NewReader(v.data), int64(len(v.data)), v.spec.contentType); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
w.inject("after_store")
|
||||
|
||||
tx, err := w.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
tag, err := tx.Exec(ctx, `
|
||||
update jobs set status = 'ready', lease_owner = null, lease_until = null, last_error = null, updated_at = now()
|
||||
where id = $1::uuid and status = 'processing' and lease_owner = $2 and attempt = $3`, job.id, job.owner, job.attempt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
w.log.Warn("lease lost, result discarded", "job_id", job.id, "attempt", job.attempt)
|
||||
return nil
|
||||
}
|
||||
for _, v := range variants {
|
||||
_, err := tx.Exec(ctx, `
|
||||
insert into attachment_variants(attachment_id, pipeline_version, name, object_key, content_type, size_bytes, width, height, sha256)
|
||||
values($1::uuid, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
on conflict (attachment_id, pipeline_version, name) do update
|
||||
set object_key = excluded.object_key, content_type = excluded.content_type, size_bytes = excluded.size_bytes,
|
||||
width = excluded.width, height = excluded.height, sha256 = excluded.sha256`,
|
||||
job.attachmentID, pipelineVersion, v.spec.name, variantKey(job.attachmentID, v.spec), v.spec.contentType,
|
||||
len(v.data), v.width, v.height, v.sha256)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
update attachments set status = 'ready', error_code = null, object_key = $2, updated_at = now()
|
||||
where id = $1::uuid`, job.attachmentID, original); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
w.log.Info("job ready", "job_id", job.id, "attachment_id", job.attachmentID, "attempt", job.attempt)
|
||||
w.inject("before_ack")
|
||||
if err := w.store.Remove(ctx, src.stagingKey); err != nil {
|
||||
w.log.Warn("staging cleanup deferred to janitor", "attachment_id", job.attachmentID, "err", err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worker) settle(ctx context.Context, job *claimedJob, permanent bool, code, reason string) error {
|
||||
tx, err := w.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var maxAttempts int
|
||||
if err := tx.QueryRow(ctx, `select max_attempts from jobs where id = $1::uuid`, job.id).Scan(&maxAttempts); err != nil {
|
||||
return err
|
||||
}
|
||||
fence := ` where id = $1::uuid and status = 'processing' and lease_owner = $2 and attempt = $3`
|
||||
if !permanent && job.attempt < maxAttempts {
|
||||
delay := w.backoff(job.attempt)
|
||||
tag, err := tx.Exec(ctx, `
|
||||
update jobs set status = 'queued', attempt = attempt + 1, next_attempt_at = now() + make_interval(secs => $4),
|
||||
lease_owner = null, lease_until = null, last_error = $5, updated_at = now()`+fence,
|
||||
job.id, job.owner, job.attempt, delay.Seconds(), reason)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
insert into outbox(job_id, attempt, available_at, created_at)
|
||||
values($1::uuid, $2, now() + make_interval(secs => $3), now())`, job.id, job.attempt+1, delay.Seconds()); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `update attachments set status = 'queued', updated_at = now() where id = $1::uuid`, job.attachmentID); err != nil {
|
||||
return err
|
||||
}
|
||||
w.log.Warn("job retry scheduled", "job_id", job.id, "attempt", job.attempt, "next_attempt", job.attempt+1,
|
||||
"delay_ms", delay.Milliseconds(), "reason", reason)
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
if !permanent {
|
||||
code = "retry_exhausted"
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
update jobs set status = 'failed', lease_owner = null, lease_until = null, last_error = $4, updated_at = now()`+fence,
|
||||
job.id, job.owner, job.attempt, reason)
|
||||
if err != nil || tag.RowsAffected() == 0 {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
update attachments set status = 'failed', error_code = $2, updated_at = now() where id = $1::uuid`, job.attachmentID, code); err != nil {
|
||||
return err
|
||||
}
|
||||
w.log.Error("job failed", "job_id", job.id, "attempt", job.attempt, "error_code", code, "reason", reason)
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (w *Worker) handle(ctx context.Context, msg redis.XMessage) bool {
|
||||
var m jobMessage
|
||||
raw, _ := msg.Values["job"].(string)
|
||||
if err := json.Unmarshal([]byte(raw), &m); err != nil || m.SchemaVersion != 1 || m.Type != jobType || !isUUID(m.JobID) {
|
||||
w.log.Warn("invalid job message dropped", "stream_id", msg.ID)
|
||||
return true
|
||||
}
|
||||
job, err := w.claim(ctx, m.JobID)
|
||||
if err != nil {
|
||||
w.log.Warn("claim failed", "job_id", m.JobID, "err", err.Error())
|
||||
return false
|
||||
}
|
||||
if job == nil {
|
||||
var status string
|
||||
var attempt int
|
||||
_ = w.db.QueryRow(ctx, `select status, attempt from jobs where id = $1::uuid`, m.JobID).Scan(&status, &attempt)
|
||||
w.log.Info("job delivery skipped", "job_id", m.JobID, "message_attempt", m.Attempt, "status", status, "attempt", attempt)
|
||||
return true
|
||||
}
|
||||
err = w.execute(ctx, job)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
permanent, code := false, "processing_failed"
|
||||
switch {
|
||||
case errors.Is(err, errInvalidImage):
|
||||
permanent, code = true, "invalid_image"
|
||||
case errors.Is(err, errSource):
|
||||
permanent = true
|
||||
}
|
||||
if err := w.settle(ctx, job, permanent, code, err.Error()); err != nil {
|
||||
w.log.Warn("settle failed", "job_id", job.id, "err", err.Error())
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *Worker) read(ctx context.Context, consumer, id string, block time.Duration) ([]redis.XMessage, error) {
|
||||
res, err := w.broker.rdb.XReadGroup(ctx, &redis.XReadGroupArgs{
|
||||
Group: jobGroup, Consumer: consumer, Streams: []string{jobStream, id}, Count: 1, Block: block,
|
||||
}).Result()
|
||||
if len(res) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
return res[0].Messages, err
|
||||
}
|
||||
|
||||
func (w *Worker) consume(ctx context.Context, consumer string) {
|
||||
pending := true
|
||||
nextClaim := time.Now().Add(w.lease)
|
||||
for ctx.Err() == nil {
|
||||
var msgs []redis.XMessage
|
||||
var err error
|
||||
switch {
|
||||
case pending:
|
||||
msgs, err = w.read(ctx, consumer, "0", -1)
|
||||
pending = len(msgs) > 0
|
||||
case time.Now().After(nextClaim):
|
||||
msgs, _, err = w.broker.rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{
|
||||
Stream: jobStream, Group: jobGroup, Consumer: consumer, MinIdle: w.lease, Start: "0-0", Count: 1,
|
||||
}).Result()
|
||||
nextClaim = time.Now().Add(15 * time.Second)
|
||||
default:
|
||||
msgs, err = w.read(ctx, consumer, ">", 5*time.Second)
|
||||
}
|
||||
if err != nil && !errors.Is(err, redis.Nil) {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "NOGROUP") {
|
||||
_ = w.broker.ensureGroup(ctx)
|
||||
}
|
||||
w.log.Warn("broker read failed", "consumer", consumer, "err", err.Error())
|
||||
time.Sleep(2 * time.Second)
|
||||
continue
|
||||
}
|
||||
for _, msg := range msgs {
|
||||
if !w.handle(context.WithoutCancel(ctx), msg) {
|
||||
pending = false
|
||||
continue
|
||||
}
|
||||
if err := w.broker.rdb.XAck(context.WithoutCancel(ctx), jobStream, jobGroup, msg.ID).Err(); err != nil {
|
||||
w.log.Warn("ack failed", "stream_id", msg.ID, "err", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) reap(ctx context.Context) error {
|
||||
rows, err := w.db.Query(ctx, `
|
||||
select id::text, attachment_id::text, attempt, coalesce(lease_owner, '')
|
||||
from jobs where status = 'processing' and lease_until < now() limit 50`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jobs, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (*claimedJob, error) {
|
||||
j := &claimedJob{}
|
||||
return j, row.Scan(&j.id, &j.attachmentID, &j.attempt, &j.owner)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, j := range jobs {
|
||||
w.log.Warn("lease expired", "job_id", j.id, "attempt", j.attempt)
|
||||
if err := w.settle(ctx, j, false, "", "lease expired"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runWorker(db *pgxpool.Pool, store *Store, broker *Broker, log *slog.Logger, w *Worker, concurrency int) {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
for {
|
||||
err := broker.ensureGroup(ctx)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
log.Warn("broker not ready", "err", err.Error())
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(2 * time.Second):
|
||||
}
|
||||
}
|
||||
host, _ := os.Hostname()
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() {
|
||||
for ctx.Err() == nil {
|
||||
if err := w.reap(ctx); err != nil && ctx.Err() == nil {
|
||||
log.Warn("lease reaper failed", "err", err.Error())
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(5 * time.Second):
|
||||
}
|
||||
}
|
||||
})
|
||||
for i := range concurrency {
|
||||
consumer := fmt.Sprintf("%s-%d", host, i)
|
||||
wg.Go(func() { w.consume(ctx, consumer) })
|
||||
}
|
||||
log.Info("worker started", "owner", w.owner, "concurrency", concurrency, "lease_s", w.lease.Seconds())
|
||||
<-ctx.Done()
|
||||
log.Info("worker stopping")
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func replayJob(ctx context.Context, db *pgxpool.Pool, attachmentID string, maxAttempts int) (string, error) {
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
var jobID, status string
|
||||
var attempt int
|
||||
var lastErr *string
|
||||
err = tx.QueryRow(ctx, `select id::text, status, attempt, last_error from jobs where attachment_id = $1::uuid for update`,
|
||||
attachmentID).Scan(&jobID, &status, &attempt, &lastErr)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "not_found", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if status != "failed" {
|
||||
return "skipped_" + status, nil
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `insert into job_replays(job_id, previous_error, replayed_at) values($1::uuid, $2, now())`, jobID, lastErr); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
update jobs set status = 'queued', attempt = attempt + 1, max_attempts = attempt + $2, next_attempt_at = now(),
|
||||
last_error = null, updated_at = now()
|
||||
where id = $1::uuid`, jobID, maxAttempts); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `insert into outbox(job_id, attempt, available_at, created_at) values($1::uuid, $2, now(), now())`, jobID, attempt+1); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `update attachments set status = 'queued', error_code = null, updated_at = now() where id = $1::uuid`, attachmentID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "queued", tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func listFailed(ctx context.Context, db *pgxpool.Pool) ([]map[string]any, error) {
|
||||
rows, err := db.Query(ctx, `
|
||||
select j.attachment_id::text, j.id::text, j.attempt, coalesce(a.error_code, ''), coalesce(j.last_error, ''), j.updated_at
|
||||
from jobs j join attachments a on a.id = j.attachment_id
|
||||
where j.status = 'failed' order by j.updated_at`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pgx.CollectRows(rows, func(row pgx.CollectableRow) (map[string]any, error) {
|
||||
var att, job, code, reason string
|
||||
var attempt int
|
||||
var updated time.Time
|
||||
err := row.Scan(&att, &job, &attempt, &code, &reason, &updated)
|
||||
return map[string]any{"attachment_id": att, "job_id": job, "attempt": attempt, "error_code": code,
|
||||
"last_error": reason, "failed_at": formatTime(updated)}, err
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user