Skip to content

Commit

Permalink
make data retention intervals configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Aug 4, 2024
1 parent 6992ed1 commit 62df8ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ type Config struct {
FollowersSyncBatchSize int
FollowersSyncInterval time.Duration

NotesTTL time.Duration
DeliveryTTL time.Duration
SharesTTL time.Duration
ActorTTL time.Duration
NotesTTL time.Duration
InvisiblePostsTTL time.Duration
DeliveryTTL time.Duration
SharesTTL time.Duration
ActorTTL time.Duration
}

// FillDefaults replaces missing or invalid settings with defaults.
Expand Down Expand Up @@ -367,6 +368,10 @@ func (c *Config) FillDefaults() {
c.NotesTTL = time.Hour * 24 * 30
}

if c.InvisiblePostsTTL <= 0 {
c.InvisiblePostsTTL = time.Hour * 24 * 14
}

if c.DeliveryTTL <= 0 {
c.DeliveryTTL = time.Hour * 24 * 7
}
Expand Down
8 changes: 4 additions & 4 deletions data/garbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ type GarbageCollector struct {
func (gc *GarbageCollector) Run(ctx context.Context) error {
now := time.Now()

if _, err := gc.DB.ExecContext(ctx, `delete from notesfts where id in (select notes.id from notes left join follows on follows.followed in (notes.author, notes.cc0, notes.to0, notes.cc1, notes.to1, notes.cc2, notes.to2) or (notes.to2 is not null and exists (select 1 from json_each(notes.object->'$.to') where value = follows.followed)) or (notes.cc2 is not null and exists (select 1 from json_each(notes.object->'$.cc') where value = follows.followed)) where follows.accepted = 1 and notes.inserted < unixepoch()-60*60*24 and notes.host != ? and follows.id is null)`, gc.Domain); err != nil {
if _, err := gc.DB.ExecContext(ctx, `delete from notesfts where id in (select notes.id from notes left join follows on follows.followed in (notes.author, notes.cc0, notes.to0, notes.cc1, notes.to1, notes.cc2, notes.to2) or (notes.to2 is not null and exists (select 1 from json_each(notes.object->'$.to') where value = follows.followed)) or (notes.cc2 is not null and exists (select 1 from json_each(notes.object->'$.cc') where value = follows.followed)) where follows.accepted = 1 and notes.inserted < $1 and notes.host != $2 and follows.id is null)`, now.Add(-gc.Config.InvisiblePostsTTL).Unix(), gc.Domain); err != nil {
return fmt.Errorf("failed to remove invisible posts: %w", err)
}

if _, err := gc.DB.ExecContext(ctx, `delete from notes where id in (select notes.id from notes left join follows on follows.followed in (notes.author, notes.cc0, notes.to0, notes.cc1, notes.to1, notes.cc2, notes.to2) or (notes.to2 is not null and exists (select 1 from json_each(notes.object->'$.to') where value = follows.followed)) or (notes.cc2 is not null and exists (select 1 from json_each(notes.object->'$.cc') where value = follows.followed)) where follows.accepted = 1 and notes.inserted < unixepoch()-60*60*24 and notes.host != ? and follows.id is null)`, gc.Domain); err != nil {
if _, err := gc.DB.ExecContext(ctx, `delete from notes where id in (select notes.id from notes left join follows on follows.followed in (notes.author, notes.cc0, notes.to0, notes.cc1, notes.to1, notes.cc2, notes.to2) or (notes.to2 is not null and exists (select 1 from json_each(notes.object->'$.to') where value = follows.followed)) or (notes.cc2 is not null and exists (select 1 from json_each(notes.object->'$.cc') where value = follows.followed)) where follows.accepted = 1 and notes.inserted < $1 and notes.host != $2 and follows.id is null)`, now.Add(-gc.Config.InvisiblePostsTTL).Unix(), gc.Domain); err != nil {
return fmt.Errorf("failed to remove invisible posts: %w", err)
}

if _, err := gc.DB.ExecContext(ctx, `delete from notesfts where id in (select id from notes where inserted < unixepoch()-60*60*24*7 and author not in (select followed from follows where accepted = 1) and host != ?)`, gc.Domain); err != nil {
if _, err := gc.DB.ExecContext(ctx, `delete from notesfts where id in (select id from notes where inserted < $1 and author not in (select followed from follows where accepted = 1) and host != $2)`, now.Add(-gc.Config.InvisiblePostsTTL).Unix(), gc.Domain); err != nil {
return fmt.Errorf("failed to remove posts by authors without followers: %w", err)
}

if _, err := gc.DB.ExecContext(ctx, `delete from notes where inserted < unixepoch()-60*60*24*7 and author not in (select followed from follows where accepted = 1) and host != ?`, gc.Domain); err != nil {
if _, err := gc.DB.ExecContext(ctx, `delete from notes where inserted < $1 and author not in (select followed from follows where accepted = 1) and host != $2`, now.Add(-gc.Config.InvisiblePostsTTL).Unix(), gc.Domain); err != nil {
return fmt.Errorf("failed to remove posts by authors without followers: %w", err)
}

Expand Down

0 comments on commit 62df8ac

Please sign in to comment.