package keys import ( "math" "sync" "time" ) const ( slotCount = 16 shardCount = 16 maxBuckets = 200000 ) type slot struct { photos uint32 low uint16 solved uint16 } type bucket struct { key [16]byte at int64 solveAt int64 solveBase uint32 prev int32 next int32 slots [slotCount]slot } type shard struct { mu sync.Mutex index map[[16]byte]int32 items []bucket head int32 tail int32 } type table struct { width int64 span int64 limit int shards [shardCount]shard } func newTable(window time.Duration) *table { width := max(int64(time.Minute), (int64(window)+slotCount-1)/slotCount) width = (width + int64(time.Minute) - 1) / int64(time.Minute) * int64(time.Minute) span := min(int64(slotCount), (int64(window)+width-1)/width) t := &table{width: width, span: span, limit: maxBuckets / shardCount} for i := range t.shards { t.shards[i].index = make(map[[16]byte]int32) t.shards[i].head = -1 t.shards[i].tail = -1 } return t } func (s *shard) unlink(i int32) { b := &s.items[i] if b.prev >= 0 { s.items[b.prev].next = b.next } else { s.head = b.next } if b.next >= 0 { s.items[b.next].prev = b.prev } else { s.tail = b.prev } b.prev, b.next = -1, -1 } func (s *shard) front(i int32) { b := &s.items[i] b.prev = -1 b.next = s.head if s.head >= 0 { s.items[s.head].prev = i } s.head = i if s.tail < 0 { s.tail = i } } func (s *shard) get(key [16]byte, limit int, now int64) *bucket { if i, ok := s.index[key]; ok { if s.head != i { s.unlink(i) s.front(i) } return &s.items[i] } var i int32 if len(s.items) < limit { s.items = append(s.items, bucket{}) i = int32(len(s.items) - 1) } else { i = s.tail s.unlink(i) delete(s.index, s.items[i].key) } s.items[i] = bucket{key: key, at: now, solveAt: math.MinInt64 / 2, prev: -1, next: -1} s.index[key] = i s.front(i) return &s.items[i] } func (b *bucket) advance(now int64) { if now <= b.at { return } if now-b.at >= slotCount { b.slots = [slotCount]slot{} } else { for at := b.at + 1; at <= now; at++ { b.slots[at%slotCount] = slot{} } } b.at = now } type input struct { nav bool low bool pass bool page bool solved bool photos int photoLimit int landings int powBits int } type verdict int const ( grant verdict = iota challenge limited ) type outcome struct { verdict verdict bits int retry time.Duration } func shardOf(key [16]byte) int { h := uint32(2166136261) for _, c := range key { h = (h ^ uint32(c)) * 16777619 } return int(h % shardCount) } func (t *table) decide(key [16]byte, now time.Time, in input) outcome { s := &t.shards[shardOf(key)] s.mu.Lock() defer s.mu.Unlock() cur := now.UnixNano() / t.width b := s.get(key, t.limit, cur) b.advance(cur) var photos, since, low, solved uint64 for i := int64(0); i < t.span; i++ { at := cur - i sl := &b.slots[at%slotCount] photos += uint64(sl.photos) low += uint64(sl.low) solved += uint64(sl.solved) if at >= b.solveAt { since += uint64(sl.photos) } } if b.solveAt > cur-t.span { since -= min(since, uint64(b.solveBase)) } else { since = photos } limit := uint64(in.photoLimit) if photos > 4*limit { return outcome{verdict: limited, retry: t.retry(b, cur, now, photos-4*limit)} } here := &b.slots[cur%slotCount] if in.solved { for i := range b.slots { b.slots[i].low = 0 } if here.solved < math.MaxUint16 { here.solved++ } b.solveAt = cur b.solveBase = here.photos here.photos += uint32(in.photos) return outcome{verdict: grant} } admitted := in.pass || in.page if since > limit || (!admitted && in.nav && in.low) || (!admitted && low >= uint64(in.landings)) { return outcome{verdict: challenge, bits: min(32, in.powBits+int(min(6, solved)))} } here.photos += uint32(in.photos) if !admitted && !in.nav && in.low && here.low < math.MaxUint16 { here.low++ } return outcome{verdict: grant} } func (t *table) retry(b *bucket, cur int64, now time.Time, excess uint64) time.Duration { var dropped uint64 for i := t.span - 1; i >= 0; i-- { at := cur - i dropped += uint64(b.slots[at%slotCount].photos) if dropped >= excess { return time.Duration((at+t.span)*t.width - now.UnixNano()) } } return time.Duration(t.width) }