-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sync.Pool to cache *bufio.Reader in tunnel server #381
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add comments for putBufioReader func. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add comments for newBufioReader func.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.