Skip to content

Commit

Permalink
add config option for deleted characters TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintWish committed Sep 29, 2024
1 parent 6a07dc7 commit 616df52
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Config struct {
Char struct {
MaxBackups int
BackupTime time.Duration
DeletedExpireTime time.Duration
}
Log struct {
Level string
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *Controller) SoftDeleteCharacter(w http.ResponseWriter, r *http.Request)
return
}

if err := c.service.SoftDeleteCharacter(uid); err != nil {
if err := c.service.SoftDeleteCharacter(uid, config.Char.DeletedExpireTime); err != nil {

Check failure on line 153 in internal/controller/internal.go

View workflow job for this annotation

GitHub Actions / build

undefined: config
c.logger.Error("service failed", "error", err)
response.Error(w, err)
return
Expand Down
7 changes: 5 additions & 2 deletions internal/database/badger/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ func (d *badgerDB) LookUpCharacterID(steamid string, slot int) (uuid.UUID, error
return uuid, nil
}

func (d *badgerDB) SoftDeleteCharacter(id uuid.UUID) error {
// We remove the character from user's active list and set an expiration.
func (d *badgerDB) SoftDeleteCharacter(id uuid.UUID, expiration time.Duration) error {
char, err := d.GetCharacter(id)
if err != nil {
return err
Expand Down Expand Up @@ -249,7 +250,9 @@ func (d *badgerDB) SoftDeleteCharacter(id uuid.UUID) error {
return fmt.Errorf("badger: failed to update user %v", err)
}

if err := txn.Set(charKey, charData); err != nil {
charEntry := badger.NewEntry(charKey, charData)
charEntry.WithTTL(expiration)
if err := txn.SetEntry(charEntry); err != nil {
return fmt.Errorf("badger: failed to update character %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/database/bbolt/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (d *bboltDB) LookUpCharacterID(steamid string, slot int) (uuid.UUID, error)
return uuid, nil
}

func (d *bboltDB) SoftDeleteCharacter(id uuid.UUID) error {
func (d *bboltDB) SoftDeleteCharacter(id uuid.UUID, expiration time.Duration) error {
char, err := d.GetCharacter(id)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Database interface {
GetCharacter(id uuid.UUID) (*schema.Character, error)
GetCharacters(steamid string) (map[int]schema.Character, error) //Gotta be a map cause JSON
LookUpCharacterID(steamid string, slot int) (uuid.UUID, error)
SoftDeleteCharacter(id uuid.UUID) error
SoftDeleteCharacter(id uuid.UUID, expiration time.Duration) error
DeleteCharacter(id uuid.UUID) error
DeleteCharacterReference(steamid string, slot int) error
MoveCharacter(id uuid.UUID, steamid string, slot int) error
Expand Down
2 changes: 1 addition & 1 deletion internal/database/mongodb/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (d *mongoDB) LookUpCharacterID(steamid string, slot int) (uuid.UUID, error)
return uuid, nil
}

func (d *mongoDB) SoftDeleteCharacter(id uuid.UUID) error {
func (d *mongoDB) SoftDeleteCharacter(id uuid.UUID, expiration time.Duration) error {
char, err := d.GetCharacter(id)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions internal/service/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"fmt"
"time"

"github.com/msrevive/nexus2/internal/bitmask"
"github.com/msrevive/nexus2/internal/payload"
Expand Down Expand Up @@ -79,8 +80,8 @@ func (s *Service) GetDeletedCharacters(steamid string) (map[int]uuid.UUID, error
return user.DeletedCharacters, nil
}

func (s *Service) SoftDeleteCharacter(uid uuid.UUID) error {
if err := s.db.SoftDeleteCharacter(uid); err != nil {
func (s *Service) SoftDeleteCharacter(uid uuid.UUID, expiration time.Duration) error {
if err := s.db.SoftDeleteCharacter(uid, expiration); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions runtime/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ verify:
char:
maxbackups: 10 # The maximum number of character backups, when limit is reach it will replace the older backup.
backuptime: 1h # How often should the system make a backup of character data.
deletedexpiretime: 0 # How long should the databse keep deleted characters? 0 is forver.
log:
level: debug # Logging level.
dir: ./runtime/logs/ # The directory we should keep all the log files for the FN.
Expand Down

0 comments on commit 616df52

Please sign in to comment.