summaryrefslogtreecommitdiff
path: root/internal/timeline/manager.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2025-04-26 09:56:15 +0000
committerLibravatar GitHub <noreply@github.com>2025-04-26 09:56:15 +0000
commit6a6a4993338262f87df34c9be051bfaac75c1829 (patch)
treebfbda090dc4b25efdd34145c016d7cc7b9c14d6e /internal/timeline/manager.go
parent[chore] Move deps to code.superseriousbusiness.org (#4054) (diff)
downloadgotosocial-6a6a4993338262f87df34c9be051bfaac75c1829.tar.xz
[performance] rewrite timelines to rely on new timeline cache type (#3941)
* start work rewriting timeline cache type * further work rewriting timeline caching * more work integration new timeline code * remove old code * add local timeline, fix up merge conflicts * remove old use of go-bytes * implement new timeline code into more areas of codebase, pull in latest go-mangler, go-mutexes, go-structr * remove old timeline package, add local timeline cache * remove references to old timeline types that needed starting up in tests * start adding page validation * fix test-identified timeline cache package issues * fix up more tests, fix missing required changes, etc * add exclusion for test.out in gitignore * clarify some things better in code comments * tweak cache size limits * fix list timeline cache fetching * further list timeline fixes * linter, ssssssssshhhhhhhhhhhh please * fix linter hints * reslice the output if it's beyond length of 'lim' * remove old timeline initialization code, bump go-structr to v0.9.4 * continued from previous commit * improved code comments * don't allow multiple entries for BoostOfID values to prevent repeated boosts of same boosts * finish writing more code comments * some variable renaming, for ease of following * change the way we update lo,hi paging values during timeline load * improved code comments for updated / returned lo , hi paging values * finish writing code comments for the StatusTimeline{} type itself * fill in more code comments * update go-structr version to latest with changed timeline unique indexing logic * have a local and public timeline *per user* * rewrite calls to public / local timeline calls * remove the zero length check, as lo, hi values might still be set * simplify timeline cache loading, fix lo/hi returns, fix timeline invalidation side-effects missing for some federated actions * swap the lo, hi values :facepalm: * add (now) missing slice reverse of tag timeline statuses when paging ASC * remove local / public caches (is out of scope for this work), share more timeline code * remove unnecessary change * again, remove more unused code * remove unused function to appease the linter * move boost checking to prepare function * fix use of timeline.lastOrder, fix incorrect range functions used * remove comments for repeat code * remove the boost logic from prepare function * do a maximum of 5 loads, not 10 * add repeat boost filtering logic, update go-structr, general improvements * more code comments * add important note * fix timeline tests now that timelines are returned in page order * remove unused field * add StatusTimeline{} tests * add more status timeline tests * start adding preloading support * ensure repeat boosts are marked in preloaded entries * share a bunch of the database load code in timeline cache, don't clear timelines on relationship change * add logic to allow dynamic clear / preloading of timelines * comment-out unused functions, but leave in place as we might end-up using them * fix timeline preload state check * much improved status timeline code comments * more code comments, don't bother inserting statuses if timeline not preloaded * shift around some logic to make sure things aren't accidentally left set * finish writing code comments * remove trim-after-insert behaviour * fix-up some comments referring to old logic * remove unsetting of lo, hi * fix preload repeatBoost checking logic * don't return on status filter errors, these are usually transient * better concurrency safety in Clear() and Done() * fix test broken due to addition of preloader * fix repeatBoost logic that doesn't account for already-hidden repeatBoosts * ensure edit submodels are dropped on cache insertion * update code-comment to expand CAS accronym * use a plus1hULID() instead of 24h * remove unused functions * add note that public / local timeline requester can be nil * fix incorrect visibility filtering of tag timeline statuses * ensure we filter home timeline statuses on local only * some small re-orderings to confirm query params in correct places * fix the local only home timeline filter func
Diffstat (limited to 'internal/timeline/manager.go')
-rw-r--r--internal/timeline/manager.go259
1 files changed, 0 insertions, 259 deletions
diff --git a/internal/timeline/manager.go b/internal/timeline/manager.go
deleted file mode 100644
index b4f075138..000000000
--- a/internal/timeline/manager.go
+++ /dev/null
@@ -1,259 +0,0 @@
-// GoToSocial
-// Copyright (C) GoToSocial Authors admin@gotosocial.org
-// SPDX-License-Identifier: AGPL-3.0-or-later
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU Affero General Public License for more details.
-//
-// You should have received a copy of the GNU Affero General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-package timeline
-
-import (
- "context"
- "sync"
- "time"
-
- "github.com/superseriousbusiness/gotosocial/internal/gtserror"
- "github.com/superseriousbusiness/gotosocial/internal/log"
-)
-
-const (
- pruneLengthIndexed = 400
- pruneLengthPrepared = 50
-)
-
-// Manager abstracts functions for creating multiple timelines, and adding, removing, and fetching entries from those timelines.
-//
-// By the time a timelineable hits the manager interface, it should already have been filtered and it should be established that the item indeed
-// belongs in the given timeline.
-//
-// The manager makes a distinction between *indexed* items and *prepared* items.
-//
-// Indexed items consist of just that item's ID (in the database) and the time it was created. An indexed item takes up very little memory, so
-// it's not a huge priority to keep trimming the indexed items list.
-//
-// Prepared items consist of the item's database ID, the time it was created, AND the apimodel representation of that item, for quick serialization.
-// Prepared items of course take up more memory than indexed items, so they should be regularly pruned if they're not being actively served.
-type Manager interface {
- // IngestOne takes one timelineable and indexes it into the given timeline, and then immediately prepares it for serving.
- // This is useful in cases where we know the item will need to be shown at the top of a user's timeline immediately (eg., a new status is created).
- //
- // It should already be established before calling this function that the item actually belongs in the timeline!
- //
- // The returned bool indicates whether the item was actually put in the timeline. This could be false in cases where
- // a status is a boost, but a boost of the original status or the status itself already exists recently in the timeline.
- IngestOne(ctx context.Context, timelineID string, item Timelineable) (bool, error)
-
- // GetTimeline returns limit n amount of prepared entries from the given timeline, in descending chronological order.
- GetTimeline(ctx context.Context, timelineID string, maxID string, sinceID string, minID string, limit int, local bool) ([]Preparable, error)
-
- // GetIndexedLength returns the amount of items that have been indexed for the given account ID.
- GetIndexedLength(ctx context.Context, timelineID string) int
-
- // GetOldestIndexedID returns the id ID for the oldest item that we have indexed for the given timeline.
- // Will be an empty string if nothing is (yet) indexed.
- GetOldestIndexedID(ctx context.Context, timelineID string) string
-
- // Remove removes one item from the given timeline.
- Remove(ctx context.Context, timelineID string, itemID string) (int, error)
-
- // RemoveTimeline completely removes one timeline.
- RemoveTimeline(ctx context.Context, timelineID string) error
-
- // WipeItemFromAllTimelines removes one item from the index and prepared items of all timelines
- WipeItemFromAllTimelines(ctx context.Context, itemID string) error
-
- // WipeStatusesFromAccountID removes all items by the given accountID from the given timeline.
- WipeItemsFromAccountID(ctx context.Context, timelineID string, accountID string) error
-
- // UnprepareItem unprepares/uncaches the prepared version fo the given itemID from the given timelineID.
- // Use this for cache invalidation when the prepared representation of an item has changed.
- UnprepareItem(ctx context.Context, timelineID string, itemID string) error
-
- // UnprepareItemFromAllTimelines unprepares/uncaches the prepared version of the given itemID from all timelines.
- // Use this for cache invalidation when the prepared representation of an item has changed.
- UnprepareItemFromAllTimelines(ctx context.Context, itemID string) error
-
- // Prune manually triggers a prune operation for the given timelineID.
- Prune(ctx context.Context, timelineID string, desiredPreparedItemsLength int, desiredIndexedItemsLength int) (int, error)
-
- // Start starts hourly cleanup jobs for this timeline manager.
- Start() error
-
- // Stop stops the timeline manager (currently a stub, doesn't do anything).
- Stop() error
-}
-
-// NewManager returns a new timeline manager.
-func NewManager(grabFunction GrabFunction, filterFunction FilterFunction, prepareFunction PrepareFunction, skipInsertFunction SkipInsertFunction) Manager {
- return &manager{
- timelines: sync.Map{},
- grabFunction: grabFunction,
- filterFunction: filterFunction,
- prepareFunction: prepareFunction,
- skipInsertFunction: skipInsertFunction,
- }
-}
-
-type manager struct {
- timelines sync.Map
- grabFunction GrabFunction
- filterFunction FilterFunction
- prepareFunction PrepareFunction
- skipInsertFunction SkipInsertFunction
-}
-
-func (m *manager) Start() error {
- // Start a background goroutine which iterates
- // through all stored timelines once per hour,
- // and cleans up old entries if that timeline
- // hasn't been accessed in the last hour.
- go func() {
- for now := range time.NewTicker(1 * time.Hour).C {
- now := now // rescope
- // Define the range function inside here,
- // so that we can use the 'now' returned
- // by the ticker, instead of having to call
- // time.Now() multiple times.
- //
- // Unless it panics, this function always
- // returns 'true', to continue the Range
- // call through the sync.Map.
- f := func(_ any, v any) bool {
- timeline, ok := v.(Timeline)
- if !ok {
- log.Panic(nil, "couldn't parse timeline manager sync map value as Timeline, this should never happen so panic")
- }
-
- if now.Sub(timeline.LastGot()) < 1*time.Hour {
- // Timeline has been fetched in the
- // last hour, move on to the next one.
- return true
- }
-
- if amountPruned := timeline.Prune(pruneLengthPrepared, pruneLengthIndexed); amountPruned > 0 {
- log.WithField("accountID", timeline.TimelineID()).Infof("pruned %d indexed and prepared items from timeline", amountPruned)
- }
-
- return true
- }
-
- // Execute the function for each timeline.
- m.timelines.Range(f)
- }
- }()
-
- return nil
-}
-
-func (m *manager) Stop() error {
- return nil
-}
-
-func (m *manager) IngestOne(ctx context.Context, timelineID string, item Timelineable) (bool, error) {
- return m.getOrCreateTimeline(ctx, timelineID).IndexAndPrepareOne(
- ctx,
- item.GetID(),
- item.GetBoostOfID(),
- item.GetAccountID(),
- item.GetBoostOfAccountID(),
- )
-}
-
-func (m *manager) Remove(ctx context.Context, timelineID string, itemID string) (int, error) {
- return m.getOrCreateTimeline(ctx, timelineID).Remove(ctx, itemID)
-}
-
-func (m *manager) RemoveTimeline(ctx context.Context, timelineID string) error {
- m.timelines.Delete(timelineID)
- return nil
-}
-
-func (m *manager) GetTimeline(ctx context.Context, timelineID string, maxID string, sinceID string, minID string, limit int, local bool) ([]Preparable, error) {
- return m.getOrCreateTimeline(ctx, timelineID).Get(ctx, limit, maxID, sinceID, minID, true)
-}
-
-func (m *manager) GetIndexedLength(ctx context.Context, timelineID string) int {
- return m.getOrCreateTimeline(ctx, timelineID).Len()
-}
-
-func (m *manager) GetOldestIndexedID(ctx context.Context, timelineID string) string {
- return m.getOrCreateTimeline(ctx, timelineID).OldestIndexedItemID()
-}
-
-func (m *manager) WipeItemFromAllTimelines(ctx context.Context, itemID string) error {
- errs := new(gtserror.MultiError)
-
- m.timelines.Range(func(_ any, v any) bool {
- if _, err := v.(Timeline).Remove(ctx, itemID); err != nil {
- errs.Append(err)
- }
-
- return true // always continue range
- })
-
- if err := errs.Combine(); err != nil {
- return gtserror.Newf("error(s) wiping status %s: %w", itemID, errs.Combine())
- }
-
- return nil
-}
-
-func (m *manager) WipeItemsFromAccountID(ctx context.Context, timelineID string, accountID string) error {
- _, err := m.getOrCreateTimeline(ctx, timelineID).RemoveAllByOrBoosting(ctx, accountID)
- return err
-}
-
-func (m *manager) UnprepareItemFromAllTimelines(ctx context.Context, itemID string) error {
- errs := new(gtserror.MultiError)
-
- // Work through all timelines held by this
- // manager, and call Unprepare for each.
- m.timelines.Range(func(_ any, v any) bool {
- if err := v.(Timeline).Unprepare(ctx, itemID); err != nil {
- errs.Append(err)
- }
-
- return true // always continue range
- })
-
- if err := errs.Combine(); err != nil {
- return gtserror.Newf("error(s) unpreparing status %s: %w", itemID, errs.Combine())
- }
-
- return nil
-}
-
-func (m *manager) UnprepareItem(ctx context.Context, timelineID string, itemID string) error {
- return m.getOrCreateTimeline(ctx, timelineID).Unprepare(ctx, itemID)
-}
-
-func (m *manager) Prune(ctx context.Context, timelineID string, desiredPreparedItemsLength int, desiredIndexedItemsLength int) (int, error) {
- return m.getOrCreateTimeline(ctx, timelineID).Prune(desiredPreparedItemsLength, desiredIndexedItemsLength), nil
-}
-
-// getOrCreateTimeline returns a timeline with the given id,
-// creating a new timeline with that id if necessary.
-func (m *manager) getOrCreateTimeline(ctx context.Context, timelineID string) Timeline {
- i, ok := m.timelines.Load(timelineID)
- if ok {
- // Timeline already existed in sync.Map.
- return i.(Timeline)
- }
-
- // Timeline did not yet exist in sync.Map.
- // Create + store it.
- timeline := NewTimeline(ctx, timelineID, m.grabFunction, m.filterFunction, m.prepareFunction, m.skipInsertFunction)
- m.timelines.Store(timelineID, timeline)
-
- return timeline
-}