Ensure proper memory management by adding Free calls to release pooled buffers across export, import, and event handling workflows.

This commit is contained in:
2025-09-07 20:32:39 +01:00
parent 2491fd2738
commit 135508c390
4 changed files with 27 additions and 10 deletions

View File

@@ -49,6 +49,7 @@ func (d *D) Export(c context.Context, w io.Writer, pubkeys ...[]byte) {
if _, err = w.Write([]byte{'\n'}); chk.E(err) {
return
}
ev.Free()
evBuf.Reset()
}
return
@@ -86,14 +87,15 @@ func (d *D) Export(c context.Context, w io.Writer, pubkeys ...[]byte) {
if err = ev.UnmarshalBinary(evBuf); chk.E(err) {
continue
}
// Serialize the event to JSON and write it to the output
if _, err = w.Write(ev.Serialize()); chk.E(err) {
continue
}
if _, err = w.Write([]byte{'\n'}); chk.E(err) {
continue
}
evBuf.Reset()
// Serialize the event to JSON and write it to the output
if _, err = w.Write(ev.Serialize()); chk.E(err) {
continue
}
if _, err = w.Write([]byte{'\n'}); chk.E(err) {
continue
}
ev.Free()
evBuf.Reset()
}
return
},

View File

@@ -52,17 +52,22 @@ func (d *D) Import(rr io.Reader) {
continue
}
ev := &event.E{}
ev := event.New()
if _, err = ev.Unmarshal(b); err != nil {
// return the pooled buffer on error
ev.Free()
continue
}
if _, _, err = d.SaveEvent(d.ctx, ev); err != nil {
// return the pooled buffer on error paths too
ev.Free()
continue
}
// return the pooled buffer after successful save
ev.Free()
b = nil
ev = nil
count++
if count%100 == 0 {
log.I.F("received %d events", count)