From 14bf8e62f81d7eac637f2097a88b4c3c32a8a7b5 Mon Sep 17 00:00:00 2001 From: kim Date: Fri, 17 Oct 2025 19:41:06 +0200 Subject: [performance] reduce account stats database calls (#4496) Reduces both code complexity and the number of separate database transactions we need to make by moving account statistics operations into the database as side-effects of the operations that effect them. In contrast to currently, where we manually update account statistics at the application layer. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4496 Co-authored-by: kim Co-committed-by: kim --- internal/processing/workers/fromclientapi.go | 77 ++-------------------------- 1 file changed, 3 insertions(+), 74 deletions(-) (limited to 'internal/processing/workers/fromclientapi.go') diff --git a/internal/processing/workers/fromclientapi.go b/internal/processing/workers/fromclientapi.go index 9cdbcc548..992f6d9e8 100644 --- a/internal/processing/workers/fromclientapi.go +++ b/internal/processing/workers/fromclientapi.go @@ -351,11 +351,6 @@ func (p *clientAPI) CreateStatus(ctx context.Context, cMsg *messages.FromClientA // Don't return, just continue as normal. } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, cMsg.Origin, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // We specifically do not timeline // or notify for backfilled statuses, // as these are more for archival than @@ -451,11 +446,6 @@ func (p *clientAPI) CreateFollowReq(ctx context.Context, cMsg *messages.FromClie }) } - // Update stats for the target account. - if err := p.utils.incrementFollowRequestsCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.surface.notifyFollowRequest(ctx, followRequest); err != nil { log.Errorf(ctx, "error notifying follow request: %v", err) } @@ -659,11 +649,6 @@ func (p *clientAPI) CreateAnnounce(ctx context.Context, cMsg *messages.FromClien // Don't return, just continue as normal. } - // Update stats for the actor account. - if err := p.utils.incrementStatusesCount(ctx, cMsg.Origin, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Timeline and notify the boost wrapper status. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { log.Errorf(ctx, "error timelining and notifying status: %v", err) @@ -840,20 +825,6 @@ func (p *clientAPI) AcceptFollow(ctx context.Context, cMsg *messages.FromClientA return gtserror.Newf("%T not parseable as *gtsmodel.Follow", cMsg.GTSModel) } - // Update stats for the target account. - if err := p.utils.decrementFollowRequestsCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - if err := p.utils.incrementFollowersCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - // Update stats for the origin account. - if err := p.utils.incrementFollowingCount(ctx, cMsg.Origin); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.surface.notifyFollow(ctx, follow); err != nil { log.Errorf(ctx, "error notifying follow: %v", err) } @@ -871,13 +842,7 @@ func (p *clientAPI) RejectFollowRequest(ctx context.Context, cMsg *messages.From return gtserror.Newf("%T not parseable as *gtsmodel.FollowRequest", cMsg.GTSModel) } - // Update stats for the target account. - if err := p.utils.decrementFollowRequestsCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - if err := p.federate.RejectFollow( - ctx, + if err := p.federate.RejectFollow(ctx, p.converter.FollowRequestToFollow(ctx, followReq), ); err != nil { log.Errorf(ctx, "error federating follow reject: %v", err) @@ -892,16 +857,6 @@ func (p *clientAPI) UndoFollow(ctx context.Context, cMsg *messages.FromClientAPI return gtserror.Newf("%T not parseable as *gtsmodel.Follow", cMsg.GTSModel) } - // Update stats for the origin account. - if err := p.utils.decrementFollowingCount(ctx, cMsg.Origin); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - - // Update stats for the target account. - if err := p.utils.decrementFollowersCount(ctx, cMsg.Target); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if follow.Account.IsLocal() { // Remove posts by target from origin's timelines. p.surface.removeRelationshipFromTimelines(ctx, @@ -965,11 +920,6 @@ func (p *clientAPI) UndoAnnounce(ctx context.Context, cMsg *messages.FromClientA return gtserror.Newf("db error deleting status: %w", err) } - // Update stats for the origin account. - if err := p.utils.decrementStatusesCount(ctx, cMsg.Origin, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - // Delete the boost wrapper status from timelines. p.surface.deleteStatusFromTimelines(ctx, status.ID) @@ -1030,11 +980,6 @@ func (p *clientAPI) DeleteStatus(ctx context.Context, cMsg *messages.FromClientA log.Errorf(ctx, "error wiping status: %v", err) } - // Update stats for the origin account. - if err := p.utils.decrementStatusesCount(ctx, cMsg.Origin, status); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } - if err := p.federate.DeleteStatus(ctx, status); err != nil { log.Errorf(ctx, "error federating status delete: %v", err) } @@ -1258,15 +1203,7 @@ func (p *clientAPI) AcceptReply(ctx context.Context, cMsg *messages.FromClientAP return gtserror.Newf("%T not parseable as *gtsmodel.InteractionRequest", cMsg.GTSModel) } - var ( - interactingAcct = req.InteractingAccount - reply = req.Reply - ) - - // Update stats for the reply author account. - if err := p.utils.incrementStatusesCount(ctx, interactingAcct, reply); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } + reply := req.Reply // Timeline the reply + notify relevant accounts. if err := p.surface.timelineAndNotifyStatus(ctx, reply); err != nil { @@ -1291,15 +1228,7 @@ func (p *clientAPI) AcceptAnnounce(ctx context.Context, cMsg *messages.FromClien return gtserror.Newf("%T not parseable as *gtsmodel.InteractionRequest", cMsg.GTSModel) } - var ( - interactingAcct = req.InteractingAccount - boost = req.Announce - ) - - // Update stats for the boost author account. - if err := p.utils.incrementStatusesCount(ctx, interactingAcct, boost); err != nil { - log.Errorf(ctx, "error updating account stats: %v", err) - } + boost := req.Announce // Timeline and notify the announce. if err := p.surface.timelineAndNotifyStatus(ctx, boost); err != nil { -- cgit v1.2.3