From c27f7aec2c3fa5c0797a875c4adcd4e9f447429f Mon Sep 17 00:00:00 2001 From: DrmagicE <379342542@qq.com> Date: Sat, 10 Jul 2021 13:05:48 +0800 Subject: [PATCH 1/3] add sync.Pool to cache *bufio.Reader in tunnel server --- pkg/yurttunnel/server/interceptor.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/yurttunnel/server/interceptor.go b/pkg/yurttunnel/server/interceptor.go index 4f115bdd367..2fcda00a9f7 100644 --- a/pkg/yurttunnel/server/interceptor.go +++ b/pkg/yurttunnel/server/interceptor.go @@ -25,6 +25,7 @@ import ( "net" "net/http" "strings" + "sync" "time" "github.com/openyurtio/openyurt/pkg/yurttunnel/constants" @@ -39,8 +40,23 @@ var ( supportedHeaders = []string{constants.ProxyHostHeaderKey, "User-Agent"} HeaderTransferEncoding = "Transfer-Encoding" HeaderChunked = "chunked" + bufioReaderPool sync.Pool ) +func newBufioReader(r io.Reader) *bufio.Reader { + if v := bufioReaderPool.Get(); v != nil { + br := v.(*bufio.Reader) + br.Reset(r) + return br + } + return bufio.NewReader(r) +} + +func putBufioReader(br *bufio.Reader) { + br.Reset(nil) + bufioReaderPool.Put(br) +} + // ReqRequestInterceptor intercepts http/https requests sent from the master, // prometheus and metric server, setup proxy tunnel to kubelet, sends requests // through the tunnel and sends responses back to the master @@ -70,7 +86,8 @@ func NewRequestInterceptor(udsSockFile string, cfg *tls.Config) *RequestIntercep } fmt.Fprintf(proxyConn, "CONNECT %s HTTP/1.1\r\nHost: %s%s\r\n\r\n", addr, "127.0.0.1", connectHeaders) - br := bufio.NewReader(proxyConn) + br := newBufioReader(proxyConn) + defer putBufioReader(br) res, err := http.ReadResponse(br, nil) if err != nil { proxyConn.Close() @@ -151,7 +168,9 @@ func (ri *RequestInterceptor) ServeHTTP(w http.ResponseWriter, r *http.Request) func getResponse(r io.Reader) (*http.Response, []byte, error) { rawResponse := bytes.NewBuffer(make([]byte, 0, 256)) // Save the bytes read while reading the response headers into the rawResponse buffer - resp, err := http.ReadResponse(bufio.NewReader(io.TeeReader(r, rawResponse)), nil) + br := newBufioReader(io.TeeReader(r, rawResponse)) + defer putBufioReader(br) + resp, err := http.ReadResponse(br, nil) if err != nil { return nil, nil, err } @@ -250,7 +269,9 @@ func isChunked(response *http.Response) bool { // serverRequest serves the normal requests, e.g., kubectl logs func serveRequest(tunnelConn net.Conn, w http.ResponseWriter, r *http.Request) { - tunnelHTTPResp, err := http.ReadResponse(bufio.NewReader(tunnelConn), r) + br := newBufioReader(tunnelConn) + defer putBufioReader(br) + tunnelHTTPResp, err := http.ReadResponse(br, r) if err != nil { klogAndHTTPError(w, http.StatusServiceUnavailable, "fail to read response from the tunnel: %v", err) return From c65722e763100b1bb5972c86f3580d323c2d9f11 Mon Sep 17 00:00:00 2001 From: DrmagicE <379342542@qq.com> Date: Sat, 10 Jul 2021 13:15:20 +0800 Subject: [PATCH 2/3] Docs: update supported kubernetes version to 1.18 --- README.md | 2 +- README.zh.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed460193cda..8ba33d978d8 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ The major OpenYurt components consist of: ## Getting started -OpenYurt supports Kubernetes versions up to 1.16. Using higher Kubernetes versions may cause +OpenYurt supports Kubernetes versions up to 1.18. Using higher Kubernetes versions may cause compatibility issues. You can setup the OpenYurt cluster [manually](docs/tutorial/manually-setup.md), but we recommend to start diff --git a/README.zh.md b/README.zh.md index 1df42053c36..27e1c9caa04 100644 --- a/README.zh.md +++ b/README.zh.md @@ -53,7 +53,7 @@ OpenYurt 的主要组件包括: [资源和系统要求](./docs/resource-and-system-requirements-cn.md) ## 开始使用 -OpenYurt 支持最高版本为1.16的 Kubernetes 。使用更高版本的 Kubernetes 可能会导致兼容性问题。 +OpenYurt 支持最高版本为1.18的 Kubernetes 。使用更高版本的 Kubernetes 可能会导致兼容性问题。 您可以[手动](docs/tutorial/manually-setup.md)设置 OpenYurt 集群,但是我们建议使用 `yurtctl` 命令行工具启动 OpenYurt 。要快速构建和安装设置 `yurtctl` ,在编译系统已安装了 golang 1.13+ 和 bash 的前提下你可以执行以下命令来完成安装: ```bash From ea5e34a308c7c420a6f759ad40e9aa9d7e94a50f Mon Sep 17 00:00:00 2001 From: DrmagicE <379342542@qq.com> Date: Tue, 13 Jul 2021 14:21:09 +0800 Subject: [PATCH 3/3] add comments --- pkg/yurttunnel/server/interceptor.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/yurttunnel/server/interceptor.go b/pkg/yurttunnel/server/interceptor.go index 2fcda00a9f7..7d8ff2ee01c 100644 --- a/pkg/yurttunnel/server/interceptor.go +++ b/pkg/yurttunnel/server/interceptor.go @@ -43,6 +43,8 @@ var ( bufioReaderPool sync.Pool ) +// newBufioReader retrieves a cached Reader from the pool if the pool is not empty, +// otherwise creates a new one func newBufioReader(r io.Reader) *bufio.Reader { if v := bufioReaderPool.Get(); v != nil { br := v.(*bufio.Reader) @@ -52,6 +54,7 @@ func newBufioReader(r io.Reader) *bufio.Reader { return bufio.NewReader(r) } +// putBufioReader puts the Reader to the pool. func putBufioReader(br *bufio.Reader) { br.Reset(nil) bufioReaderPool.Put(br)