Skip to content

Commit

Permalink
pre-compile regex
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislusf committed Nov 25, 2019
1 parent f3ffa92 commit 49182b6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions teeproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -196,19 +198,22 @@ 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() {
var altServers arrayAlternatives
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)

Expand Down

0 comments on commit 49182b6

Please sign in to comment.