start adding preloading support

This commit is contained in:
kim 2025-04-08 14:16:08 +01:00
commit b3c9bfde18
7 changed files with 255 additions and 79 deletions

View file

@ -25,6 +25,7 @@ import (
statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/paging"
)
@ -90,3 +91,35 @@ func (p *Processor) HomeTimelineGet(
},
)
}
// preloadHomeTimeline will ensure that the timeline
// cache for home owned by given account is preloaded.
func (p *Processor) preloadHomeTimeline(
ctx context.Context,
account *gtsmodel.Account,
) error {
// Get (and so, create) home timeline cache for account ID.
timeline := p.state.Caches.Timelines.Home.MustGet(account.ID)
// Preload timeline with funcs.
n, err := timeline.Preload(ctx,
// Database load function.
func(page *paging.Page) ([]*gtsmodel.Status, error) {
return p.state.DB.GetHomeTimeline(ctx, account.ID, page)
},
// Status filtering function.
func(status *gtsmodel.Status) (bool, error) {
ok, err := p.visFilter.StatusHomeTimelineable(ctx, account, status)
return !ok, err
},
)
if err != nil {
return gtserror.Newf("error preloading home timeline %s: %w", account.ID, err)
}
log.Infof(ctx, "%s: preloaded %d", account.Username, n)
return nil
}

View file

@ -27,6 +27,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/paging"
)
@ -103,3 +104,36 @@ func (p *Processor) ListTimelineGet(
},
)
}
// preloadListTimeline will ensure that the timeline
// cache for list owned by given account is preloaded.
func (p *Processor) preloadListTimeline(
ctx context.Context,
account *gtsmodel.Account,
list *gtsmodel.List,
) error {
// Get (and so, create) list timeline cache for list ID.
timeline := p.state.Caches.Timelines.List.MustGet(list.ID)
// Preload timeline with funcs.
n, err := timeline.Preload(ctx,
// Database load function.
func(page *paging.Page) ([]*gtsmodel.Status, error) {
return p.state.DB.GetListTimeline(ctx, list.ID, page)
},
// Status filtering function.
func(status *gtsmodel.Status) (bool, error) {
ok, err := p.visFilter.StatusHomeTimelineable(ctx, account, status)
return !ok, err
},
)
if err != nil {
return gtserror.Newf("error preloading list timeline %s: %w", list.ID, err)
}
log.Infof(ctx, "%s[%q]: preloaded %d", account.Username, list.Title, n)
return nil
}

View file

@ -24,11 +24,12 @@ import (
"net/url"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/cache/timeline"
timelinepkg "github.com/superseriousbusiness/gotosocial/internal/cache/timeline"
"github.com/superseriousbusiness/gotosocial/internal/db"
statusfilter "github.com/superseriousbusiness/gotosocial/internal/filter/status"
"github.com/superseriousbusiness/gotosocial/internal/filter/usermute"
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
@ -61,10 +62,48 @@ func New(state *state.State, converter *typeutils.Converter, visFilter *visibili
}
}
// Preload ...
func (p *Processor) Preload(ctx context.Context) error {
// Get all of our local user accounts.
users, err := p.state.DB.GetAllUsers(ctx)
if err != nil {
return gtserror.Newf("error getting users: %w", err)
}
for _, user := range users {
// Get associated account.
account := user.Account
// Preload this user account's home timeline cache.
if err := p.preloadHomeTimeline(ctx, account); err != nil {
return gtserror.Newf("error preloading home timeline: %w", err)
}
// Get all lists owned by this user account.
lists, err := p.state.DB.GetListsByAccountID(
gtscontext.SetBarebones(ctx),
account.ID,
)
if err != nil {
return gtserror.Newf("error getting account %s lists: %w", account.ID, err)
}
for _, list := range lists {
// Preload each of this user account's list timeline caches.
if err := p.preloadListTimeline(ctx, account, list); err != nil {
return gtserror.Newf("error preloading list timeline: %w", err)
}
}
}
return nil
}
func (p *Processor) getStatusTimeline(
ctx context.Context,
requester *gtsmodel.Account,
cache *timeline.StatusTimeline,
timeline *timelinepkg.StatusTimeline,
page *paging.Page,
pagePath string,
pageQuery url.Values,
@ -128,10 +167,10 @@ func (p *Processor) getStatusTimeline(
return apiStatus, nil
}
if cache != nil {
if timeline != nil {
// Load status page via timeline cache, also
// getting lo, hi values for next, prev pages.
apiStatuses, lo, hi, err = cache.Load(ctx,
apiStatuses, lo, hi, err = timeline.Load(ctx,
// Status page
// to load.
@ -157,7 +196,7 @@ func (p *Processor) getStatusTimeline(
} else {
// Load status page without a receiving timeline cache.
// TODO: remove this code path when all support caching.
apiStatuses, lo, hi, err = timeline.LoadStatusTimeline(ctx,
apiStatuses, lo, hi, err = timelinepkg.LoadStatusTimeline(ctx,
page,
loadPage,
func(ids []string) ([]*gtsmodel.Status, error) {