Skip to content

Commit cd6bc02

Browse files
committed
[feature] Compress backup file
1 parent 708ee56 commit cd6bc02

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ yarn-error.log*
2424
# app-specific ignores
2525
history.json
2626
gc-state.json
27-
gc-state.backup
27+
gc-state.backup.gz

internal/app/controller/engineHistory.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controller
22

33
import (
4+
"bufio"
5+
"compress/gzip"
46
"encoding/json"
57
"github.com/pkg/errors"
68
"io/ioutil"
@@ -12,7 +14,7 @@ import (
1214

1315
const maxHistorySize = 10
1416
const stateFilename = "gc-state.json"
15-
const backupFilename = "gc-state.backup"
17+
const backupFilename = "gc-state.backup.gz"
1618

1719
type PersistentState struct {
1820
CurrentState *GameControllerState `json:"currentState"`
@@ -70,8 +72,10 @@ func (s *PersistentState) RevertProtocolEntry(id string) error {
7072
}
7173

7274
type StatePreserver struct {
73-
file *os.File
74-
backupFile *os.File
75+
file *os.File
76+
backupFile *os.File
77+
backupWriter *bufio.Writer
78+
backupWriterGzip *gzip.Writer
7579
}
7680

7781
// Open opens the state and backup file
@@ -87,6 +91,8 @@ func (r *StatePreserver) Open() error {
8791
return err
8892
}
8993
r.backupFile = f
94+
r.backupWriterGzip = gzip.NewWriter(r.backupFile)
95+
r.backupWriter = bufio.NewWriter(r.backupWriterGzip)
9096
return nil
9197
}
9298

@@ -110,6 +116,12 @@ func (r *StatePreserver) Close() {
110116
}
111117
}
112118
if r.backupFile != nil {
119+
if err := r.backupWriter.Flush(); err != nil {
120+
log.Print("Could not flush backup writer: ", err)
121+
}
122+
if err := r.backupWriterGzip.Close(); err != nil {
123+
log.Print("Could not close backup writer: ", err)
124+
}
113125
if err := r.backupFile.Close(); err != nil {
114126
log.Print("Could not close backup file", err)
115127
}
@@ -160,8 +172,11 @@ func (r *StatePreserver) Save(state *PersistentState) {
160172
return
161173
}
162174
jsonCompact = append(jsonCompact, []byte("\n")...)
163-
_, err = r.backupFile.Write(jsonCompact)
175+
_, err = r.backupWriter.Write(jsonCompact)
164176
if err != nil {
165177
log.Print("Could not write to backup file: ", err)
166178
}
179+
if err := r.backupWriter.Flush(); err != nil {
180+
log.Print("Could not flush backup writer: ", err)
181+
}
167182
}

0 commit comments

Comments
 (0)