Skip to content

Commit 0bfca00

Browse files
authored
Calculate max capacity for locations slice (#360)
1 parent 1639e67 commit 0bfca00

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

internal/nginx/config/servers.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ func createLocations(pathRules []state.PathRule, listenerPort int) []http.Locati
6969
return []http.Location{createDefaultRootLocation()}
7070
}
7171

72-
locs := make([]http.Location, 0, lenPathRules) // FIXME(pleshakov): expand with rule.Routes
72+
// To calculate the maximum number of locations, we need to take into account the following:
73+
// 1. Each match rule for a path rule will have one location.
74+
// 2. Each path rule may have an additional location if it contains non-path-only matches.
75+
// 3. There may be an additional location for the default root path.
76+
maxLocs := 1
77+
for _, rules := range pathRules {
78+
maxLocs += len(rules.MatchRules) + 1
79+
}
80+
81+
locs := make([]http.Location, 0, maxLocs)
7382

7483
rootPathExists := false
7584

0 commit comments

Comments
 (0)