mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2026-01-06 03:23:16 -06:00
don't use cache (overkill)
This commit is contained in:
parent
3991f60e83
commit
bde5520cc0
2 changed files with 45 additions and 42 deletions
|
|
@ -17,7 +17,10 @@
|
||||||
|
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import "mime/multipart"
|
import (
|
||||||
|
"mime/multipart"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// InstanceSettingsUpdateRequest models an instance update request.
|
// InstanceSettingsUpdateRequest models an instance update request.
|
||||||
//
|
//
|
||||||
|
|
@ -154,4 +157,5 @@ type RandomStats struct {
|
||||||
Statuses int64
|
Statuses int64
|
||||||
TotalUsers int64
|
TotalUsers int64
|
||||||
MonthlyActiveUsers int64
|
MonthlyActiveUsers int64
|
||||||
|
Generated time.Time
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,10 @@ import (
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"codeberg.org/gruf/go-cache/v3"
|
|
||||||
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
|
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
|
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
|
||||||
"github.com/superseriousbusiness/gotosocial/internal/log"
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
||||||
|
|
@ -39,41 +38,43 @@ type Converter struct {
|
||||||
randAvatars sync.Map
|
randAvatars sync.Map
|
||||||
visFilter *visibility.Filter
|
visFilter *visibility.Filter
|
||||||
intFilter *interaction.Filter
|
intFilter *interaction.Filter
|
||||||
randStatsCache cache.TTLCache[struct{}, apimodel.RandomStats]
|
randStats atomic.Pointer[apimodel.RandomStats]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConverter(state *state.State) *Converter {
|
func NewConverter(state *state.State) *Converter {
|
||||||
// If randomizing instance stats, create a cache so we
|
|
||||||
// don't have to generate them every time, and set a 1hr
|
|
||||||
// ttl on the cache so they're not always the same.
|
|
||||||
var randStatsCache cache.TTLCache[struct{}, apimodel.RandomStats]
|
|
||||||
if config.GetInstanceStatsRandomize() {
|
|
||||||
randStatsCache = cache.NewTTL[struct{}, apimodel.RandomStats](0, 1, 0)
|
|
||||||
randStatsCache.SetTTL(time.Hour, false)
|
|
||||||
if !randStatsCache.Start(time.Minute) {
|
|
||||||
log.Panicf(nil, "couldn't start randStatsCache")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Converter{
|
return &Converter{
|
||||||
state: state,
|
state: state,
|
||||||
defaultAvatars: populateDefaultAvatars(),
|
defaultAvatars: populateDefaultAvatars(),
|
||||||
visFilter: visibility.NewFilter(state),
|
visFilter: visibility.NewFilter(state),
|
||||||
intFilter: interaction.NewFilter(state),
|
intFilter: interaction.NewFilter(state),
|
||||||
randStatsCache: randStatsCache,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomStats returns or generates
|
// RandomStats returns or generates
|
||||||
// and returns random instance stats.
|
// and returns random instance stats.
|
||||||
func (c *Converter) RandomStats() apimodel.RandomStats {
|
func (c *Converter) RandomStats() apimodel.RandomStats {
|
||||||
|
now := time.Now()
|
||||||
|
stats := c.randStats.Load()
|
||||||
|
if stats != nil && time.Since(stats.Generated) < time.Hour {
|
||||||
|
// Random stats are still
|
||||||
|
// fresh (less than 1hr old),
|
||||||
|
// so return them as-is.
|
||||||
|
return *stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate new random stats.
|
||||||
|
newStats := genRandStats()
|
||||||
|
newStats.Generated = now
|
||||||
|
c.randStats.Store(&newStats)
|
||||||
|
return newStats
|
||||||
|
}
|
||||||
|
|
||||||
|
func genRandStats() apimodel.RandomStats {
|
||||||
const (
|
const (
|
||||||
statusesMax = 10000000
|
statusesMax = 10000000
|
||||||
usersMax = 1000000
|
usersMax = 1000000
|
||||||
)
|
)
|
||||||
|
|
||||||
stats, ok := c.randStatsCache.Get(struct{}{})
|
|
||||||
if !ok {
|
|
||||||
statusesB, err := crand.Int(crand.Reader, big.NewInt(statusesMax))
|
statusesB, err := crand.Int(crand.Reader, big.NewInt(statusesMax))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Only errs if something is buggered with the OS.
|
// Only errs if something is buggered with the OS.
|
||||||
|
|
@ -86,17 +87,15 @@ func (c *Converter) RandomStats() apimodel.RandomStats {
|
||||||
log.Panicf(nil, "error randomly generating users count: %v", err)
|
log.Panicf(nil, "error randomly generating users count: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Monthly users should only ever
|
||||||
|
// be <= 100% of total users.
|
||||||
totalUsers := totalUsersB.Int64()
|
totalUsers := totalUsersB.Int64()
|
||||||
activeRatio := rand.Float64() //nolint
|
activeRatio := rand.Float64() //nolint
|
||||||
mau := int64(float64(totalUsers) * activeRatio)
|
mau := int64(float64(totalUsers) * activeRatio)
|
||||||
|
|
||||||
stats = apimodel.RandomStats{
|
return apimodel.RandomStats{
|
||||||
Statuses: statusesB.Int64(),
|
Statuses: statusesB.Int64(),
|
||||||
TotalUsers: totalUsers,
|
TotalUsers: totalUsers,
|
||||||
MonthlyActiveUsers: mau,
|
MonthlyActiveUsers: mau,
|
||||||
}
|
}
|
||||||
c.randStatsCache.Set(struct{}{}, stats)
|
|
||||||
}
|
|
||||||
|
|
||||||
return stats
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue