Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] support configuring database caches #1246

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions example/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,61 @@ db-tls-mode: "disable"
# Default: ""
db-tls-ca-cert: ""

cache:
gts:
###########################
#### DATABASE CACHES ######
###########################
#
# Database cache configuration:
#
# Allows configuration of caches used
# when loading GTS models from the database.
#
# max-size = maximum cached objects count
# ttl = cached object lifetime
# sweep-freq = frequency to look for stale cache objects

account-max-size: 100
account-ttl: "5m"
account-sweep-freq: "10s"

block-max-size: 100
block-ttl: "5m"
block-sweep-freq: "10s"

domain-block-max-size: 1000
domain-block-ttl: "24h"
domain-block-sweep-freq: "1m"

emoji-max-size: 500
emoji-ttl: "5m"
emoji-sweep-freq: "10s"

emoji-category-max-size: 100
emoji-category-ttl: "5m"
emoji-category-sweep-freq: "10s"

mention-max-size: 500
mention-ttl: "5m"
mention-sweep-freq: "10s"

notification-max-size: 500
notification-ttl: "5m"
notification-sweep-freq: "10s"

status-max-size: 500
status-ttl: "5m"
status-sweep-freq: "10s"

tombstone-max-size: 100
tombstone-ttl: "5m"
tombstone-sweep-freq: "10s"

user-max-size: 100
user-ttl: "5m"
user-sweep-freq: "10s"

######################
##### WEB CONFIG #####
######################
Expand Down
63 changes: 31 additions & 32 deletions internal/cache/gts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
package cache

import (
"time"

"codeberg.org/gruf/go-cache/v3/result"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)

Expand Down Expand Up @@ -100,34 +99,34 @@ func (c *gtsCaches) Init() {

func (c *gtsCaches) Start() {
tryUntil("starting gtsmodel.Account cache", 5, func() bool {
return c.account.Start(time.Second * 10)
return c.account.Start(config.GetCacheGTSAccountSweepFreq())
})
tryUntil("starting gtsmodel.Block cache", 5, func() bool {
return c.block.Start(time.Second * 10)
return c.block.Start(config.GetCacheGTSBlockSweepFreq())
})
tryUntil("starting gtsmodel.DomainBlock cache", 5, func() bool {
return c.domainBlock.Start(time.Second * 10)
return c.domainBlock.Start(config.GetCacheGTSDomainBlockSweepFreq())
})
tryUntil("starting gtsmodel.Emoji cache", 5, func() bool {
return c.emoji.Start(time.Second * 10)
return c.emoji.Start(config.GetCacheGTSEmojiSweepFreq())
})
tryUntil("starting gtsmodel.EmojiCategory cache", 5, func() bool {
return c.emojiCategory.Start(time.Second * 10)
return c.emojiCategory.Start(config.GetCacheGTSEmojiCategorySweepFreq())
})
tryUntil("starting gtsmodel.Mention cache", 5, func() bool {
return c.mention.Start(time.Second * 10)
return c.mention.Start(config.GetCacheGTSMentionSweepFreq())
})
tryUntil("starting gtsmodel.Notification cache", 5, func() bool {
return c.notification.Start(time.Second * 10)
return c.notification.Start(config.GetCacheGTSNotificationSweepFreq())
})
tryUntil("starting gtsmodel.Status cache", 5, func() bool {
return c.status.Start(time.Second * 10)
return c.status.Start(config.GetCacheGTSStatusSweepFreq())
})
tryUntil("starting gtsmodel.Tombstone cache", 5, func() bool {
return c.tombstone.Start(time.Second * 10)
return c.tombstone.Start(config.GetCacheGTSTombstoneSweepFreq())
})
tryUntil("starting gtsmodel.User cache", 5, func() bool {
return c.user.Start(time.Second * 10)
return c.user.Start(config.GetCacheGTSUserSweepFreq())
})
}

Expand Down Expand Up @@ -195,8 +194,8 @@ func (c *gtsCaches) initAccount() {
a2 := new(gtsmodel.Account)
*a2 = *a1
return a2
}, 1000)
c.account.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSAccountMaxSize())
c.account.SetTTL(config.GetCacheGTSAccountTTL(), true)
}

func (c *gtsCaches) initBlock() {
Expand All @@ -208,8 +207,8 @@ func (c *gtsCaches) initBlock() {
b2 := new(gtsmodel.Block)
*b2 = *b1
return b2
}, 1000)
c.block.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSBlockMaxSize())
c.block.SetTTL(config.GetCacheGTSBlockTTL(), true)
}

func (c *gtsCaches) initDomainBlock() {
Expand All @@ -219,8 +218,8 @@ func (c *gtsCaches) initDomainBlock() {
d2 := new(gtsmodel.DomainBlock)
*d2 = *d1
return d2
}, 1000)
c.domainBlock.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSDomainBlockMaxSize())
c.domainBlock.SetTTL(config.GetCacheGTSDomainBlockTTL(), true)
}

func (c *gtsCaches) initEmoji() {
Expand All @@ -233,8 +232,8 @@ func (c *gtsCaches) initEmoji() {
e2 := new(gtsmodel.Emoji)
*e2 = *e1
return e2
}, 1000)
c.emoji.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSEmojiMaxSize())
c.emoji.SetTTL(config.GetCacheGTSEmojiTTL(), true)
}

func (c *gtsCaches) initEmojiCategory() {
Expand All @@ -245,8 +244,8 @@ func (c *gtsCaches) initEmojiCategory() {
c2 := new(gtsmodel.EmojiCategory)
*c2 = *c1
return c2
}, 1000)
c.emojiCategory.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSEmojiCategoryMaxSize())
c.emojiCategory.SetTTL(config.GetCacheGTSEmojiCategoryTTL(), true)
}

func (c *gtsCaches) initMention() {
Expand All @@ -256,8 +255,8 @@ func (c *gtsCaches) initMention() {
m2 := new(gtsmodel.Mention)
*m2 = *m1
return m2
}, 1000)
c.mention.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSMentionMaxSize())
c.mention.SetTTL(config.GetCacheGTSMentionTTL(), true)
}

func (c *gtsCaches) initNotification() {
Expand All @@ -267,8 +266,8 @@ func (c *gtsCaches) initNotification() {
n2 := new(gtsmodel.Notification)
*n2 = *n1
return n2
}, 1000)
c.notification.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSNotificationMaxSize())
c.notification.SetTTL(config.GetCacheGTSNotificationTTL(), true)
}

func (c *gtsCaches) initStatus() {
Expand All @@ -280,8 +279,8 @@ func (c *gtsCaches) initStatus() {
s2 := new(gtsmodel.Status)
*s2 = *s1
return s2
}, 1000)
c.status.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSStatusMaxSize())
c.status.SetTTL(config.GetCacheGTSStatusTTL(), true)
}

// initTombstone will initialize the gtsmodel.Tombstone cache.
Expand All @@ -293,8 +292,8 @@ func (c *gtsCaches) initTombstone() {
t2 := new(gtsmodel.Tombstone)
*t2 = *t1
return t2
}, 100)
c.tombstone.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSTombstoneMaxSize())
c.tombstone.SetTTL(config.GetCacheGTSTombstoneTTL(), true)
}

func (c *gtsCaches) initUser() {
Expand All @@ -308,6 +307,6 @@ func (c *gtsCaches) initUser() {
u2 := new(gtsmodel.User)
*u2 = *u1
return u2
}, 1000)
c.user.SetTTL(time.Minute*5, false)
}, config.GetCacheGTSUserMaxSize())
c.user.SetTTL(config.GetCacheGTSUserTTL(), true)
}
52 changes: 51 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package config

import (
"reflect"
"time"

"codeberg.org/gruf/go-bytesize"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -129,6 +130,9 @@ type Configuration struct {
AdvancedCookiesSamesite string `name:"advanced-cookies-samesite" usage:"'strict' or 'lax', see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite"`
AdvancedRateLimitRequests int `name:"advanced-rate-limit-requests" usage:"Amount of HTTP requests to permit within a 5 minute window. 0 or less turns rate limiting off."`

// Cache configuration vars.
Cache CacheConfiguration `name:"cache"`

// TODO: move these elsewhere, these are more ephemeral vs long-running flags like above
AdminAccountUsername string `name:"username" usage:"the username to create/delete/etc"`
AdminAccountEmail string `name:"email" usage:"the email address of this account"`
Expand All @@ -137,7 +141,53 @@ type Configuration struct {
AdminMediaPruneDryRun bool `name:"dry-run" usage:"perform a dry run and only log number of items eligible for pruning"`
}

// MarshalMap will marshal current Configuration into a map structure (useful for JSON).
type CacheConfiguration struct {
GTS GTSCacheConfiguration `name:"gts"`
}

type GTSCacheConfiguration struct {
AccountMaxSize int `name:"account-max-size"`
AccountTTL time.Duration `name:"account-ttl"`
AccountSweepFreq time.Duration `name:"account-sweep-freq"`

BlockMaxSize int `name:"block-max-size"`
BlockTTL time.Duration `name:"block-ttl"`
BlockSweepFreq time.Duration `name:"block-sweep-freq"`

DomainBlockMaxSize int `name:"domain-block-max-size"`
DomainBlockTTL time.Duration `name:"domain-block-ttl"`
DomainBlockSweepFreq time.Duration `name:"domain-block-sweep-freq"`

EmojiMaxSize int `name:"emoji-max-size"`
EmojiTTL time.Duration `name:"emoji-ttl"`
EmojiSweepFreq time.Duration `name:"emoji-sweep-freq"`

EmojiCategoryMaxSize int `name:"emoji-category-max-size"`
EmojiCategoryTTL time.Duration `name:"emoji-category-ttl"`
EmojiCategorySweepFreq time.Duration `name:"emoji-category-sweep-freq"`

MentionMaxSize int `name:"mention-max-size"`
MentionTTL time.Duration `name:"mention-ttl"`
MentionSweepFreq time.Duration `name:"mention-sweep-freq"`

NotificationMaxSize int `name:"notification-max-size"`
NotificationTTL time.Duration `name:"notification-ttl"`
NotificationSweepFreq time.Duration `name:"notification-sweep-freq"`

StatusMaxSize int `name:"status-max-size"`
StatusTTL time.Duration `name:"status-ttl"`
StatusSweepFreq time.Duration `name:"status-sweep-freq"`

TombstoneMaxSize int `name:"tombstone-max-size"`
TombstoneTTL time.Duration `name:"tombstone-ttl"`
TombstoneSweepFreq time.Duration `name:"tombstone-sweep-freq"`

UserMaxSize int `name:"user-max-size"`
UserTTL time.Duration `name:"user-ttl"`
UserSweepFreq time.Duration `name:"user-sweep-freq"`
}

// MarshalMap will marshal current Configuration into a map structure (useful for JSON/TOML/YAML).
func (cfg *Configuration) MarshalMap() (map[string]interface{}, error) {
var dst map[string]interface{}
dec, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Expand Down
File renamed without changes.
59 changes: 54 additions & 5 deletions internal/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

package config

import "github.com/coreos/go-oidc/v3/oidc"
import (
"time"

"codeberg.org/gruf/go-bytesize"
"github.com/coreos/go-oidc/v3/oidc"
)

// Defaults contains a populated Configuration with reasonable defaults. Note that
// if you use this, you will still need to set Host, and, if desired, ConfigPath.
Expand Down Expand Up @@ -56,13 +61,13 @@ var Defaults = Configuration{
AccountsReasonRequired: true,
AccountsAllowCustomCSS: false,

MediaImageMaxSize: 10485760, // 10mb
MediaVideoMaxSize: 41943040, // 40mb
MediaImageMaxSize: 10 * bytesize.MiB,
MediaVideoMaxSize: 40 * bytesize.MiB,
MediaDescriptionMinChars: 0,
MediaDescriptionMaxChars: 500,
MediaRemoteCacheDays: 30,
MediaEmojiLocalMaxSize: 51200, // 50kb
MediaEmojiRemoteMaxSize: 102400, // 100kb
MediaEmojiLocalMaxSize: 50 * bytesize.KiB,
MediaEmojiRemoteMaxSize: 100 * bytesize.KiB,

StorageBackend: "local",
StorageLocalBasePath: "/gotosocial/storage",
Expand Down Expand Up @@ -101,4 +106,48 @@ var Defaults = Configuration{

AdvancedCookiesSamesite: "lax",
AdvancedRateLimitRequests: 1000, // per 5 minutes

Cache: CacheConfiguration{
GTS: GTSCacheConfiguration{
AccountMaxSize: 100,
AccountTTL: time.Minute * 5,
AccountSweepFreq: time.Second * 10,

BlockMaxSize: 100,
BlockTTL: time.Minute * 5,
BlockSweepFreq: time.Second * 10,

DomainBlockMaxSize: 1000,
DomainBlockTTL: time.Hour * 24,
DomainBlockSweepFreq: time.Minute,

EmojiMaxSize: 500,
EmojiTTL: time.Minute * 5,
EmojiSweepFreq: time.Second * 10,

EmojiCategoryMaxSize: 100,
EmojiCategoryTTL: time.Minute * 5,
EmojiCategorySweepFreq: time.Second * 10,

MentionMaxSize: 500,
MentionTTL: time.Minute * 5,
MentionSweepFreq: time.Second * 10,

NotificationMaxSize: 500,
NotificationTTL: time.Minute * 5,
NotificationSweepFreq: time.Second * 10,

StatusMaxSize: 500,
StatusTTL: time.Minute * 5,
StatusSweepFreq: time.Second * 10,

TombstoneMaxSize: 100,
TombstoneTTL: time.Minute * 5,
TombstoneSweepFreq: time.Second * 10,

UserMaxSize: 100,
UserTTL: time.Minute * 5,
UserSweepFreq: time.Second * 10,
},
},
}
Loading