mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-23 01:46:16 -06:00
remove tryUntil
This commit is contained in:
parent
4580ae194c
commit
be85a5b642
7 changed files with 30 additions and 43 deletions
|
|
@ -38,7 +38,9 @@ import (
|
||||||
func initState(ctx context.Context) (*state.State, error) {
|
func initState(ctx context.Context) (*state.State, error) {
|
||||||
var state state.State
|
var state state.State
|
||||||
state.Caches.Init()
|
state.Caches.Init()
|
||||||
state.Caches.Start()
|
if err := state.Caches.Start(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error starting caches: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Only set state DB connection.
|
// Only set state DB connection.
|
||||||
// Don't need Actions or Workers for this (yet).
|
// Don't need Actions or Workers for this (yet).
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,9 @@ func setupList(ctx context.Context) (*list, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
state.Caches.Init()
|
state.Caches.Init()
|
||||||
state.Caches.Start()
|
if err := state.Caches.Start(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error starting caches: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Only set state DB connection.
|
// Only set state DB connection.
|
||||||
// Don't need Actions or Workers for this.
|
// Don't need Actions or Workers for this.
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,9 @@ func setupPrune(ctx context.Context) (*prune, error) {
|
||||||
var state state.State
|
var state state.State
|
||||||
|
|
||||||
state.Caches.Init()
|
state.Caches.Init()
|
||||||
state.Caches.Start()
|
if err := state.Caches.Start(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error starting caches: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Scheduler is required for the
|
// Scheduler is required for the
|
||||||
// cleaner, but no other workers
|
// cleaner, but no other workers
|
||||||
|
|
|
||||||
|
|
@ -118,11 +118,10 @@ var Start action.GTSAction = func(ctx context.Context) error {
|
||||||
)
|
)
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if state.Caches.Inited() {
|
// Stop any started caches.
|
||||||
// We reached a point where caches
|
//
|
||||||
// were initialized. Stop them.
|
// Noop if never started.
|
||||||
state.Caches.Stop()
|
state.Caches.Stop()
|
||||||
}
|
|
||||||
|
|
||||||
if route != nil {
|
if route != nil {
|
||||||
// We reached a point where the API router
|
// We reached a point where the API router
|
||||||
|
|
@ -207,7 +206,9 @@ var Start action.GTSAction = func(ctx context.Context) error {
|
||||||
|
|
||||||
// Initialize caches
|
// Initialize caches
|
||||||
state.Caches.Init()
|
state.Caches.Init()
|
||||||
state.Caches.Start()
|
if err := state.Caches.Start(); err != nil {
|
||||||
|
return fmt.Errorf("error starting caches: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Open connection to the database now caches started.
|
// Open connection to the database now caches started.
|
||||||
dbService, err := bundb.NewBunDBService(ctx, state)
|
dbService, err := bundb.NewBunDBService(ctx, state)
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,9 @@ func (suite *ListsStandardTestSuite) SetupSuite() {
|
||||||
|
|
||||||
func (suite *ListsStandardTestSuite) SetupTest() {
|
func (suite *ListsStandardTestSuite) SetupTest() {
|
||||||
suite.state.Caches.Init()
|
suite.state.Caches.Init()
|
||||||
suite.state.Caches.Start()
|
if err := suite.state.Caches.Start(); err != nil {
|
||||||
|
panic("error starting caches: " + err.Error())
|
||||||
|
}
|
||||||
testrig.StartNoopWorkers(&suite.state)
|
testrig.StartNoopWorkers(&suite.state)
|
||||||
|
|
||||||
testrig.InitTestConfig()
|
testrig.InitTestConfig()
|
||||||
|
|
|
||||||
26
internal/cache/cache.go
vendored
26
internal/cache/cache.go
vendored
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"codeberg.org/gruf/go-cache/v3/ttl"
|
"codeberg.org/gruf/go-cache/v3/ttl"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/cache/headerfilter"
|
"github.com/superseriousbusiness/gotosocial/internal/cache/headerfilter"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||||
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/log"
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -124,23 +125,18 @@ func (c *Caches) Init() {
|
||||||
|
|
||||||
// Start will start any caches that require a background
|
// Start will start any caches that require a background
|
||||||
// routine, which usually means any kind of TTL caches.
|
// routine, which usually means any kind of TTL caches.
|
||||||
func (c *Caches) Start() {
|
func (c *Caches) Start() error {
|
||||||
log.Infof(nil, "start: %p", c)
|
log.Infof(nil, "start: %p", c)
|
||||||
|
|
||||||
tryUntil("starting webfinger cache", 5, func() bool {
|
if !c.Webfinger.Start(5 * time.Minute) {
|
||||||
return c.Webfinger.Start(5 * time.Minute)
|
return gtserror.New("could not start webfinger cache")
|
||||||
})
|
|
||||||
|
|
||||||
tryUntil("starting statusesFilterableFields cache", 5, func() bool {
|
|
||||||
return c.StatusesFilterableFields.Start(5 * time.Minute)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inited returns true if the
|
if !c.StatusesFilterableFields.Start(5 * time.Minute) {
|
||||||
// caches have been initialized.
|
return gtserror.New("could not start statusesFilterableFields cache")
|
||||||
func (c *Caches) Inited() bool {
|
}
|
||||||
// Use nilness of *ttl.Cache pointers as heuristic.
|
|
||||||
return c.Webfinger != nil && c.StatusesFilterableFields != nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop will stop any caches that require a background
|
// Stop will stop any caches that require a background
|
||||||
|
|
@ -148,8 +144,8 @@ func (c *Caches) Inited() bool {
|
||||||
func (c *Caches) Stop() {
|
func (c *Caches) Stop() {
|
||||||
log.Infof(nil, "stop: %p", c)
|
log.Infof(nil, "stop: %p", c)
|
||||||
|
|
||||||
tryUntil("stopping webfinger cache", 5, c.Webfinger.Stop)
|
_ = c.Webfinger.Stop()
|
||||||
tryUntil("stopping statusesFilterableFields cache", 5, c.StatusesFilterableFields.Stop)
|
_ = c.StatusesFilterableFields.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sweep will sweep all the available caches to ensure none
|
// Sweep will sweep all the available caches to ensure none
|
||||||
|
|
|
||||||
18
internal/cache/util.go
vendored
18
internal/cache/util.go
vendored
|
|
@ -19,11 +19,9 @@ package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
|
||||||
|
|
||||||
errorsv2 "codeberg.org/gruf/go-errors/v2"
|
errorsv2 "codeberg.org/gruf/go-errors/v2"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SentinelError is an error that can be returned and checked against to indicate a non-permanent
|
// SentinelError is an error that can be returned and checked against to indicate a non-permanent
|
||||||
|
|
@ -51,19 +49,3 @@ type nocopy struct{}
|
||||||
func (*nocopy) Lock() {}
|
func (*nocopy) Lock() {}
|
||||||
|
|
||||||
func (*nocopy) Unlock() {}
|
func (*nocopy) Unlock() {}
|
||||||
|
|
||||||
// tryUntil will attempt to call 'do' for 'count' attempts, before panicking with 'msg'.
|
|
||||||
func tryUntil(msg string, count int, do func() bool) {
|
|
||||||
for i := 0; i < count; i++ {
|
|
||||||
if do() {
|
|
||||||
// success.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sleep for a little before retry (a bcakoff).
|
|
||||||
time.Sleep(time.Millisecond * 1 << (i + 1))
|
|
||||||
}
|
|
||||||
|
|
||||||
// panic on total failure as this shouldn't happen.
|
|
||||||
log.Panicf(nil, "failed %s after %d tries", msg, count)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue