mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-27 01:20:28 +00:00
7cc40302a5
* add miekg/dns dependency * set/validate accountDomain * move finger to dereferencer * totally break GetRemoteAccount * start reworking finger func a bit * start reworking getRemoteAccount a bit * move mention parts to namestring * rework webfingerget * use util function to extract webfinger parts * use accountDomain * rework finger again, final form * just a real nasty commit, the worst * remove refresh from account * use new ASRepToAccount signature * fix incorrect debug call * fix for new getRemoteAccount * rework GetRemoteAccount * start updating tests to remove repetition * break a lot of tests Move shared test logic into the testrig, rather than having it scattered all over the place. This allows us to just mock the transport controller once, and have all tests use it (unless they need not to for some other reason). * fix up tests to use main mock httpclient * webfinger only if necessary * cheeky linting with the lads * update mentionName regex recognize instance accounts * don't finger instance accounts * test webfinger part extraction * increase default worker count to 4 per cpu * don't repeat regex parsing * final search for discovered accountDomain * be more permissive in namestring lookup * add more extraction tests * simplify GetParseMentionFunc * skip long search if local account * fix broken test * consolidate to all use same caching libraries Signed-off-by: kim <grufwub@gmail.com> * perform more caching in the database layer Signed-off-by: kim <grufwub@gmail.com> * remove ASNote cache Signed-off-by: kim <grufwub@gmail.com> * update cache library, improve db tracing hooks Signed-off-by: kim <grufwub@gmail.com> * return ErrNoEntries if no account status IDs found, small formatting changes Signed-off-by: kim <grufwub@gmail.com> * fix tests, thanks tobi! Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
210 lines
5.8 KiB
Go
210 lines
5.8 KiB
Go
package cache
|
|
|
|
// LookupCfg is the LookupCache configuration.
|
|
type LookupCfg[OGKey, AltKey comparable, Value any] struct {
|
|
// RegisterLookups is called on init to register lookups
|
|
// within LookupCache's internal LookupMap
|
|
RegisterLookups func(*LookupMap[OGKey, AltKey])
|
|
|
|
// AddLookups is called on each addition to the cache, to
|
|
// set any required additional key lookups for supplied item
|
|
AddLookups func(*LookupMap[OGKey, AltKey], Value)
|
|
|
|
// DeleteLookups is called on each eviction/invalidation of
|
|
// an item in the cache, to remove any unused key lookups
|
|
DeleteLookups func(*LookupMap[OGKey, AltKey], Value)
|
|
}
|
|
|
|
// LookupCache is a cache built on-top of TTLCache, providing multi-key
|
|
// lookups for items in the cache by means of additional lookup maps. These
|
|
// maps simply store additional keys => original key, with hook-ins to automatically
|
|
// call user supplied functions on adding an item, or on updating/deleting an
|
|
// item to keep the LookupMap up-to-date.
|
|
type LookupCache[OGKey, AltKey comparable, Value any] interface {
|
|
Cache[OGKey, Value]
|
|
|
|
// GetBy fetches a cached value by supplied lookup identifier and key
|
|
GetBy(lookup string, key AltKey) (value Value, ok bool)
|
|
|
|
// CASBy will attempt to perform a CAS operation on supplied lookup identifier and key
|
|
CASBy(lookup string, key AltKey, cmp, swp Value) bool
|
|
|
|
// SwapBy will attempt to perform a swap operation on supplied lookup identifier and key
|
|
SwapBy(lookup string, key AltKey, swp Value) Value
|
|
|
|
// HasBy checks if a value is cached under supplied lookup identifier and key
|
|
HasBy(lookup string, key AltKey) bool
|
|
|
|
// InvalidateBy invalidates a value by supplied lookup identifier and key
|
|
InvalidateBy(lookup string, key AltKey) bool
|
|
}
|
|
|
|
type lookupTTLCache[OK, AK comparable, V any] struct {
|
|
TTLCache[OK, V]
|
|
config LookupCfg[OK, AK, V]
|
|
lookup LookupMap[OK, AK]
|
|
}
|
|
|
|
// NewLookup returns a new initialized LookupCache.
|
|
func NewLookup[OK, AK comparable, V any](cfg LookupCfg[OK, AK, V]) LookupCache[OK, AK, V] {
|
|
switch {
|
|
case cfg.RegisterLookups == nil:
|
|
panic("cache: nil lookups register function")
|
|
case cfg.AddLookups == nil:
|
|
panic("cache: nil lookups add function")
|
|
case cfg.DeleteLookups == nil:
|
|
panic("cache: nil delete lookups function")
|
|
}
|
|
c := &lookupTTLCache[OK, AK, V]{config: cfg}
|
|
c.TTLCache.Init()
|
|
c.lookup.lookup = make(map[string]map[AK]OK)
|
|
c.config.RegisterLookups(&c.lookup)
|
|
c.SetEvictionCallback(nil)
|
|
c.SetInvalidateCallback(nil)
|
|
return c
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) SetEvictionCallback(hook Hook[OK, V]) {
|
|
if hook == nil {
|
|
hook = emptyHook[OK, V]
|
|
}
|
|
c.TTLCache.SetEvictionCallback(func(key OK, value V) {
|
|
hook(key, value)
|
|
c.config.DeleteLookups(&c.lookup, value)
|
|
})
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) SetInvalidateCallback(hook Hook[OK, V]) {
|
|
if hook == nil {
|
|
hook = emptyHook[OK, V]
|
|
}
|
|
c.TTLCache.SetInvalidateCallback(func(key OK, value V) {
|
|
hook(key, value)
|
|
c.config.DeleteLookups(&c.lookup, value)
|
|
})
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) GetBy(lookup string, key AK) (V, bool) {
|
|
c.Lock()
|
|
origKey, ok := c.lookup.Get(lookup, key)
|
|
if !ok {
|
|
c.Unlock()
|
|
var value V
|
|
return value, false
|
|
}
|
|
v, ok := c.GetUnsafe(origKey)
|
|
c.Unlock()
|
|
return v, ok
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) Put(key OK, value V) bool {
|
|
c.Lock()
|
|
put := c.PutUnsafe(key, value)
|
|
if put {
|
|
c.config.AddLookups(&c.lookup, value)
|
|
}
|
|
c.Unlock()
|
|
return put
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) Set(key OK, value V) {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
c.SetUnsafe(key, value)
|
|
c.config.AddLookups(&c.lookup, value)
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) CASBy(lookup string, key AK, cmp, swp V) bool {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
origKey, ok := c.lookup.Get(lookup, key)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return c.CASUnsafe(origKey, cmp, swp)
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) SwapBy(lookup string, key AK, swp V) V {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
origKey, ok := c.lookup.Get(lookup, key)
|
|
if !ok {
|
|
var value V
|
|
return value
|
|
}
|
|
return c.SwapUnsafe(origKey, swp)
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) HasBy(lookup string, key AK) bool {
|
|
c.Lock()
|
|
has := c.lookup.Has(lookup, key)
|
|
c.Unlock()
|
|
return has
|
|
}
|
|
|
|
func (c *lookupTTLCache[OK, AK, V]) InvalidateBy(lookup string, key AK) bool {
|
|
c.Lock()
|
|
defer c.Unlock()
|
|
origKey, ok := c.lookup.Get(lookup, key)
|
|
if !ok {
|
|
return false
|
|
}
|
|
c.InvalidateUnsafe(origKey)
|
|
return true
|
|
}
|
|
|
|
// LookupMap is a structure that provides lookups for
|
|
// keys to primary keys under supplied lookup identifiers.
|
|
// This is essentially a wrapper around map[string](map[K1]K2).
|
|
type LookupMap[OK comparable, AK comparable] struct {
|
|
lookup map[string](map[AK]OK)
|
|
}
|
|
|
|
// RegisterLookup registers a lookup identifier in the LookupMap,
|
|
// note this can only be doing during the cfg.RegisterLookups() hook.
|
|
func (l *LookupMap[OK, AK]) RegisterLookup(id string) {
|
|
if _, ok := l.lookup[id]; ok {
|
|
panic("cache: lookup mapping already exists for identifier")
|
|
}
|
|
l.lookup[id] = make(map[AK]OK, 100)
|
|
}
|
|
|
|
// Get fetches an entry's primary key for lookup identifier and key.
|
|
func (l *LookupMap[OK, AK]) Get(id string, key AK) (OK, bool) {
|
|
keys, ok := l.lookup[id]
|
|
if !ok {
|
|
var key OK
|
|
return key, false
|
|
}
|
|
origKey, ok := keys[key]
|
|
return origKey, ok
|
|
}
|
|
|
|
// Set adds a lookup to the LookupMap under supplied lookup identifier,
|
|
// linking supplied key to the supplied primary (original) key.
|
|
func (l *LookupMap[OK, AK]) Set(id string, key AK, origKey OK) {
|
|
keys, ok := l.lookup[id]
|
|
if !ok {
|
|
panic("cache: invalid lookup identifier")
|
|
}
|
|
keys[key] = origKey
|
|
}
|
|
|
|
// Has checks if there exists a lookup for supplied identifier and key.
|
|
func (l *LookupMap[OK, AK]) Has(id string, key AK) bool {
|
|
keys, ok := l.lookup[id]
|
|
if !ok {
|
|
return false
|
|
}
|
|
_, ok = keys[key]
|
|
return ok
|
|
}
|
|
|
|
// Delete removes a lookup from LookupMap with supplied identifier and key.
|
|
func (l *LookupMap[OK, AK]) Delete(id string, key AK) {
|
|
keys, ok := l.lookup[id]
|
|
if !ok {
|
|
return
|
|
}
|
|
delete(keys, key)
|
|
}
|