97 lines
3.1 KiB
Go

package queryserving
import (
"context"
"strconv"
"time"
)
// QueryRegistry keeps only bounded, short-lived, server-derived report queries.
// It contains no permission decisions and never controls task mutations.
type QueryRegistry interface {
RememberQuery(context.Context, string, string, []byte)
RecentQueries(context.Context, string) [][]byte
}
const rememberQueryScript = `
redis.call('ZADD',KEYS[1],ARGV[1],ARGV[2])
redis.call('HSET',KEYS[2],ARGV[2],ARGV[3])
local old=redis.call('ZRANGEBYSCORE',KEYS[1],'-inf',ARGV[4])
for _,id in ipairs(old) do redis.call('ZREM',KEYS[1],id);redis.call('HDEL',KEYS[2],id) end
local excess=redis.call('ZCARD',KEYS[1])-256
if excess>0 then
local ids=redis.call('ZRANGE',KEYS[1],0,excess-1)
for _,id in ipairs(ids) do redis.call('ZREM',KEYS[1],id);redis.call('HDEL',KEYS[2],id) end
end
redis.call('EXPIRE',KEYS[1],480);redis.call('EXPIRE',KEYS[2],480)
return 1`
const recentQueriesScript = `
local ids=redis.call('ZREVRANGEBYSCORE',KEYS[1],'+inf',ARGV[1],'LIMIT',0,256)
local out={}
for _,id in ipairs(ids) do
local value=redis.call('HGET',KEYS[2],id)
if value then table.insert(out,id);table.insert(out,value) end
end
return out`
func (v *Valkey) registryKeys(scope string) (string, string) {
root := v.prefix + ":{" + Key("active-query/v1", scope) + "}"
return root + ":order", root + ":data"
}
func (v *Valkey) RememberQuery(parent context.Context, scope, id string, b []byte) {
if parent.Err() != nil || len(b) > 16<<10 || time.Now().UnixNano() < v.until.Load() {
return
}
ctx, cancel := context.WithTimeout(parent, 40*time.Millisecond)
defer cancel()
order, data := v.registryKeys(scope)
now := time.Now().Unix()
encoded := encodeCacheValue(scope+":"+id, b)
err := v.client.Do(ctx, v.client.B().Eval().Script(rememberQueryScript).Numkeys(2).Key(order, data).Arg(strconv.FormatInt(now, 10), id, string(encoded), strconv.FormatInt(now-240, 10)).Build()).Error()
if err != nil && parent.Err() == nil {
v.failed()
}
}
func (v *Valkey) RecentQueries(parent context.Context, scope string) [][]byte {
if parent.Err() != nil || time.Now().UnixNano() < v.until.Load() {
return nil
}
ctx, cancel := context.WithTimeout(parent, 100*time.Millisecond)
defer cancel()
order, data := v.registryKeys(scope)
values, err := v.client.Do(ctx, v.client.B().Eval().Script(recentQueriesScript).Numkeys(2).Key(order, data).Arg(strconv.FormatInt(time.Now().Unix()-240, 10)).Build()).AsStrSlice()
if err != nil {
if parent.Err() == nil {
v.failed()
}
return nil
}
if len(values)%2 != 0 || len(values) > 512 {
return nil
}
out := [][]byte{}
for i := 0; i < len(values); i += 2 {
if b, ok := decodeCacheValue(scope+":"+values[i], []byte(values[i+1])); ok && len(b) <= 16<<10 {
out = append(out, b)
}
}
return out
}
func (m *ManagedValkey) RememberQuery(ctx context.Context, scope, id string, b []byte) {
m.mu.RLock()
v := m.value
m.mu.RUnlock()
if v != nil {
v.RememberQuery(ctx, scope, id, b)
}
}
func (m *ManagedValkey) RecentQueries(ctx context.Context, scope string) [][]byte {
m.mu.RLock()
v := m.value
m.mu.RUnlock()
if v != nil {
return v.RecentQueries(ctx, scope)
}
return nil
}