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

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git

commit 8e69bfa957c36474d503209d6125e5402566e8f2
Author: spacewander <[email protected]>
AuthorDate: Wed May 26 14:40:39 2021 +0800

    perf: reuse objects
---
 internal/http/request.go       | 22 +++++++++++++++++++---
 internal/http/request_test.go  |  1 +
 internal/http/response.go      | 20 +++++++++++++++++++-
 internal/http/response_test.go |  1 +
 internal/plugin/plugin.go      |  3 +++
 5 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/internal/http/request.go b/internal/http/request.go
index 11c9ef4..c7f2e8d 100644
--- a/internal/http/request.go
+++ b/internal/http/request.go
@@ -17,6 +17,7 @@ package http
 import (
        "net"
        "net/http"
+       "sync"
 
        pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
        "github.com/api7/ext-plugin-proto/go/A6"
@@ -78,6 +79,11 @@ func (r *Request) Header() pkgHTTP.Header {
        return r.hdr
 }
 
+func (r *Request) Reset() {
+       r.path = nil
+       r.hdr = nil
+}
+
 func (r *Request) FetchChanges(id uint32, builder *flatbuffers.Builder) bool {
        if r.path == nil && r.hdr == nil {
                return false
@@ -143,13 +149,23 @@ func (r *Request) FetchChanges(id uint32, builder 
*flatbuffers.Builder) bool {
        return true
 }
 
+var reqPool = sync.Pool{
+       New: func() interface{} {
+               return &Request{}
+       },
+}
+
 func CreateRequest(buf []byte) *Request {
-       req := &Request{
-               r: hrc.GetRootAsReq(buf, 0),
-       }
+       req := reqPool.Get().(*Request)
+       req.r = hrc.GetRootAsReq(buf, 0)
        return req
 }
 
+func ReuseRequest(r *Request) {
+       r.Reset()
+       reqPool.Put(r)
+}
+
 type Header struct {
        http.Header
 }
diff --git a/internal/http/request_test.go b/internal/http/request_test.go
index 54fed1e..accaa38 100644
--- a/internal/http/request_test.go
+++ b/internal/http/request_test.go
@@ -116,6 +116,7 @@ func TestSrcIp(t *testing.T) {
                out := buildReq(reqOpt{srcIP: ip})
                r := CreateRequest(out)
                assert.Equal(t, ip, r.SrcIP())
+               ReuseRequest(r)
        }
 }
 
diff --git a/internal/http/response.go b/internal/http/response.go
index ef28b9a..d0aa8a4 100644
--- a/internal/http/response.go
+++ b/internal/http/response.go
@@ -17,6 +17,7 @@ package http
 import (
        "bytes"
        "net/http"
+       "sync"
 
        "github.com/api7/ext-plugin-proto/go/A6"
        hrc "github.com/api7/ext-plugin-proto/go/A6/HTTPReqCall"
@@ -51,6 +52,12 @@ func (r *Response) WriteHeader(statusCode int) {
        r.code = statusCode
 }
 
+func (r *Response) Reset() {
+       r.body = nil
+       r.code = 0
+       r.hdr = nil
+}
+
 func (r *Response) FetchChanges(id uint32, builder *flatbuffers.Builder) bool {
        if r.body == nil && r.code == 0 && len(r.hdr) == 0 {
                return false
@@ -112,6 +119,17 @@ func (r *Response) FetchChanges(id uint32, builder 
*flatbuffers.Builder) bool {
        return true
 }
 
+var respPool = sync.Pool{
+       New: func() interface{} {
+               return &Response{}
+       },
+}
+
 func CreateResponse() *Response {
-       return &Response{}
+       return respPool.Get().(*Response)
+}
+
+func ReuseResponse(r *Response) {
+       r.Reset()
+       respPool.Put(r)
 }
diff --git a/internal/http/response_test.go b/internal/http/response_test.go
index 5eddd4c..eadad62 100644
--- a/internal/http/response_test.go
+++ b/internal/http/response_test.go
@@ -41,6 +41,7 @@ func getStopAction(t *testing.T, b *flatbuffers.Builder) 
*hrc.Stop {
 
 func TestFetchChanges(t *testing.T) {
        r := CreateResponse()
+       defer ReuseResponse(r)
        r.Write([]byte("hello"))
        h := r.Header()
        h.Set("foo", "bar")
diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go
index 3ca4159..b88ade7 100644
--- a/internal/plugin/plugin.go
+++ b/internal/plugin/plugin.go
@@ -45,7 +45,10 @@ func reportAction(id uint32, req *inHTTP.Request, resp 
*inHTTP.Response) *flatbu
 
 func HTTPReqCall(buf []byte) (*flatbuffers.Builder, error) {
        req := inHTTP.CreateRequest(buf)
+       defer inHTTP.ReuseRequest(req)
+
        resp := inHTTP.CreateResponse()
+       defer inHTTP.ReuseResponse(resp)
 
        token := req.ConfToken()
        conf, err := GetRuleConf(token)

Reply via email to