aboutsummaryrefslogtreecommitdiff
path: root/yamldb
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-11-12 18:50:25 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-11-12 18:50:25 +0300
commita5c97d68e77ca8c9c5518051f085051487cd0fbf (patch)
tree75e94674574fc613090bd2f05c88ec9d7f8d751d /yamldb
parentfbe99c65ec36bb0d8b3a508e0894a9830ff9c995 (diff)
Spawn TDlib instances for recovered sessions
Diffstat (limited to 'yamldb')
-rw-r--r--yamldb/yamldb.go32
1 files changed, 16 insertions, 16 deletions
diff --git a/yamldb/yamldb.go b/yamldb/yamldb.go
index eec23be..1478cc7 100644
--- a/yamldb/yamldb.go
+++ b/yamldb/yamldb.go
@@ -18,9 +18,7 @@ type YamlDB struct {
// Transaction executes the given callback and safely saves
// the data after they are modified within the callback
-func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)) error {
- var err error
-
+func (db *YamlDB) Transaction(callback func() bool, marshaller func() ([]byte, error)) error {
log.Debug("Enter transaction")
db.lock.Lock()
defer func() {
@@ -28,19 +26,21 @@ func (db *YamlDB) Transaction(callback func(), marshaller func() ([]byte, error)
log.Debug("Exit transaction")
}()
- callback()
-
- yamlData, err := marshaller()
- if err != nil {
- return errors.Wrap(err, "Data marshalling error")
- }
- err = ioutil.WriteFile(db.PathNew, yamlData, 0644)
- if err != nil {
- return errors.Wrap(err, "YamlDB write failure")
- }
- err = os.Rename(db.PathNew, db.Path)
- if err != nil {
- return errors.Wrap(err, "Couldn't rewrite an old YamlDB file")
+ isDataChanged := callback()
+
+ if isDataChanged {
+ yamlData, err := marshaller()
+ if err != nil {
+ return errors.Wrap(err, "Data marshalling error")
+ }
+ err = ioutil.WriteFile(db.PathNew, yamlData, 0644)
+ if err != nil {
+ return errors.Wrap(err, "YamlDB write failure")
+ }
+ err = os.Rename(db.PathNew, db.Path)
+ if err != nil {
+ return errors.Wrap(err, "Couldn't rewrite an old YamlDB file")
+ }
}
return nil