Skip to content

Commit a2ace83

Browse files
committed
Add keepAliveChecker function
1 parent 513351e commit a2ace83

File tree

5 files changed

+280
-180
lines changed

5 files changed

+280
-180
lines changed

internal/mode/static/nginx/config/generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ func (g GeneratorImpl) executeConfigTemplates(
134134
fileBytes := make(map[string][]byte)
135135

136136
httpUpstreams := g.createUpstreams(conf.Upstreams, upstreamsettings.NewProcessor())
137-
upstreamMap := g.createUpstreamMap(httpUpstreams)
137+
keepAliveCheck := newKeepAliveChecker(httpUpstreams)
138138

139-
for _, execute := range g.getExecuteFuncs(generator, httpUpstreams, upstreamMap) {
139+
for _, execute := range g.getExecuteFuncs(generator, httpUpstreams, keepAliveCheck) {
140140
results := execute(conf)
141141
for _, res := range results {
142142
fileBytes[res.dest] = append(fileBytes[res.dest], res.data...)
@@ -164,12 +164,12 @@ func (g GeneratorImpl) executeConfigTemplates(
164164
func (g GeneratorImpl) getExecuteFuncs(
165165
generator policies.Generator,
166166
upstreams []http.Upstream,
167-
upstreamMap UpstreamMap,
167+
keepAliveCheck keepAliveChecker,
168168
) []executeFunc {
169169
return []executeFunc{
170170
executeMainConfig,
171171
executeBaseHTTPConfig,
172-
g.newExecuteServersFunc(generator, upstreamMap),
172+
g.newExecuteServersFunc(generator, keepAliveCheck),
173173
newExecuteUpstreamsFunc(upstreams),
174174
executeSplitClients,
175175
executeMaps,

internal/mode/static/nginx/config/servers.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,21 @@ var httpUpgradeHeader = http.Header{
4343
Value: "$http_upgrade",
4444
}
4545

46-
func (g GeneratorImpl) newExecuteServersFunc(generator policies.Generator, um UpstreamMap) executeFunc {
46+
func (g GeneratorImpl) newExecuteServersFunc(
47+
generator policies.Generator,
48+
keepAliveCheck keepAliveChecker,
49+
) executeFunc {
4750
return func(configuration dataplane.Configuration) []executeResult {
48-
return g.executeServers(configuration, generator, um)
51+
return g.executeServers(configuration, generator, keepAliveCheck)
4952
}
5053
}
5154

5255
func (g GeneratorImpl) executeServers(
5356
conf dataplane.Configuration,
5457
generator policies.Generator,
55-
um UpstreamMap,
58+
keepAliveCheck keepAliveChecker,
5659
) []executeResult {
57-
servers, httpMatchPairs := createServers(conf, generator, um)
60+
servers, httpMatchPairs := createServers(conf, generator, keepAliveCheck)
5861

5962
serverConfig := http.ServerConfig{
6063
Servers: servers,
@@ -104,7 +107,7 @@ func getIPFamily(baseHTTPConfig dataplane.BaseHTTPConfig) shared.IPFamily {
104107
func createServers(
105108
conf dataplane.Configuration,
106109
generator policies.Generator,
107-
um UpstreamMap,
110+
keepAliveCheck keepAliveChecker,
108111
) ([]http.Server, httpMatchPairs) {
109112
servers := make([]http.Server, 0, len(conf.HTTPServers)+len(conf.SSLServers))
110113
finalMatchPairs := make(httpMatchPairs)
@@ -116,15 +119,15 @@ func createServers(
116119

117120
for idx, s := range conf.HTTPServers {
118121
serverID := fmt.Sprintf("%d", idx)
119-
httpServer, matchPairs := createServer(s, serverID, generator, um)
122+
httpServer, matchPairs := createServer(s, serverID, generator, keepAliveCheck)
120123
servers = append(servers, httpServer)
121124
maps.Copy(finalMatchPairs, matchPairs)
122125
}
123126

124127
for idx, s := range conf.SSLServers {
125128
serverID := fmt.Sprintf("SSL_%d", idx)
126129

127-
sslServer, matchPairs := createSSLServer(s, serverID, generator, um)
130+
sslServer, matchPairs := createSSLServer(s, serverID, generator, keepAliveCheck)
128131
if _, portInUse := sharedTLSPorts[s.Port]; portInUse {
129132
sslServer.Listen = getSocketNameHTTPS(s.Port)
130133
sslServer.IsSocket = true
@@ -140,7 +143,7 @@ func createSSLServer(
140143
virtualServer dataplane.VirtualServer,
141144
serverID string,
142145
generator policies.Generator,
143-
um UpstreamMap,
146+
keepAliveCheck keepAliveChecker,
144147
) (http.Server, httpMatchPairs) {
145148
listen := fmt.Sprint(virtualServer.Port)
146149
if virtualServer.IsDefault {
@@ -150,7 +153,7 @@ func createSSLServer(
150153
}, nil
151154
}
152155

153-
locs, matchPairs, grpc := createLocations(&virtualServer, serverID, generator, um)
156+
locs, matchPairs, grpc := createLocations(&virtualServer, serverID, generator, keepAliveCheck)
154157

155158
server := http.Server{
156159
ServerName: virtualServer.Hostname,
@@ -179,7 +182,7 @@ func createServer(
179182
virtualServer dataplane.VirtualServer,
180183
serverID string,
181184
generator policies.Generator,
182-
um UpstreamMap,
185+
keepAliveCheck keepAliveChecker,
183186
) (http.Server, httpMatchPairs) {
184187
listen := fmt.Sprint(virtualServer.Port)
185188

@@ -190,7 +193,7 @@ func createServer(
190193
}, nil
191194
}
192195

193-
locs, matchPairs, grpc := createLocations(&virtualServer, serverID, generator, um)
196+
locs, matchPairs, grpc := createLocations(&virtualServer, serverID, generator, keepAliveCheck)
194197

195198
server := http.Server{
196199
ServerName: virtualServer.Hostname,
@@ -226,7 +229,7 @@ func createLocations(
226229
server *dataplane.VirtualServer,
227230
serverID string,
228231
generator policies.Generator,
229-
um UpstreamMap,
232+
keepAliveCheck keepAliveChecker,
230233
) ([]http.Location, httpMatchPairs, bool) {
231234
maxLocs, pathsAndTypes := getMaxLocationCountAndPathMap(server.PathRules)
232235
locs := make([]http.Location, 0, maxLocs)
@@ -255,7 +258,15 @@ func createLocations(
255258

256259
if !needsInternalLocations(rule) {
257260
for _, r := range rule.MatchRules {
258-
extLocations = updateLocations(r.Filters, extLocations, r, server.Port, rule.Path, rule.GRPC, um)
261+
extLocations = updateLocations(
262+
r.Filters,
263+
extLocations,
264+
r,
265+
server.Port,
266+
rule.Path,
267+
rule.GRPC,
268+
keepAliveCheck,
269+
)
259270
}
260271

261272
locs = append(locs, extLocations...)
@@ -277,7 +288,7 @@ func createLocations(
277288
server.Port,
278289
rule.Path,
279290
rule.GRPC,
280-
um,
291+
keepAliveCheck,
281292
)
282293

283294
internalLocations = append(internalLocations, intLocation)
@@ -414,7 +425,7 @@ func updateLocation(
414425
listenerPort int32,
415426
path string,
416427
grpc bool,
417-
um UpstreamMap,
428+
keepAliveCheck keepAliveChecker,
418429
) http.Location {
419430
if filters.InvalidFilter != nil {
420431
location.Return = &http.Return{Code: http.StatusInternalServerError}
@@ -436,7 +447,7 @@ func updateLocation(
436447
extraHeaders = append(extraHeaders, grpcAuthorityHeader)
437448
} else {
438449
extraHeaders = append(extraHeaders, httpUpgradeHeader)
439-
extraHeaders = append(extraHeaders, getConnectionHeader(um, matchRule.BackendGroup.Backends))
450+
extraHeaders = append(extraHeaders, getConnectionHeader(keepAliveCheck, matchRule.BackendGroup.Backends))
440451
}
441452

442453
proxySetHeaders := generateProxySetHeaders(&matchRule.Filters, createBaseProxySetHeaders(extraHeaders...))
@@ -476,12 +487,12 @@ func updateLocations(
476487
listenerPort int32,
477488
path string,
478489
grpc bool,
479-
um UpstreamMap,
490+
keepAliveCheck keepAliveChecker,
480491
) []http.Location {
481492
updatedLocations := make([]http.Location, len(buildLocations))
482493

483494
for i, loc := range buildLocations {
484-
updatedLocations[i] = updateLocation(filters, loc, matchRule, listenerPort, path, grpc, um)
495+
updatedLocations[i] = updateLocation(filters, loc, matchRule, listenerPort, path, grpc, keepAliveCheck)
485496
}
486497

487498
return updatedLocations
@@ -890,9 +901,9 @@ func createBaseProxySetHeaders(extraHeaders ...http.Header) []http.Header {
890901
return baseHeaders
891902
}
892903

893-
func getConnectionHeader(um UpstreamMap, backends []dataplane.Backend) http.Header {
904+
func getConnectionHeader(keepAliveCheck keepAliveChecker, backends []dataplane.Backend) http.Header {
894905
for _, backend := range backends {
895-
if um.keepAliveEnabled(backend.UpstreamName) {
906+
if keepAliveCheck(backend.UpstreamName) {
896907
// if keep-alive settings are enabled on any upstream, the connection header value
897908
// must be empty for the location
898909
return unsetHTTPConnectionHeader

0 commit comments

Comments
 (0)