| 1 | package dbreads |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "encoding/base32" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "regexp" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | var idEnc = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding) |
| 15 | |
| 16 | var ( |
| 17 | idShape = regexp.MustCompile(`^dbr_[a-z2-7]{26}$`) |
| 18 | regionShape = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) |
| 19 | hourShape = regexp.MustCompile(`^[0-9]{10}$`) |
| 20 | ) |
| 21 | |
| 22 | // NewID mints an id from r: "dbr_" and 26 lowercase base32 characters, 128 |
| 23 | // random bits, the way runs.NewID mints run ids. Production mints with MintID. |
| 24 | func NewID(r io.Reader) (string, error) { |
| 25 | var b [16]byte |
| 26 | if _, err := io.ReadFull(r, b[:]); err != nil { |
| 27 | return "", err |
| 28 | } |
| 29 | return "dbr_" + idEnc.EncodeToString(b[:]), nil |
| 30 | } |
| 31 | |
| 32 | func MintID() (string, error) { return NewID(rand.Reader) } |
| 33 | |
| 34 | // ValidID reports a db read id: 30 bytes of [a-z2-7_], one clean path |
| 35 | // element, and never "queue". |
| 36 | func ValidID(id string) bool { return idShape.MatchString(id) } |
| 37 | |
| 38 | // ValidRegion reports a placement code that can be one key segment. A fleet |
| 39 | // placement ("fleet/x") is not one: fleet instances never forward. |
| 40 | func ValidRegion(r string) bool { return regionShape.MatchString(r) } |
| 41 | |
| 42 | func RequestKey(id string) string { return Prefix + id + "/request.json" } |
| 43 | func PickedKey(id string) string { return Prefix + id + "/picked" } |
| 44 | func OutPrefix(id string) string { return Prefix + id + "/out/" } |
| 45 | func ResultKey(id string) string { return Prefix + id + "/result.json" } |
| 46 | func CancelKey(id string) string { return Prefix + id + "/cancel" } |
| 47 | |
| 48 | // ChunkKey is out/<seq>, zero-padded to 8 digits so keys sort in order. |
| 49 | func ChunkKey(id string, seq int64) string { return OutPrefix(id) + fmt.Sprintf("%08d", seq) } |
| 50 | |
| 51 | // ParseChunkKey reads a key ChunkKey made under outPrefix. |
| 52 | func ParseChunkKey(key, outPrefix string) (int64, bool) { |
| 53 | name, ok := strings.CutPrefix(key, outPrefix) |
| 54 | if !ok || len(name) != 8 { |
| 55 | return 0, false |
| 56 | } |
| 57 | for i := 0; i < 8; i++ { |
| 58 | if name[i] < '0' || name[i] > '9' { |
| 59 | return 0, false |
| 60 | } |
| 61 | } |
| 62 | seq, err := strconv.ParseInt(name, 10, 64) |
| 63 | if err != nil || seq < 1 { |
| 64 | return 0, false |
| 65 | } |
| 66 | return seq, true |
| 67 | } |
| 68 | |
| 69 | const hourLayout = "2006010215" |
| 70 | |
| 71 | func QueueRegionPrefix(region string) string { return Prefix + "queue/" + region + "/" } |
| 72 | |
| 73 | func QueueHourPrefix(region string, t time.Time) string { |
| 74 | return QueueRegionPrefix(region) + t.UTC().Format(hourLayout) + "/" |
| 75 | } |
| 76 | |
| 77 | // QueueKey is the empty pointer the console writes after the request: |
| 78 | // db-reads/queue/<region>/<YYYYMMDDHH>/<unix>-<id>. A box can skip an |
| 79 | // out-of-window entry from its key alone (spec §5.4). |
| 80 | func QueueKey(region string, issued time.Time, id string) string { |
| 81 | return QueueHourPrefix(region, issued) + strconv.FormatInt(issued.Unix(), 10) + "-" + id |
| 82 | } |
| 83 | |
| 84 | // QueueEntry is a parsed queue key. |
| 85 | type QueueEntry struct { |
| 86 | Region, Hour, ID string |
| 87 | Issued time.Time |
| 88 | } |
| 89 | |
| 90 | func ParseQueueKey(key string) (QueueEntry, bool) { |
| 91 | rest, ok := strings.CutPrefix(key, Prefix+"queue/") |
| 92 | if !ok { |
| 93 | return QueueEntry{}, false |
| 94 | } |
| 95 | parts := strings.Split(rest, "/") |
| 96 | if len(parts) != 3 || !ValidRegion(parts[0]) || !hourShape.MatchString(parts[1]) { |
| 97 | return QueueEntry{}, false |
| 98 | } |
| 99 | unixRaw, id, ok := strings.Cut(parts[2], "-") |
| 100 | if !ok || !ValidID(id) { |
| 101 | return QueueEntry{}, false |
| 102 | } |
| 103 | unix, err := strconv.ParseInt(unixRaw, 10, 64) |
| 104 | if err != nil || unix <= 0 || strconv.FormatInt(unix, 10) != unixRaw { |
| 105 | return QueueEntry{}, false |
| 106 | } |
| 107 | return QueueEntry{Region: parts[0], Hour: parts[1], ID: id, Issued: time.Unix(unix, 0).UTC()}, true |
| 108 | } |
| 109 | |
| 110 | // InWindow is the box's pickup window: no more than PickupWindow plus Skew |
| 111 | // old, and no more than Skew in the future (spec §5.5, §5.7). |
| 112 | func InWindow(issued, now time.Time) bool { |
| 113 | return !issued.Before(now.Add(-PickupWindow-Skew)) && !issued.After(now.Add(Skew)) |
| 114 | } |
| 115 | |