summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2025-01-22 12:42:12 +0000
committerLibravatar GitHub <noreply@github.com>2025-01-22 13:42:12 +0100
commit0a99901c657909a605a7f1c6a4ede282ba01ce43 (patch)
treea8b3f1bba879b051db41b18e1707d0c90e315a20 /internal
parent[chore]: Bump github.com/coreos/go-oidc/v3 from 3.11.0 to 3.12.0 (#3662) (diff)
downloadgotosocial-0a99901c657909a605a7f1c6a4ede282ba01ce43.tar.xz
[performance] reduce InboxForward->Create calls by partially implementing Exists() (#3647)
* alphabetical reordering * keep a cache of activity IDs we have handled creates for * reduce number of inbox forwarding create calls by partially implementing Exists() * increase cache size, since all we're storing is string keys
Diffstat (limited to 'internal')
-rw-r--r--internal/cache/cache.go28
-rw-r--r--internal/federation/federatingdb/create.go4
-rw-r--r--internal/federation/federatingdb/db.go6
-rw-r--r--internal/federation/federatingdb/exists.go10
-rw-r--r--internal/federation/federator.go2
-rw-r--r--internal/federation/transport.go2
6 files changed, 29 insertions, 23 deletions
diff --git a/internal/cache/cache.go b/internal/cache/cache.go
index 560fbc9f6..6f925e24f 100644
--- a/internal/cache/cache.go
+++ b/internal/cache/cache.go
@@ -40,6 +40,11 @@ type Caches struct {
// the block []headerfilter.Filter cache.
BlockHeaderFilters headerfilter.Cache
+ // TTL cache of statuses -> filterable text fields.
+ // To ensure up-to-date fields, cache is keyed as:
+ // `[status.ID][status.UpdatedAt.Unix()]`
+ StatusesFilterableFields *ttl.Cache[string, []string]
+
// Visibility provides access to the item visibility
// cache. (used by the visibility filter).
Visibility VisibilityCache
@@ -47,11 +52,6 @@ type Caches struct {
// Webfinger provides access to the webfinger URL cache.
Webfinger *ttl.Cache[string, string] // TTL=24hr, sweep=5min
- // TTL cache of statuses -> filterable text fields.
- // To ensure up-to-date fields, cache is keyed as:
- // `[status.ID][status.UpdatedAt.Unix()]`
- StatusesFilterableFields *ttl.Cache[string, []string]
-
// prevent pass-by-value.
_ nocopy
}
@@ -203,6 +203,15 @@ func (c *Caches) Sweep(threshold float64) {
c.Visibility.Trim(threshold)
}
+func (c *Caches) initStatusesFilterableFields() {
+ c.StatusesFilterableFields = new(ttl.Cache[string, []string])
+ c.StatusesFilterableFields.Init(
+ 0,
+ 512,
+ 1*time.Hour,
+ )
+}
+
func (c *Caches) initWebfinger() {
// Calculate maximum cache size.
cap := calculateCacheMax(
@@ -219,12 +228,3 @@ func (c *Caches) initWebfinger() {
24*time.Hour,
)
}
-
-func (c *Caches) initStatusesFilterableFields() {
- c.StatusesFilterableFields = new(ttl.Cache[string, []string])
- c.StatusesFilterableFields.Init(
- 0,
- 512,
- 1*time.Hour,
- )
-}
diff --git a/internal/federation/federatingdb/create.go b/internal/federation/federatingdb/create.go
index 60232efe3..11030b16b 100644
--- a/internal/federation/federatingdb/create.go
+++ b/internal/federation/federatingdb/create.go
@@ -63,6 +63,10 @@ func (f *federatingDB) Create(ctx context.Context, asType vocab.Type) error {
return nil
}
+ // Cache entry for this create activity ID for later
+ // checks in the Exist() function if we see it again.
+ f.activityIDs.Set(ap.GetJSONLDId(asType).String(), struct{}{})
+
switch name := asType.GetTypeName(); name {
case ap.ActivityBlock:
// BLOCK SOMETHING
diff --git a/internal/federation/federatingdb/db.go b/internal/federation/federatingdb/db.go
index ba10ed98b..230098073 100644
--- a/internal/federation/federatingdb/db.go
+++ b/internal/federation/federatingdb/db.go
@@ -21,6 +21,7 @@ import (
"context"
"net/url"
+ "codeberg.org/gruf/go-cache/v3/simple"
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
@@ -61,6 +62,10 @@ type federatingDB struct {
visFilter *visibility.Filter
intFilter *interaction.Filter
spamFilter *spam.Filter
+
+ // tracks Activity IDs we have handled creates for,
+ // for use in the Exists() function during forwarding.
+ activityIDs simple.Cache[string, struct{}]
}
// New returns a DB that satisfies the pub.Database
@@ -79,5 +84,6 @@ func New(
intFilter: intFilter,
spamFilter: spamFilter,
}
+ fdb.activityIDs.Init(0, 2048)
return &fdb
}
diff --git a/internal/federation/federatingdb/exists.go b/internal/federation/federatingdb/exists.go
index abac9960e..ec996f72f 100644
--- a/internal/federation/federatingdb/exists.go
+++ b/internal/federation/federatingdb/exists.go
@@ -22,12 +22,8 @@ import (
"net/url"
)
-// Exists returns true if the database has an entry for the specified
-// id. It may not be owned by this application instance.
-//
-// The library makes this call only after acquiring a lock first.
-//
-// Implementation note: this just straight up isn't implemented, and doesn't *really* need to be either.
+// Exists is an implementation of pub.Database{}.Exists(), optimized specifically for
+// the only usecase in which go-fed/activity/pub actually calls it. Do not use otherwise!
func (f *federatingDB) Exists(ctx context.Context, id *url.URL) (exists bool, err error) {
- return false, nil
+ return f.activityIDs.Has(id.String()), nil
}
diff --git a/internal/federation/federator.go b/internal/federation/federator.go
index 4e11c7d4d..b6f54906b 100644
--- a/internal/federation/federator.go
+++ b/internal/federation/federator.go
@@ -33,7 +33,7 @@ import (
var _ interface {
pub.CommonBehavior
pub.FederatingProtocol
-} = &Federator{}
+} = (*Federator)(nil)
type Federator struct {
db db.DB
diff --git a/internal/federation/transport.go b/internal/federation/transport.go
index b6f7a46c8..bab89eafc 100644
--- a/internal/federation/transport.go
+++ b/internal/federation/transport.go
@@ -49,7 +49,7 @@ import (
// Note that the library will not maintain a long-lived pointer to the
// returned Transport so that any private credentials are able to be
// garbage collected.
-func (f *Federator) NewTransport(ctx context.Context, actorBoxIRI *url.URL, gofedAgent string) (pub.Transport, error) {
+func (f *Federator) NewTransport(ctx context.Context, actorBoxIRI *url.URL, _ string) (pub.Transport, error) {
var username string
var err error