Proxy performance (#946)

* proxy: add benchmark

Signed-off-by: Tw <tw19881113@gmail.com>

* replacer: prepare lazily

update issue#939

benchmark            old ns/op     new ns/op     delta
BenchmarkProxy-4     83865         72544         -13.50%

Signed-off-by: Tw <tw19881113@gmail.com>

* proxy: use buffer pool to avoid temporary allocation

Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
Tw
2016-07-21 01:06:14 +00:00
committed by Matt Holt
parent 1240690973
commit beae16f07c
4 changed files with 99 additions and 37 deletions
+38 -30
View File
@@ -37,8 +37,8 @@ type Replacer interface {
// they will be used to overwrite other replacements
// if there is a name conflict.
type replacer struct {
replacements map[string]string
customReplacements map[string]string
replacements map[string]func() string
customReplacements map[string]func() string
emptyValue string
responseRecorder *ResponseRecorder
}
@@ -53,36 +53,36 @@ type replacer struct {
func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Replacer {
rep := &replacer{
responseRecorder: rr,
customReplacements: make(map[string]string),
replacements: map[string]string{
"{method}": r.Method,
customReplacements: make(map[string]func() string),
replacements: map[string]func() string{
"{method}": func() string { return r.Method },
"{scheme}": func() string {
if r.TLS != nil {
return "https"
}
return "http"
}(),
},
"{hostname}": func() string {
name, err := os.Hostname()
if err != nil {
return ""
}
return name
}(),
"{host}": r.Host,
},
"{host}": func() string { return r.Host },
"{hostonly}": func() string {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
return r.Host
}
return host
}(),
"{path}": r.URL.Path,
"{path_escaped}": url.QueryEscape(r.URL.Path),
"{query}": r.URL.RawQuery,
"{query_escaped}": url.QueryEscape(r.URL.RawQuery),
"{fragment}": r.URL.Fragment,
"{proto}": r.Proto,
},
"{path}": func() string { return r.URL.Path },
"{path_escaped}": func() string { return url.QueryEscape(r.URL.Path) },
"{query}": func() string { return r.URL.RawQuery },
"{query_escaped}": func() string { return url.QueryEscape(r.URL.RawQuery) },
"{fragment}": func() string { return r.URL.Fragment },
"{proto}": func() string { return r.Proto },
"{remote}": func() string {
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
return fwdFor
@@ -92,25 +92,25 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
return r.RemoteAddr
}
return host
}(),
},
"{port}": func() string {
_, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return ""
}
return port
}(),
"{uri}": r.URL.RequestURI(),
"{uri_escaped}": url.QueryEscape(r.URL.RequestURI()),
"{when}": time.Now().Format(timeFormat),
},
"{uri}": func() string { return r.URL.RequestURI() },
"{uri_escaped}": func() string { return url.QueryEscape(r.URL.RequestURI()) },
"{when}": func() string { return time.Now().Format(timeFormat) },
"{file}": func() string {
_, file := path.Split(r.URL.Path)
return file
}(),
},
"{dir}": func() string {
dir, _ := path.Split(r.URL.Path)
return dir
}(),
},
"{request}": func() string {
dump, err := httputil.DumpRequest(r, false)
if err != nil {
@@ -118,14 +118,15 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
}
return requestReplacer.Replace(string(dump))
}(),
},
},
emptyValue: emptyValue,
}
// Header placeholders (case-insensitive)
for header, values := range r.Header {
rep.replacements[headerReplacer+strings.ToLower(header)+"}"] = strings.Join(values, ",")
values := values
rep.replacements[headerReplacer+strings.ToLower(header)+"}"] = func() string { return strings.Join(values, ",") }
}
return rep
@@ -141,9 +142,9 @@ func (r *replacer) Replace(s string) string {
// Make response placeholders now
if r.responseRecorder != nil {
r.replacements["{status}"] = strconv.Itoa(r.responseRecorder.status)
r.replacements["{size}"] = strconv.Itoa(r.responseRecorder.size)
r.replacements["{latency}"] = time.Since(r.responseRecorder.start).String()
r.replacements["{status}"] = func() string { return strconv.Itoa(r.responseRecorder.status) }
r.replacements["{size}"] = func() string { return strconv.Itoa(r.responseRecorder.size) }
r.replacements["{latency}"] = func() string { return time.Since(r.responseRecorder.start).String() }
}
// Include custom placeholders, overwriting existing ones if necessary
@@ -158,7 +159,10 @@ func (r *replacer) Replace(s string) string {
idxEnd := strings.Index(s[endOffset:], "}")
if idxEnd > -1 {
placeholder := strings.ToLower(s[idxStart : endOffset+idxEnd+1])
replacement := r.replacements[placeholder]
replacement := ""
if getReplacement, ok := r.replacements[placeholder]; ok {
replacement = getReplacement()
}
if replacement == "" {
replacement = r.emptyValue
}
@@ -169,7 +173,11 @@ func (r *replacer) Replace(s string) string {
}
// Regular replacements - these are easier because they're case-sensitive
for placeholder, replacement := range r.replacements {
for placeholder, getReplacement := range r.replacements {
if !strings.Contains(s, placeholder) {
continue
}
replacement := getReplacement()
if replacement == "" {
replacement = r.emptyValue
}
@@ -181,7 +189,7 @@ func (r *replacer) Replace(s string) string {
// Set sets key to value in the r.customReplacements map.
func (r *replacer) Set(key, value string) {
r.customReplacements["{"+key+"}"] = value
r.customReplacements["{"+key+"}"] = func() string { return value }
}
const (