Skip to content

Commit

Permalink
imp(backup): 优化了备份时的内存占用
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Dec 29, 2024
1 parent 169a113 commit fa4ad61
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
14 changes: 12 additions & 2 deletions dice/dice_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -125,7 +126,8 @@ func (dm *DiceManager) Backup(sel BackupSelection, fromAuto bool) (string, error
}

backup := func(d *Dice, fn string) {
data, err := os.ReadFile(fn)
file, err := os.Open(fn)
defer file.Close()

Check failure on line 130 in dice/dice_backup.go

View workflow job for this annotation

GitHub Actions / Test & Lint

SA5001: should check error returned from os.Open() before deferring file.Close() (staticcheck)

Check failure on line 130 in dice/dice_backup.go

View workflow job for this annotation

GitHub Actions / Review Dog

SA5001: should check error returned from os.Open() before deferring file.Close() (staticcheck)
if err != nil && !strings.Contains(fn, "session.token") {
if d != nil {
d.Logger.Errorf("备份文件失败: %s, 原因: %s", fn, err.Error())
Expand All @@ -145,7 +147,15 @@ func (dm *DiceManager) Backup(sel BackupSelection, fromAuto bool) (string, error
}
return
}
_, _ = fileWriter.Write(data)

_, err = io.Copy(fileWriter, file)
if err != nil {
if d != nil {
d.Logger.Errorf("备份文件失败: %s, 原因: %s", fn, err.Error())
} else {
logger.Errorf("备份文件失败: %s, 原因: %s", fn, err.Error())
}
}
}

backupDir := func(path string, info fs.FileInfo, _ error) error {
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ require (
github.com/blevesearch/bleve_index_api v1.1.13
github.com/go-gorm/caches/v4 v4.0.5
github.com/gofrs/flock v0.12.1
github.com/gonutz/w32/v2 v2.11.1
github.com/joho/godotenv v1.5.1
github.com/spaolacci/murmur3 v1.1.0
github.com/tidwall/gjson v1.17.0
github.com/tidwall/sjson v1.2.5
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.11
moul.io/zapfilter v1.7.0
)

Expand Down Expand Up @@ -145,7 +149,6 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
Expand Down Expand Up @@ -192,8 +195,6 @@ require (
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/postgres v1.5.11 // indirect
modernc.org/libc v1.37.6 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gonutz/w32/v2 v2.11.1 h1:plG738ZY7VIkPGf3adZ6lFeAf2evCKrULKyZT5GrPoc=
github.com/gonutz/w32/v2 v2.11.1/go.mod h1:MgtHx0AScDVNKyB+kjyPder4xIi3XAcHS6LDDU2DmdE=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -229,7 +231,6 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
Expand Down

0 comments on commit fa4ad61

Please sign in to comment.