mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-27 19:20:59 +00:00
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>
This commit is contained in:
parent
dc02d49bf1
commit
da480e8529
@ -1289,14 +1289,16 @@ func (h *Handler) serveWatchIPNBus(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
h.b.WatchNotifications(ctx, mask, f.Flush, func(roNotify *ipn.Notify) (keepGoing bool) {
|
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 {
|
if err != nil {
|
||||||
h.logf("json.Marshal: %v", err)
|
h.logf("json.Marshal: %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(w, "%s\n", js); err != nil {
|
if _, err := w.Write(buf.Bytes()); err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
f.Flush()
|
f.Flush()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user