From 72fa94e90008bf10110425e38f349713ea839ba5 Mon Sep 17 00:00:00 2001 From: Kate Osborn Date: Wed, 11 Jan 2023 13:13:59 -0700 Subject: [PATCH 1/2] Calculate max capacity for locations slice --- internal/nginx/config/servers.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/nginx/config/servers.go b/internal/nginx/config/servers.go index d7f37f6139..d60263b298 100644 --- a/internal/nginx/config/servers.go +++ b/internal/nginx/config/servers.go @@ -69,7 +69,16 @@ func createLocations(pathRules []state.PathRule, listenerPort int) []http.Locati return []http.Location{createDefaultRootLocation()} } - locs := make([]http.Location, 0, lenPathRules) // FIXME(pleshakov): expand with rule.Routes + maxLocs := 0 + // To calculate the maximum number of locations, we need to take into account the following: + // 1. Each match rule for a path rule will have one location. + // 2. Each path rule may have an additional location if it contains non-path-only matches. + // 3. Each path rule may have an additional location for the default root path. + for _, rules := range pathRules { + maxLocs += len(rules.MatchRules) + 2 + } + + locs := make([]http.Location, 0, maxLocs) rootPathExists := false From cb0910891b7b669a38b03656e576d73eac7e557d Mon Sep 17 00:00:00 2001 From: Kate Osborn Date: Thu, 12 Jan 2023 08:39:22 -0700 Subject: [PATCH 2/2] Fix mistake --- internal/nginx/config/servers.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/nginx/config/servers.go b/internal/nginx/config/servers.go index d60263b298..332407b5e1 100644 --- a/internal/nginx/config/servers.go +++ b/internal/nginx/config/servers.go @@ -69,13 +69,13 @@ func createLocations(pathRules []state.PathRule, listenerPort int) []http.Locati return []http.Location{createDefaultRootLocation()} } - maxLocs := 0 // To calculate the maximum number of locations, we need to take into account the following: // 1. Each match rule for a path rule will have one location. // 2. Each path rule may have an additional location if it contains non-path-only matches. - // 3. Each path rule may have an additional location for the default root path. + // 3. There may be an additional location for the default root path. + maxLocs := 1 for _, rules := range pathRules { - maxLocs += len(rules.MatchRules) + 2 + maxLocs += len(rules.MatchRules) + 1 } locs := make([]http.Location, 0, maxLocs)