package dbreads import ( "crypto/rand" "encoding/base32" "fmt" "io" "regexp" "strconv" "strings" "time" ) var idEnc = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding) var ( idShape = regexp.MustCompile(`^dbr_[a-z2-7]{26}$`) regionShape = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) hourShape = regexp.MustCompile(`^[0-9]{10}$`) ) // NewID mints an id from r: "dbr_" and 26 lowercase base32 characters, 128 // random bits, the way runs.NewID mints run ids. Production mints with MintID. func NewID(r io.Reader) (string, error) { var b [16]byte if _, err := io.ReadFull(r, b[:]); err != nil { return "", err } return "dbr_" + idEnc.EncodeToString(b[:]), nil } func MintID() (string, error) { return NewID(rand.Reader) } // ValidID reports a db read id: 30 bytes of [a-z2-7_], one clean path // element, and never "queue". func ValidID(id string) bool { return idShape.MatchString(id) } // ValidRegion reports a placement code that can be one key segment. A fleet // placement ("fleet/x") is not one: fleet instances never forward. func ValidRegion(r string) bool { return regionShape.MatchString(r) } func RequestKey(id string) string { return Prefix + id + "/request.json" } func PickedKey(id string) string { return Prefix + id + "/picked" } func OutPrefix(id string) string { return Prefix + id + "/out/" } func ResultKey(id string) string { return Prefix + id + "/result.json" } func CancelKey(id string) string { return Prefix + id + "/cancel" } // ChunkKey is out/, zero-padded to 8 digits so keys sort in order. func ChunkKey(id string, seq int64) string { return OutPrefix(id) + fmt.Sprintf("%08d", seq) } // ParseChunkKey reads a key ChunkKey made under outPrefix. func ParseChunkKey(key, outPrefix string) (int64, bool) { name, ok := strings.CutPrefix(key, outPrefix) if !ok || len(name) != 8 { return 0, false } for i := 0; i < 8; i++ { if name[i] < '0' || name[i] > '9' { return 0, false } } seq, err := strconv.ParseInt(name, 10, 64) if err != nil || seq < 1 { return 0, false } return seq, true } const hourLayout = "2006010215" func QueueRegionPrefix(region string) string { return Prefix + "queue/" + region + "/" } func QueueHourPrefix(region string, t time.Time) string { return QueueRegionPrefix(region) + t.UTC().Format(hourLayout) + "/" } // QueueKey is the empty pointer the console writes after the request: // db-reads/queue///-. A box can skip an // out-of-window entry from its key alone (spec §5.4). func QueueKey(region string, issued time.Time, id string) string { return QueueHourPrefix(region, issued) + strconv.FormatInt(issued.Unix(), 10) + "-" + id } // QueueEntry is a parsed queue key. type QueueEntry struct { Region, Hour, ID string Issued time.Time } func ParseQueueKey(key string) (QueueEntry, bool) { rest, ok := strings.CutPrefix(key, Prefix+"queue/") if !ok { return QueueEntry{}, false } parts := strings.Split(rest, "/") if len(parts) != 3 || !ValidRegion(parts[0]) || !hourShape.MatchString(parts[1]) { return QueueEntry{}, false } unixRaw, id, ok := strings.Cut(parts[2], "-") if !ok || !ValidID(id) { return QueueEntry{}, false } unix, err := strconv.ParseInt(unixRaw, 10, 64) if err != nil || unix <= 0 || strconv.FormatInt(unix, 10) != unixRaw { return QueueEntry{}, false } return QueueEntry{Region: parts[0], Hour: parts[1], ID: id, Issued: time.Unix(unix, 0).UTC()}, true } // InWindow is the box's pickup window: no more than PickupWindow plus Skew // old, and no more than Skew in the future (spec §5.5, §5.7). func InWindow(issued, now time.Time) bool { return !issued.Before(now.Add(-PickupWindow-Skew)) && !issued.After(now.Add(Skew)) }