summaryrefslogtreecommitdiff
path: root/internal/transport/controller.go
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-05-15 09:40:48 +0000
committerLibravatar kim <gruf@noreply.codeberg.org>2025-05-15 09:40:48 +0000
commit3cff4b2d7d138f45787a94e6e64acdccb00e8951 (patch)
treed18f18289b3082b129d6e129aa383ddde5869521 /internal/transport/controller.go
parent[chore] Change default database in example config to sqlite, update docs. (#4... (diff)
downloadgotosocial-3cff4b2d7d138f45787a94e6e64acdccb00e8951.tar.xz
[chore] various federatingdb tweaks (#4178)
after seeing a potential reported federating worker lockup i decided to start digging into the federatingdb code. this PR encompasses: - removes one of our last unused interface types `federatingdb.DB{}`, replacing it with a struct type `*federatingdb.DB{}` - in `transport.dereferenceLocal()` differentiates between an unsupported lookup type and ErrNoEntries to reduce unnecessary calls, and reduce potential lockups that may occur while trying to call our own endpoints that then call `federatingdb.Lock()` - removes a bunch of the locks on follow state changes since the DB already synchronizes that - removes the unnecessary `pub.Clock{}` struct field and type passed to the transport controller frankly it would be great if we could remove the locking in `federatingdb.Lock()` and instead handle it ourselves as it gets very confusing trying to figure out what functions will have locks held. but i guess that's one for when we move further away from the go-fed/activity/pub package usage. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4178 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal/transport/controller.go')
-rw-r--r--internal/transport/controller.go58
1 files changed, 36 insertions, 22 deletions
diff --git a/internal/transport/controller.go b/internal/transport/controller.go
index 0f3c1c9b0..33b74c76e 100644
--- a/internal/transport/controller.go
+++ b/internal/transport/controller.go
@@ -23,6 +23,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/json"
+ "errors"
"fmt"
"io"
"net/http"
@@ -30,11 +31,12 @@ import (
"strconv"
"code.superseriousbusiness.org/activity/pub"
- "code.superseriousbusiness.org/activity/streams/vocab"
"code.superseriousbusiness.org/gotosocial/internal/ap"
apiutil "code.superseriousbusiness.org/gotosocial/internal/api/util"
"code.superseriousbusiness.org/gotosocial/internal/config"
+ "code.superseriousbusiness.org/gotosocial/internal/db"
"code.superseriousbusiness.org/gotosocial/internal/federation/federatingdb"
+ "code.superseriousbusiness.org/gotosocial/internal/gtserror"
"code.superseriousbusiness.org/gotosocial/internal/state"
"code.superseriousbusiness.org/gotosocial/internal/util"
"codeberg.org/gruf/go-byteutil"
@@ -52,15 +54,14 @@ type Controller interface {
type controller struct {
state *state.State
- fedDB federatingdb.DB
- clock pub.Clock
+ fedDB *federatingdb.DB
client pub.HttpClient
trspCache cache.TTLCache[string, *transport]
userAgent string
}
// NewController returns an implementation of the Controller interface for creating new transports
-func NewController(state *state.State, federatingDB federatingdb.DB, clock pub.Clock, client pub.HttpClient) Controller {
+func NewController(state *state.State, federatingDB *federatingdb.DB, client pub.HttpClient) Controller {
var (
host = config.GetHost()
proto = config.GetProtocol()
@@ -70,7 +71,6 @@ func NewController(state *state.State, federatingDB federatingdb.DB, clock pub.C
c := &controller{
state: state,
fedDB: federatingDB,
- clock: clock,
client: client,
trspCache: cache.NewTTL[string, *transport](0, 100, 0),
userAgent: fmt.Sprintf("gotosocial/%s (+%s://%s)", version, proto, host),
@@ -153,37 +153,51 @@ func (c *controller) dereferenceLocal(
ctx context.Context,
uri *url.URL,
) (*http.Response, error) {
- var (
- t vocab.Type
- err error
- )
- t, err = c.fedDB.Get(ctx, uri)
- if err != nil {
- // Don't check especially for
- // db.ErrNoEntries, as we *want*
- // to pass this back to the caller
- // if we didn't get anything.
- return nil, err
+ // Try fetch via federating DB.
+ t, err := c.fedDB.Get(ctx, uri)
+
+ switch {
+ // No problem.
+ case err == nil:
+
+ // Catch and handle objects not found.
+ case errors.Is(err, db.ErrNoEntries):
+ return &http.Response{
+ Request: &http.Request{URL: uri},
+ Status: http.StatusText(http.StatusNotFound),
+ StatusCode: http.StatusNotFound,
+ Header: map[string][]string{
+ "Content-Type": {apiutil.AppActivityLDJSON},
+ "Content-Length": {"0"},
+ },
+ }, nil
+
+ // Any other.
+ default:
+ return nil, gtserror.Newf("error getting: %w", err)
}
if util.IsNil(t) {
- // This should never happen.
- panic("nil vocab.Type after successful c.fedDB.Get call")
+ // Assert this should never happen.
+ panic(gtserror.New("nil vocab.Type"))
}
- i, err := ap.Serialize(t)
+ // Serialize type to JSON map.
+ m, err := ap.Serialize(t)
if err != nil {
return nil, err
}
- b, err := json.Marshal(i)
+ // Marshal JSON to bytes.
+ b, err := json.Marshal(m)
if err != nil {
return nil, err
}
- contentLength := len(b)
- // Return a response with AS data as body.
+ // Return a response
+ // with AS data as body.
+ contentLength := len(b)
rsp := &http.Response{
Request: &http.Request{URL: uri},
Status: http.StatusText(http.StatusOK),