diff --git a/cmd/mtredisalize/changetracker.go b/cmd/mtredisalize/changetracker.go index 9f90d86..6024e97 100644 --- a/cmd/mtredisalize/changetracker.go +++ b/cmd/mtredisalize/changetracker.go @@ -39,15 +39,13 @@ func (ct *ChangeTracker) BlockChanged(key []byte) { func (ct *ChangeTracker) FlushChanges(url string) (err error) { log.Println("change flush triggered") var oldChanges map[quantizedXZ]bool - var sendNotification bool ct.mutex.Lock() - oldChanges = ct.changes - if len(oldChanges) > 0 { - sendNotification = true + if len(ct.changes) > 0 { + oldChanges = ct.changes ct.changes = make(map[quantizedXZ]bool) } ct.mutex.Unlock() - if !sendNotification { + if oldChanges == nil { return } go func() { @@ -57,15 +55,14 @@ func (ct *ChangeTracker) FlushChanges(url string) (err error) { changes[i] = change i++ } - var err error - var encoded []byte - if encoded, err = json.Marshal(changes); err != nil { + var buf bytes.Buffer + encoder := json.NewEncoder(&buf) + if err = encoder.Encode(changes); err != nil { log.Printf("WARN: encode changes to JSON failed: %s", err) return } - var buf bytes.Buffer - buf.Write(encoded) - if _, err = http.Post(url, "application/json", &buf); err != nil { + if _, err = http.Post( + url, "application/json", bytes.NewBuffer(buf.Bytes())); err != nil { log.Printf("WARN: posting changes to %s failed: %s", url, err) } }()