Simplified json encoding

This commit is contained in:
Sascha L. Teichmann 2014-09-18 06:49:50 +02:00
parent 51626a1371
commit e81051a84c

View File

@ -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)
}
}()