Files
mtusi2026lab6/internal/broker/redisstream/envelope.go
T
2026-11-01 14:46:14 +03:00

148 lines
4.3 KiB
Go

package redisstream
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"time"
"unicode/utf8"
"github.com/google/uuid"
)
const (
SchemaVersion = 1
JobType = "attachment.process.v1"
)
var (
errInvalidEnvelope = errors.New("invalid job envelope")
traceparentPattern = regexp.MustCompile(`^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$`)
uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
)
type Envelope struct {
SchemaVersion int `json:"schema_version"`
JobID uuid.UUID `json:"job_id"`
Type string `json:"type"`
AttachmentID uuid.UUID `json:"attachment_id"`
PipelineVersion int `json:"pipeline_version"`
Attempt int `json:"attempt"`
CreatedAt time.Time `json:"created_at"`
Traceparent string `json:"traceparent,omitempty"`
Tracestate string `json:"tracestate,omitempty"`
}
func (e Envelope) Validate() error {
switch {
case e.SchemaVersion != SchemaVersion:
return fmt.Errorf("%w: unsupported schema_version", errInvalidEnvelope)
case e.Type != JobType:
return fmt.Errorf("%w: unsupported type", errInvalidEnvelope)
case e.JobID == uuid.Nil:
return fmt.Errorf("%w: job_id is required", errInvalidEnvelope)
case e.AttachmentID == uuid.Nil:
return fmt.Errorf("%w: attachment_id is required", errInvalidEnvelope)
case e.PipelineVersion < 1:
return fmt.Errorf("%w: pipeline_version must be positive", errInvalidEnvelope)
case e.Attempt < 1:
return fmt.Errorf("%w: attempt must be positive", errInvalidEnvelope)
case e.CreatedAt.IsZero():
return fmt.Errorf("%w: created_at is required", errInvalidEnvelope)
case e.Traceparent != "" && !traceparentPattern.MatchString(e.Traceparent):
return fmt.Errorf("%w: invalid traceparent", errInvalidEnvelope)
case utf8.RuneCountInString(e.Tracestate) > 512:
return fmt.Errorf("%w: tracestate is too long", errInvalidEnvelope)
default:
return nil
}
}
func EncodeEnvelope(envelope Envelope) ([]byte, error) {
if err := envelope.Validate(); err != nil {
return nil, err
}
payload, err := json.Marshal(envelope)
if err != nil {
return nil, fmt.Errorf("encode job envelope: %w", err)
}
return payload, nil
}
func DecodeEnvelope(payload []byte) (Envelope, error) {
var wire envelopeJSON
decoder := json.NewDecoder(bytes.NewReader(payload))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&wire); err != nil {
return Envelope{}, fmt.Errorf("%w: decode: %v", errInvalidEnvelope, err)
}
if err := ensureJSONEnd(decoder); err != nil {
return Envelope{}, err
}
jobID, err := parseUUID(wire.JobID, "job_id")
if err != nil {
return Envelope{}, err
}
attachmentID, err := parseUUID(wire.AttachmentID, "attachment_id")
if err != nil {
return Envelope{}, err
}
createdAt, err := time.Parse(time.RFC3339, wire.CreatedAt)
if err != nil {
return Envelope{}, fmt.Errorf("%w: invalid created_at", errInvalidEnvelope)
}
envelope := Envelope{
SchemaVersion: wire.SchemaVersion,
JobID: jobID,
Type: wire.Type,
AttachmentID: attachmentID,
PipelineVersion: wire.PipelineVersion,
Attempt: wire.Attempt,
CreatedAt: createdAt,
Traceparent: wire.Traceparent,
Tracestate: wire.Tracestate,
}
if err := envelope.Validate(); err != nil {
return Envelope{}, err
}
return envelope, nil
}
type envelopeJSON 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"`
Traceparent string `json:"traceparent,omitempty"`
Tracestate string `json:"tracestate,omitempty"`
}
func parseUUID(value, field string) (uuid.UUID, error) {
if !uuidPattern.MatchString(value) {
return uuid.Nil, fmt.Errorf("%w: invalid %s", errInvalidEnvelope, field)
}
parsed, err := uuid.Parse(value)
if err != nil {
return uuid.Nil, fmt.Errorf("%w: invalid %s", errInvalidEnvelope, field)
}
return parsed, nil
}
func ensureJSONEnd(decoder *json.Decoder) error {
var value any
if err := decoder.Decode(&value); !errors.Is(err, io.EOF) {
return fmt.Errorf("%w: trailing JSON value", errInvalidEnvelope)
}
return nil
}