From 49182b6eb0c04da3fcf97a7a2263ddf43d04e3b3 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 25 Nov 2019 09:38:39 -0800 Subject: [PATCH] pre-compile regex --- teeproxy.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/teeproxy.go b/teeproxy.go index 5926db4..fa0b24f 100644 --- a/teeproxy.go +++ b/teeproxy.go @@ -32,6 +32,8 @@ var ( tlsCertificate = flag.String("cert.file", "", "path to the TLS certificate file") forwardClientIP = flag.Bool("forward-client-ip", false, "enable forwarding of the client IP to the backend using the 'X-Forwarded-For' and 'Forwarded' headers") closeConnections = flag.Bool("close-connections", false, "close connections to the clients and backends") + + alternateMethodsRegex *regexp.Regexp ) // Sets the request URL. @@ -147,7 +149,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { updateForwardedHeaders(req) } if *percent == 100.0 || h.Randomizer.Float64()*100 < *percent { - if matchedByHttpMethod(req.Method, *alternateMethods) { + if matchedByHttpMethod(req.Method) { for _, alt := range h.Alternatives { alternativeRequest = DuplicateRequest(req) @@ -196,12 +198,11 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } } -func matchedByHttpMethod(requestMethod, expectedMethod string) bool { - if expectedMethod == "" { +func matchedByHttpMethod(requestMethod string) bool { + if alternateMethodsRegex == nil { return true } - m, _ := regexp.MatchString(requestMethod, expectedMethod) - return m + return alternateMethodsRegex.MatchString(requestMethod) } func main() { @@ -209,6 +210,10 @@ func main() { flag.Var(&altServers, "b", "where testing traffic goes. response are skipped. http://localhost:8081/test, allowed multiple times for multiple testing backends") flag.Parse() + if *alternateMethods != "" { + alternateMethodsRegex = regexp.MustCompile(*alternateMethods) + } + log.Printf("Starting teeproxy at %s sending to A: %s and B: %s", *listen, *targetProduction, altServers)