Skip to content

Commit

Permalink
delete actors and follows from dead servers
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 23, 2023
1 parent 76267fb commit 0e850d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 6 additions & 2 deletions fed/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,14 @@ func deliver(ctx context.Context, log *slog.Logger, db *sql.DB, activity *ap.Act

if to, err := resolver.Resolve(ctx, log, db, actor, actorID, false); err != nil {
log.Warn("Failed to resolve a recipient", "to", actorID, "activity", activity.ID, "error", err)
anyFailed = true
if !errors.Is(err, ErrActorGone) && !errors.Is(err, ErrBlockedDomain) {
anyFailed = true
}
} else if err := Send(ctx, log, db, actor, resolver, to, buf); err != nil {
log.Warn("Failed to send a post", "to", actorID, "activity", activity.ID, "error", err)
anyFailed = true
if !errors.Is(err, ErrBlockedDomain) {
anyFailed = true
}
}

return true
Expand Down
17 changes: 16 additions & 1 deletion fed/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"io"
"io/ioutil"
"log/slog"
"net"
"net/http"
"net/url"
"path"
Expand All @@ -40,6 +41,7 @@ import (
const (
resolverCacheTTL = time.Hour * 24 * 3
resolverMaxIdleConns = 128
maxInstanceRecoveryTime = time.Hour * 24 * 30
resolverIdleConnTimeout = time.Minute
)

Expand Down Expand Up @@ -123,10 +125,12 @@ func (r *Resolver) resolve(ctx context.Context, log *slog.Logger, db *sql.DB, fr

var actorString string
var updated int64
var sinceLastUpdate time.Duration
if err := db.QueryRowContext(ctx, `select actor, updated from persons where id = ?`, to).Scan(&actorString, &updated); err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("Failed to fetch %s cache: %w", to, err)
} else if err == nil {
if !isLocal && !offline && time.Now().Sub(time.Unix(updated, 0)) > resolverCacheTTL {
sinceLastUpdate = time.Now().Sub(time.Unix(updated, 0))
if !isLocal && !offline && sinceLastUpdate > resolverCacheTTL {
log.Info("Updating old cache entry for actor", "to", to)
update = true
} else {
Expand Down Expand Up @@ -163,6 +167,17 @@ func (r *Resolver) resolve(ctx context.Context, log *slog.Logger, db *sql.DB, fr
return nil, fmt.Errorf("Failed to fetch %s: %w", finger, ErrActorGone)
}

var (
urlError *url.Error
opError *net.OpError
dnsError *net.DNSError
)
// if it's been a while since the last update and the server's domain is expired (NXDOMAIN), actor is gone
if sinceLastUpdate > maxInstanceRecoveryTime && errors.As(err, &urlError) && errors.As(urlError.Err, &opError) && errors.As(opError.Err, &dnsError) && dnsError.IsNotFound {
log.Warn("Server is probably gone, deleting associated objects", "to", to)
deleteActor(ctx, log, db, to)
}

return nil, fmt.Errorf("Failed to fetch %s: %w", finger, err)
}
defer resp.Body.Close()
Expand Down

0 comments on commit 0e850d8

Please sign in to comment.