This is an automated email from the ASF dual-hosted git repository.

wenfeng pushed a commit to branch native
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/native by this push:
     new 1fbd400  feat: use pool for header codec (#361)
1fbd400 is described below

commit 1fbd4004b842dc94474c69265d20d734717ec81f
Author: xujianhai666 <[email protected]>
AuthorDate: Mon Jan 6 10:01:54 2020 +0800

    feat: use pool for header codec (#361)
    
    Closes #360
---
 internal/remote/remote_client.go |  5 +++--
 primitive/pool.go                | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/internal/remote/remote_client.go b/internal/remote/remote_client.go
index ce35c6c..b5ad436 100644
--- a/internal/remote/remote_client.go
+++ b/internal/remote/remote_client.go
@@ -135,8 +135,8 @@ func (c *remotingClient) connect(ctx context.Context, addr 
string) (*tcpConnWrap
 
 func (c *remotingClient) receiveResponse(r *tcpConnWrapper) {
        var err error
-       header := make([]byte, 4)
-       defer c.closeConnection(r)
+       header := primitive.GetHeader()
+       defer primitive.BackHeader(header)
        for {
                if err != nil {
                        if r.isClosed(err) {
@@ -169,6 +169,7 @@ func (c *remotingClient) receiveResponse(r *tcpConnWrapper) 
{
                }
 
                buf := make([]byte, length)
+
                _, err = io.ReadFull(r, buf)
                if err != nil {
                        if r.isClosed(err) {
diff --git a/primitive/pool.go b/primitive/pool.go
new file mode 100644
index 0000000..5f729e7
--- /dev/null
+++ b/primitive/pool.go
@@ -0,0 +1,40 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package primitive
+
+import (
+       "sync"
+)
+
+var headerPool = sync.Pool{}
+
+func init() {
+       headerPool.New = func() interface{} {
+               return make([]byte, 4)
+       }
+}
+
+func GetHeader() []byte {
+       d := headerPool.Get().([]byte)
+       //d = (d)[:0]
+       return d
+}
+
+func BackHeader(d []byte) {
+       headerPool.Put(d)
+}

Reply via email to