File tree Expand file tree Collapse file tree 1 file changed +6
-1
lines changed
Expand file tree Collapse file tree 1 file changed +6
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package file
33import (
44 "encoding/json"
55 "errors"
6+ "github.com/astaxie/beego/logs"
67 "os"
78 "path/filepath"
89 "strings"
@@ -151,7 +152,6 @@ func storeSyncMapToFile(m sync.Map, filePath string) {
151152 if err != nil {
152153 panic (err )
153154 }
154- defer file .Close ()
155155 m .Range (func (key , value interface {}) bool {
156156 var b []byte
157157 var err error
@@ -191,6 +191,11 @@ func storeSyncMapToFile(m sync.Map, filePath string) {
191191 return true
192192 })
193193 _ = file .Sync ()
194+ _ = file .Close ()
195+ // must close file first, then rename it
194196 err = os .Rename (filePath + ".tmp" , filePath )
197+ if err != nil {
198+ logs .Error (err , "store to file err, data will lost" )
199+ }
195200 // replace the file, maybe provides atomic operation
196201}
You can’t perform that action at this time.
0 commit comments