localapi: reduce garbage production in bus watcher

Updates #optimization

Signed-off-by: James Tucker <james@tailscale.com>
This commit is contained in:
James Tucker 2024-03-20 16:05:46 -07:00 committed by James Tucker
parent 3f7313dbdb
commit e0f97738ee
1 changed files with 3 additions and 5 deletions

View File

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