From 3555d23136b0a3d425cdfd3333c1be95fbbb3fa3 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Fri, 25 Feb 2022 21:21:21 +0800 Subject: [PATCH] network: remove unnecessary sync.Pool --- client/internal/highway/bdh.go | 16 +++++----- client/internal/highway/highway.go | 9 ++++-- client/internal/network/frame.go | 47 +++++++++------------------- client/internal/network/request.go | 2 -- client/internal/network/response.go | 1 + client/internal/network/transport.go | 1 + 6 files changed, 32 insertions(+), 44 deletions(-) diff --git a/client/internal/highway/bdh.go b/client/internal/highway/bdh.go index 4d76a182..2921d37e 100644 --- a/client/internal/highway/bdh.go +++ b/client/internal/highway/bdh.go @@ -105,7 +105,8 @@ func (s *Session) UploadBDH(input BdhInput) ([]byte, error) { ReqExtendinfo: input.Ext, }) offset += rl - _, err = io.Copy(conn, network.HeadBodyFrame(head, chunk)) + frame := network.HeadBodyFrame(head, chunk) + _, err = frame.WriteTo(conn) if err != nil { return nil, errors.Wrap(err, "write conn error") } @@ -189,7 +190,7 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in return err } - buffer := make([]byte, blockSize) + chunk := make([]byte, blockSize) for { nextId := atomic.AddUint32(&BlockId, 1) if nextId >= uint32(len(blocks)) { @@ -203,10 +204,10 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in } cond.L.Unlock() } - buffer = buffer[:blockSize] + chunk = chunk[:blockSize] cond.L.Lock() // lock protect reading - n, err := input.Body.ReadAt(buffer, block.Offset) + n, err := input.Body.ReadAt(chunk, block.Offset) cond.L.Unlock() if err != nil { @@ -214,12 +215,12 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in break } if err == io.ErrUnexpectedEOF { - buffer = buffer[:n] + chunk = chunk[:n] } else { return err } } - ch := md5.Sum(buffer) + ch := md5.Sum(chunk) head, _ := proto.Marshal(&pb.ReqDataHighwayHead{ MsgBasehead: s.dataHighwayHead(4096, input.CommandID, 2052), MsgSeghead: &pb.SegHead{ @@ -232,7 +233,8 @@ func (s *Session) UploadBDHMultiThread(input BdhMultiThreadInput, threadCount in }, ReqExtendinfo: input.Ext, }) - _, err = io.Copy(conn, network.HeadBodyFrame(head, buffer)) + frame := network.HeadBodyFrame(head, chunk) + _, err = frame.WriteTo(conn) if err != nil { return errors.Wrap(err, "write conn error") } diff --git a/client/internal/highway/highway.go b/client/internal/highway/highway.go index 81cd0ffd..d031bc6d 100644 --- a/client/internal/highway/highway.go +++ b/client/internal/highway/highway.go @@ -86,7 +86,8 @@ func (s *Session) Upload(addr Addr, input Input) error { ReqExtendinfo: []byte{}, }) offset += rl - _, err = io.Copy(conn, network.HeadBodyFrame(head, chunk)) + frame := network.HeadBodyFrame(head, chunk) + _, err = frame.WriteTo(conn) if err != nil { return errors.Wrap(err, "write conn error") } @@ -145,7 +146,8 @@ func (s *Session) UploadExciting(input ExcitingInput) ([]byte, error) { ReqExtendinfo: input.Ext, }) offset += int64(rl) - req, _ := http.NewRequest("POST", url, network.HeadBodyFrame(head, chunk)) + frame := network.HeadBodyFrame(head, chunk) + req, _ := http.NewRequest("POST", url, &frame) req.Header.Set("Accept", "*/*") req.Header.Set("Connection", "Keep-Alive") req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)") @@ -206,7 +208,8 @@ func (s *Session) sendHeartbreak(conn net.Conn) error { LocaleId: 2052, }, }) - _, err := io.Copy(conn, network.HeadBodyFrame(head, nil)) + frame := network.HeadBodyFrame(head, nil) + _, err := frame.WriteTo(conn) return err } diff --git a/client/internal/network/frame.go b/client/internal/network/frame.go index c272a64a..2b1b8f08 100644 --- a/client/internal/network/frame.go +++ b/client/internal/network/frame.go @@ -2,30 +2,11 @@ package network import ( "encoding/binary" - "io" "net" - "sync" ) var etx = []byte{0x29} -type Buffers struct { - net.Buffers -} - -var pool = sync.Pool{ - New: func() interface{} { - lenHead := make([]byte, 9) - lenHead[0] = 0x28 - return &Buffers{net.Buffers{lenHead, nil, nil, etx}} - }, -} - -func (b *Buffers) WriteTo(w io.Writer) (n int64, err error) { - defer pool.Put(b) // implement auto put to pool - return b.Buffers.WriteTo(w) -} - // HeadBodyFrame 包格式 // * STX // * head length @@ -34,17 +15,19 @@ func (b *Buffers) WriteTo(w io.Writer) (n int64, err error) { // * body data // * ETX // 节省内存, 可被go runtime优化为writev操作 -func HeadBodyFrame(head []byte, body []byte) *Buffers { - b := pool.Get().(*Buffers) - if len(b.Buffers) == 0 { - lenHead := make([]byte, 9) - lenHead[0] = 0x28 - b.Buffers = net.Buffers{lenHead, nil, nil, etx} - } - b.Buffers[2] = body - b.Buffers[1] = head - _ = b.Buffers[0][8] - binary.BigEndian.PutUint32(b.Buffers[0][1:], uint32(len(head))) - binary.BigEndian.PutUint32(b.Buffers[0][5:], uint32(len(body))) - return b +func HeadBodyFrame(head []byte, body []byte) net.Buffers { + buffers := make(net.Buffers, 4) + // buffer0 format: + // * STX + // * head length + // * body length + buffer0 := make([]byte, 9) + buffer0[0] = 0x28 + binary.BigEndian.PutUint32(buffer0[1:], uint32(len(head))) + binary.BigEndian.PutUint32(buffer0[5:], uint32(len(body))) + buffers[0] = buffer0 + buffers[1] = head + buffers[2] = body + buffers[3] = etx + return buffers } diff --git a/client/internal/network/request.go b/client/internal/network/request.go index b34930f5..6e61686b 100644 --- a/client/internal/network/request.go +++ b/client/internal/network/request.go @@ -7,8 +7,6 @@ const ( RequestTypeSimple = 0x0B ) -var emptyKey = make([]byte, 16) - type EncryptType uint32 const ( diff --git a/client/internal/network/response.go b/client/internal/network/response.go index c2d97f3c..bdaa4c40 100644 --- a/client/internal/network/response.go +++ b/client/internal/network/response.go @@ -46,6 +46,7 @@ func (t *Transport) ReadResponse(head []byte) (*Response, error) { case EncryptTypeD2Key: body = binary.NewTeaCipher(t.Sig.D2Key).Decrypt(body) case EncryptTypeEmptyKey: + emptyKey := make([]byte, 16) body = binary.NewTeaCipher(emptyKey).Decrypt(body) } err := t.readSSOFrame(resp, body) diff --git a/client/internal/network/transport.go b/client/internal/network/transport.go index dd79a8d9..e2286e33 100644 --- a/client/internal/network/transport.go +++ b/client/internal/network/transport.go @@ -89,6 +89,7 @@ func (t *Transport) PackPacket(req *Request) []byte { case EncryptTypeD2Key: body = binary.NewTeaCipher(t.Sig.D2Key).Encrypt(body) case EncryptTypeEmptyKey: + emptyKey := make([]byte, 16) body = binary.NewTeaCipher(emptyKey).Encrypt(body) } w.Write(body)