Allow for proper garbage collection pooling bytes.Buffer (#6266)

This commit is contained in:
Harshavardhana
2018-08-16 18:37:43 -07:00
committed by kannappanr
parent 1103ad2d08
commit 65de2d68c0
4 changed files with 89 additions and 35 deletions

View File

@@ -17,7 +17,6 @@
package rpc
import (
"bytes"
"context"
"crypto/tls"
"encoding/gob"
@@ -48,22 +47,25 @@ func (client *Client) Call(serviceMethod string, args, reply interface{}) error
return fmt.Errorf("rpc reply must be a pointer type, but found %v", replyKind)
}
data, err := gobEncode(args)
if err != nil {
argBuf := bufPool.Get()
defer bufPool.Put(argBuf)
if err := gobEncodeBuf(args, argBuf); err != nil {
return err
}
callRequest := CallRequest{
Method: serviceMethod,
ArgBytes: data,
ArgBytes: argBuf.Bytes(),
}
var buf bytes.Buffer
if err = gob.NewEncoder(&buf).Encode(callRequest); err != nil {
reqBuf := bufPool.Get()
defer bufPool.Put(reqBuf)
if err := gob.NewEncoder(reqBuf).Encode(callRequest); err != nil {
return err
}
response, err := client.httpClient.Post(client.serviceURL.String(), "", &buf)
response, err := client.httpClient.Post(client.serviceURL.String(), "", reqBuf)
if err != nil {
return err
}