fix staticcheck SA6002: use *[]byte in sync.Pool to avoid allocation
This commit is contained in:
parent
e014e9ccd4
commit
a9990d59bc
1 changed files with 8 additions and 3 deletions
11
agent.go
11
agent.go
|
|
@ -120,18 +120,23 @@ func runSession(ctx context.Context, baseURL, token string) error {
|
|||
}
|
||||
|
||||
bufPool := &sync.Pool{
|
||||
New: func() any { return make([]byte, 0, 4096) },
|
||||
New: func() any {
|
||||
b := make([]byte, 0, 4096)
|
||||
return &b
|
||||
},
|
||||
}
|
||||
|
||||
sendBinaryResult := func(event string, msg proto.Message) {
|
||||
buf := bufPool.Get().([]byte)[:0]
|
||||
bp := bufPool.Get().(*[]byte)
|
||||
buf := (*bp)[:0]
|
||||
bin, err := proto.MarshalOptions{}.MarshalAppend(buf, msg)
|
||||
if err != nil {
|
||||
slog.Error("marshal protobuf", "error", err)
|
||||
return
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString(bin)
|
||||
bufPool.Put(bin[:0])
|
||||
*bp = bin[:0]
|
||||
bufPool.Put(bp)
|
||||
payload, _ := json.Marshal(map[string]string{"binary": encoded})
|
||||
sendMsg(event, payload)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue