diff options
author | 2022-03-15 15:01:19 +0100 | |
---|---|---|
committer | 2022-03-15 15:01:19 +0100 | |
commit | e63b6531994adcf976d8e15c2b791682b8531e7d (patch) | |
tree | 6be094842816c978a3dda349c21ccc49ae8b4b95 /internal/transport/controller.go | |
parent | [bugfix] Fix bug where admin panel could not be accessed at `/admin` (#427) (diff) | |
download | gotosocial-e63b6531994adcf976d8e15c2b791682b8531e7d.tar.xz |
[performance] Add dereference shortcuts to avoid making http calls to self (#430)
* update transport (controller) to allow shortcuts
* go fmt
* expose underlying sig transport to allow test sigs
Diffstat (limited to 'internal/transport/controller.go')
-rw-r--r-- | internal/transport/controller.go | 80 |
1 files changed, 66 insertions, 14 deletions
diff --git a/internal/transport/controller.go b/internal/transport/controller.go index b16f5a1b0..56a922a8b 100644 --- a/internal/transport/controller.go +++ b/internal/transport/controller.go @@ -21,14 +21,18 @@ package transport import ( "context" "crypto" + "encoding/json" "fmt" + "net/url" "sync" "github.com/go-fed/httpsig" "github.com/spf13/viper" "github.com/superseriousbusiness/activity/pub" + "github.com/superseriousbusiness/activity/streams" "github.com/superseriousbusiness/gotosocial/internal/config" "github.com/superseriousbusiness/gotosocial/internal/db" + "github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb" ) // Controller generates transports for use in making federation requests to other servers. @@ -42,19 +46,65 @@ type controller struct { clock pub.Clock client pub.HttpClient appAgent string + + // dereferenceFollowersShortcut is a shortcut to dereference followers of an + // account on this instance, without making any external api/http calls. + // + // It is passed to new transports, and should only be invoked when the iri.Host == this host. + dereferenceFollowersShortcut func(ctx context.Context, iri *url.URL) ([]byte, error) + + // dereferenceUserShortcut is a shortcut to dereference followers an account on + // this instance, without making any external api/http calls. + // + // It is passed to new transports, and should only be invoked when the iri.Host == this host. + dereferenceUserShortcut func(ctx context.Context, iri *url.URL) ([]byte, error) +} + +func dereferenceFollowersShortcut(federatingDB federatingdb.DB) func(context.Context, *url.URL) ([]byte, error) { + return func(ctx context.Context, iri *url.URL) ([]byte, error) { + followers, err := federatingDB.Followers(ctx, iri) + if err != nil { + return nil, err + } + + i, err := streams.Serialize(followers) + if err != nil { + return nil, err + } + + return json.Marshal(i) + } +} + +func dereferenceUserShortcut(federatingDB federatingdb.DB) func(context.Context, *url.URL) ([]byte, error) { + return func(ctx context.Context, iri *url.URL) ([]byte, error) { + user, err := federatingDB.Get(ctx, iri) + if err != nil { + return nil, err + } + + i, err := streams.Serialize(user) + if err != nil { + return nil, err + } + + return json.Marshal(i) + } } // NewController returns an implementation of the Controller interface for creating new transports -func NewController(db db.DB, clock pub.Clock, client pub.HttpClient) Controller { +func NewController(db db.DB, federatingDB federatingdb.DB, clock pub.Clock, client pub.HttpClient) Controller { applicationName := viper.GetString(config.Keys.ApplicationName) host := viper.GetString(config.Keys.Host) appAgent := fmt.Sprintf("%s %s", applicationName, host) return &controller{ - db: db, - clock: clock, - client: client, - appAgent: appAgent, + db: db, + clock: clock, + client: client, + appAgent: appAgent, + dereferenceFollowersShortcut: dereferenceFollowersShortcut(federatingDB), + dereferenceUserShortcut: dereferenceUserShortcut(federatingDB), } } @@ -78,15 +128,17 @@ func (c *controller) NewTransport(pubKeyID string, privkey crypto.PrivateKey) (T sigTransport := pub.NewHttpSigTransport(c.client, c.appAgent, c.clock, getSigner, postSigner, pubKeyID, privkey) return &transport{ - client: c.client, - appAgent: c.appAgent, - gofedAgent: "(go-fed/activity v1.0.0)", - clock: c.clock, - pubKeyID: pubKeyID, - privkey: privkey, - sigTransport: sigTransport, - getSigner: getSigner, - getSignerMu: &sync.Mutex{}, + client: c.client, + appAgent: c.appAgent, + gofedAgent: "(go-fed/activity v1.0.0)", + clock: c.clock, + pubKeyID: pubKeyID, + privkey: privkey, + sigTransport: sigTransport, + getSigner: getSigner, + getSignerMu: &sync.Mutex{}, + dereferenceFollowersShortcut: c.dereferenceFollowersShortcut, + dereferenceUserShortcut: c.dereferenceUserShortcut, }, nil } |