ipn/localapi: reuse buffer for encoding JSON in serveWatchIPNBus

Reuse a single byte buffer for JSON encoding all notifications
to the same subscriber. This retains more memory, but produces
less garbage.

Updates tailscale/corp#18514

Signed-off-by: Percy Wegmann <percy@tailscale.com>
oxtoacart/golden_memory
Percy Wegmann 3 months ago
parent dc02d49bf1
commit da480e8529
No known key found for this signature in database
GPG Key ID: 29D8CDEB4C13D48B

@ -1289,14 +1289,16 @@ func (h *Handler) serveWatchIPNBus(w http.ResponseWriter, r *http.Request) {
}
w.Header().Set("Content-Type", "application/json")
buf := bytes.NewBuffer(nil)
ctx := r.Context()
h.b.WatchNotifications(ctx, mask, f.Flush, func(roNotify *ipn.Notify) (keepGoing bool) {
js, err := json.Marshal(roNotify)
buf.Reset()
err := json.NewEncoder(buf).Encode(roNotify)
if err != nil {
h.logf("json.Marshal: %v", err)
return false
}
if _, err := fmt.Fprintf(w, "%s\n", js); err != nil {
if _, err := w.Write(buf.Bytes()); err != nil {
return false
}
f.Flush()

Loading…
Cancel
Save