Chore: use protobytes replace most of bytes.Buffer

This commit is contained in:
Dreamacro
2023-04-17 14:08:39 +08:00
parent df61a586c9
commit b7aade5e11
15 changed files with 244 additions and 236 deletions

View File

@ -1,7 +1,6 @@
package route
import (
"bytes"
"encoding/json"
"net/http"
"strconv"
@ -9,6 +8,7 @@ import (
"github.com/Dreamacro/clash/tunnel/statistic"
"github.com/Dreamacro/protobytes"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
"github.com/gorilla/websocket"
@ -47,11 +47,11 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
interval = t
}
buf := &bytes.Buffer{}
buf := protobytes.BytesWriter{}
sendSnapshot := func() error {
buf.Reset()
snapshot := statistic.DefaultManager.Snapshot()
if err := json.NewEncoder(buf).Encode(snapshot); err != nil {
if err := json.NewEncoder(&buf).Encode(snapshot); err != nil {
return err
}

View File

@ -12,6 +12,7 @@ import (
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel/statistic"
"github.com/Dreamacro/protobytes"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/go-chi/render"
@ -151,12 +152,12 @@ func traffic(w http.ResponseWriter, r *http.Request) {
tick := time.NewTicker(time.Second)
defer tick.Stop()
t := statistic.DefaultManager
buf := &bytes.Buffer{}
buf := protobytes.BytesWriter{}
var err error
for range tick.C {
buf.Reset()
up, down := t.Now()
if err := json.NewEncoder(buf).Encode(Traffic{
if err := json.NewEncoder(&buf).Encode(Traffic{
Up: up,
Down: down,
}); err != nil {