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

alexstocks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu.git


The following commit(s) were added to refs/heads/develop by this push:
     new 3e5f3b9b [fix] Http proxy connection can not reuse between pixiu and 
upstream (#578)
3e5f3b9b is described below

commit 3e5f3b9b21b01b088f3894fa45e46f290e0898e0
Author: Mark4z <[email protected]>
AuthorDate: Thu Sep 7 08:28:10 2023 +0800

    [fix] Http proxy connection can not reuse between pixiu and upstream (#578)
---
 pixiu/pkg/common/http/manager.go                |  2 ++
 pixiu/pkg/filter/http/httpproxy/routerfilter.go | 48 +++++++++++++++----------
 pixiu/pkg/pluginregistry/proxywasm_register.go  |  4 ++-
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/pixiu/pkg/common/http/manager.go b/pixiu/pkg/common/http/manager.go
index 4ffb310f..47d02fba 100644
--- a/pixiu/pkg/common/http/manager.go
+++ b/pixiu/pkg/common/http/manager.go
@@ -132,6 +132,8 @@ func (hcm *HttpConnectionManager) buildTargetResponse(c 
*pch.HttpContext) {
                if err != nil {
                        panic(err)
                }
+               //close body
+               _ = res.Body.Close()
                //Merge header
                remoteHeader := res.Header
                for k := range remoteHeader {
diff --git a/pixiu/pkg/filter/http/httpproxy/routerfilter.go 
b/pixiu/pkg/filter/http/httpproxy/routerfilter.go
index 0bbee9d7..41ad533e 100644
--- a/pixiu/pkg/filter/http/httpproxy/routerfilter.go
+++ b/pixiu/pkg/filter/http/httpproxy/routerfilter.go
@@ -20,8 +20,9 @@ package httpproxy
 import (
        "encoding/json"
        "fmt"
-       http3 "net/http"
+       stdhttp "net/http"
        "net/url"
+       "time"
 )
 
 import (
@@ -47,14 +48,20 @@ type (
        }
        // FilterFactory is http filter instance
        FilterFactory struct {
-               cfg *Config
+               cfg    *Config
+               client stdhttp.Client
        }
        //Filter
        Filter struct {
-               transport http3.RoundTripper
+               client stdhttp.Client
        }
        // Config describe the config of FilterFactory
-       Config struct{}
+       Config struct {
+               Timeout             time.Duration `yaml:"timeout" 
json:"timeout,omitempty"`
+               MaxIdleConns        int           `yaml:"maxIdleConns" 
json:"maxIdleConns,omitempty"`
+               MaxIdleConnsPerHost int           `yaml:"maxIdleConnsPerHost" 
json:"maxIdleConnsPerHost,omitempty"`
+               MaxConnsPerHost     int           `yaml:"maxConnsPerHost" 
json:"maxConnsPerHost,omitempty"`
+       }
 )
 
 func (p *Plugin) Kind() string {
@@ -70,11 +77,22 @@ func (factory *FilterFactory) Config() interface{} {
 }
 
 func (factory *FilterFactory) Apply() error {
+       cfg := factory.cfg
+       client := stdhttp.Client{
+               Timeout: cfg.Timeout,
+               Transport: stdhttp.RoundTripper(&stdhttp.Transport{
+                       MaxIdleConns:        cfg.MaxIdleConns,
+                       MaxIdleConnsPerHost: cfg.MaxIdleConnsPerHost,
+                       MaxConnsPerHost:     cfg.MaxConnsPerHost,
+               }),
+       }
+       factory.client = client
        return nil
 }
 
 func (factory *FilterFactory) PrepareFilterChain(ctx *http.HttpContext, chain 
filter.FilterChain) error {
-       f := &Filter{transport: &http3.Transport{}}
+       //reuse http client
+       f := &Filter{factory.client}
        chain.AppendDecodeFilters(f)
        return nil
 }
@@ -92,7 +110,7 @@ func (f *Filter) Decode(hc *http.HttpContext) 
filter.FilterStatus {
        if endpoint == nil {
                logger.Debugf("[dubbo-go-pixiu] cluster not found endpoint")
                bt, _ := json.Marshal(http.ErrResponse{Message: "cluster not 
found endpoint"})
-               hc.SendLocalReply(http3.StatusServiceUnavailable, bt)
+               hc.SendLocalReply(stdhttp.StatusServiceUnavailable, bt)
                return filter.Stop
        }
 
@@ -100,7 +118,7 @@ func (f *Filter) Decode(hc *http.HttpContext) 
filter.FilterStatus {
        r := hc.Request
 
        var (
-               req *http3.Request
+               req *stdhttp.Request
                err error
        )
 
@@ -111,31 +129,25 @@ func (f *Filter) Decode(hc *http.HttpContext) 
filter.FilterStatus {
                RawQuery: r.URL.RawQuery,
        }
 
-       req, err = http3.NewRequest(r.Method, parsedURL.String(), r.Body)
+       req, err = stdhttp.NewRequest(r.Method, parsedURL.String(), r.Body)
        if err != nil {
                bt, _ := json.Marshal(http.ErrResponse{Message: 
fmt.Sprintf("BUG: new request failed: %v", err)})
-               hc.SendLocalReply(http3.StatusInternalServerError, bt)
+               hc.SendLocalReply(stdhttp.StatusInternalServerError, bt)
                return filter.Stop
        }
        req.Header = r.Header
 
-       cli := &http3.Client{
-               Transport: f.transport,
-               Timeout:   hc.Timeout,
-       }
-
-       resp, err := cli.Do(req)
+       resp, err := f.client.Do(req)
        if err != nil {
                urlErr, ok := err.(*url.Error)
                if ok && urlErr.Timeout() {
-                       hc.SendLocalReply(http3.StatusGatewayTimeout, 
[]byte(err.Error()))
+                       hc.SendLocalReply(stdhttp.StatusGatewayTimeout, 
[]byte(err.Error()))
                        return filter.Stop
                }
-               hc.SendLocalReply(http3.StatusServiceUnavailable, 
[]byte(err.Error()))
+               hc.SendLocalReply(stdhttp.StatusServiceUnavailable, 
[]byte(err.Error()))
                return filter.Stop
        }
        logger.Debugf("[dubbo-go-pixiu] client call resp:%v", resp)
-
        hc.SourceResp = resp
        // response write in hcm
        return filter.Continue
diff --git a/pixiu/pkg/pluginregistry/proxywasm_register.go 
b/pixiu/pkg/pluginregistry/proxywasm_register.go
index c8bfcd9d..9d74a2da 100644
--- a/pixiu/pkg/pluginregistry/proxywasm_register.go
+++ b/pixiu/pkg/pluginregistry/proxywasm_register.go
@@ -19,4 +19,6 @@
 
 package pluginregistry
 
-import _ "github.com/apache/dubbo-go-pixiu/pixiu/pkg/filter/http/proxywasm"
+import (
+       _ "github.com/apache/dubbo-go-pixiu/pixiu/pkg/filter/http/proxywasm"
+)

Reply via email to