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) { func (ct *ChangeTracker) FlushChanges(url string) (err error) {
log.Println("change flush triggered") log.Println("change flush triggered")
var oldChanges map[quantizedXZ]bool var oldChanges map[quantizedXZ]bool
var sendNotification bool
ct.mutex.Lock() ct.mutex.Lock()
if len(ct.changes) > 0 {
oldChanges = ct.changes oldChanges = ct.changes
if len(oldChanges) > 0 {
sendNotification = true
ct.changes = make(map[quantizedXZ]bool) ct.changes = make(map[quantizedXZ]bool)
} }
ct.mutex.Unlock() ct.mutex.Unlock()
if !sendNotification { if oldChanges == nil {
return return
} }
go func() { go func() {
@ -57,15 +55,14 @@ func (ct *ChangeTracker) FlushChanges(url string) (err error) {
changes[i] = change changes[i] = change
i++ i++
} }
var err error var buf bytes.Buffer
var encoded []byte encoder := json.NewEncoder(&buf)
if encoded, err = json.Marshal(changes); err != nil { if err = encoder.Encode(changes); err != nil {
log.Printf("WARN: encode changes to JSON failed: %s", err) log.Printf("WARN: encode changes to JSON failed: %s", err)
return return
} }
var buf bytes.Buffer if _, err = http.Post(
buf.Write(encoded) url, "application/json", bytes.NewBuffer(buf.Bytes())); err != nil {
if _, err = http.Post(url, "application/json", &buf); err != nil {
log.Printf("WARN: posting changes to %s failed: %s", url, err) log.Printf("WARN: posting changes to %s failed: %s", url, err)
} }
}() }()