Skip to content

Commit 513351e

Browse files
committed
Add UpstreamKeepAlive struct for http Upstream
1 parent 02705e0 commit 513351e

File tree

5 files changed

+108
-71
lines changed

5 files changed

+108
-71
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,18 @@ const (
8484

8585
// Upstream holds all configuration for an HTTP upstream.
8686
type Upstream struct {
87-
Name string
88-
ZoneSize string // format: 512k, 1m
89-
KeepAliveTime string
90-
KeepAliveTimeout string
91-
Servers []UpstreamServer
92-
KeepAliveConnections int32
93-
KeepAliveRequests int32
87+
Name string
88+
ZoneSize string // format: 512k, 1m
89+
KeepAlive UpstreamKeepAlive
90+
Servers []UpstreamServer
91+
}
92+
93+
// UpstreamKeepAlive holds the keepalive configuration for an HTTP upstream.
94+
type UpstreamKeepAlive struct {
95+
Time string
96+
Timeout string
97+
Connections int32
98+
Requests int32
9499
}
95100

96101
// UpstreamServer holds all configuration for an HTTP upstream server.

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,8 +3093,10 @@ func TestGetConnectionHeader(t *testing.T) {
30933093
upstreamMap: UpstreamMap{
30943094
nameToUpstream: map[string]http.Upstream{
30953095
"upstream": {
3096-
Name: "upstream",
3097-
KeepAliveConnections: 1,
3096+
Name: "upstream",
3097+
KeepAlive: http.UpstreamKeepAlive{
3098+
Connections: 1,
3099+
},
30983100
},
30993101
},
31003102
},
@@ -3110,16 +3112,22 @@ func TestGetConnectionHeader(t *testing.T) {
31103112
upstreamMap: UpstreamMap{
31113113
nameToUpstream: map[string]http.Upstream{
31123114
"upstream1": {
3113-
Name: "upstream1",
3114-
KeepAliveConnections: 1,
3115+
Name: "upstream1",
3116+
KeepAlive: http.UpstreamKeepAlive{
3117+
Connections: 1,
3118+
},
31153119
},
31163120
"upstream2": {
3117-
Name: "upstream2",
3118-
KeepAliveRequests: 1,
3121+
Name: "upstream2",
3122+
KeepAlive: http.UpstreamKeepAlive{
3123+
Requests: 1,
3124+
},
31193125
},
31203126
"upstream3": {
3121-
Name: "upstream3",
3122-
KeepAliveTime: "5s",
3127+
Name: "upstream3",
3128+
KeepAlive: http.UpstreamKeepAlive{
3129+
Time: "5s",
3130+
},
31233131
},
31243132
},
31253133
},
@@ -3142,8 +3150,10 @@ func TestGetConnectionHeader(t *testing.T) {
31423150
upstreamMap: UpstreamMap{
31433151
nameToUpstream: map[string]http.Upstream{
31443152
"upstream1": {
3145-
Name: "upstream1",
3146-
KeepAliveConnections: 1,
3153+
Name: "upstream1",
3154+
KeepAlive: http.UpstreamKeepAlive{
3155+
Connections: 1,
3156+
},
31473157
},
31483158
"upstream2": {
31493159
Name: "upstream2",

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ type UpstreamMap struct {
4040

4141
func (um UpstreamMap) keepAliveEnabled(name string) bool {
4242
if upstream, exists := um.nameToUpstream[name]; exists {
43-
return upstream.KeepAliveConnections != 0 ||
44-
upstream.KeepAliveRequests != 0 ||
45-
upstream.KeepAliveTime != "" ||
46-
upstream.KeepAliveTimeout != ""
43+
return upstream.KeepAlive.Connections != 0 ||
44+
upstream.KeepAlive.Requests != 0 ||
45+
upstream.KeepAlive.Time != "" ||
46+
upstream.KeepAlive.Timeout != ""
4747
}
4848

4949
return false
@@ -176,13 +176,15 @@ func (g GeneratorImpl) createUpstream(
176176
}
177177

178178
return http.Upstream{
179-
Name: up.Name,
180-
ZoneSize: zoneSize,
181-
Servers: upstreamServers,
182-
KeepAliveConnections: upstreamPolicySettings.KeepAlive.Connections,
183-
KeepAliveRequests: upstreamPolicySettings.KeepAlive.Requests,
184-
KeepAliveTime: upstreamPolicySettings.KeepAlive.Time,
185-
KeepAliveTimeout: upstreamPolicySettings.KeepAlive.Timeout,
179+
Name: up.Name,
180+
ZoneSize: zoneSize,
181+
Servers: upstreamServers,
182+
KeepAlive: http.UpstreamKeepAlive{
183+
Connections: upstreamPolicySettings.KeepAlive.Connections,
184+
Requests: upstreamPolicySettings.KeepAlive.Requests,
185+
Time: upstreamPolicySettings.KeepAlive.Time,
186+
Timeout: upstreamPolicySettings.KeepAlive.Timeout,
187+
},
186188
}
187189
}
188190

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ upstream {{ $u.Name }} {
1616
{{ range $server := $u.Servers }}
1717
server {{ $server.Address }};
1818
{{- end }}
19-
{{ if $u.KeepAliveConnections -}}
20-
keepalive {{ $u.KeepAliveConnections }};
19+
{{ if $u.KeepAlive.Connections -}}
20+
keepalive {{ $u.KeepAlive.Connections }};
2121
{{- end }}
22-
{{ if $u.KeepAliveRequests -}}
23-
keepalive_requests {{ $u.KeepAliveRequests }};
22+
{{ if $u.KeepAlive.Requests -}}
23+
keepalive_requests {{ $u.KeepAlive.Requests }};
2424
{{- end }}
25-
{{ if $u.KeepAliveTime -}}
26-
keepalive_time {{ $u.KeepAliveTime }};
25+
{{ if $u.KeepAlive.Time -}}
26+
keepalive_time {{ $u.KeepAlive.Time }};
2727
{{- end }}
28-
{{ if $u.KeepAliveTimeout -}}
29-
keepalive_timeout {{ $u.KeepAliveTimeout }};
28+
{{ if $u.KeepAlive.Timeout -}}
29+
keepalive_timeout {{ $u.KeepAlive.Timeout }};
3030
{{- end }}
3131
}
3232
{{ end -}}

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

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,12 @@ func TestCreateUpstreams(t *testing.T) {
237237
Address: "12.0.0.0:80",
238238
},
239239
},
240-
KeepAliveConnections: 1,
241-
KeepAliveRequests: 1,
242-
KeepAliveTime: "5s",
243-
KeepAliveTimeout: "10s",
240+
KeepAlive: http.UpstreamKeepAlive{
241+
Connections: 1,
242+
Requests: 1,
243+
Time: "5s",
244+
Timeout: "10s",
245+
},
244246
},
245247
{
246248
Name: invalidBackendRef,
@@ -262,8 +264,8 @@ func TestCreateUpstream(t *testing.T) {
262264
gen := GeneratorImpl{}
263265
tests := []struct {
264266
msg string
265-
stateUpstream dataplane.Upstream
266267
expectedUpstream http.Upstream
268+
stateUpstream dataplane.Upstream
267269
}{
268270
{
269271
stateUpstream: dataplane.Upstream{
@@ -389,10 +391,12 @@ func TestCreateUpstream(t *testing.T) {
389391
Address: "10.0.0.1:80",
390392
},
391393
},
392-
KeepAliveConnections: 1,
393-
KeepAliveRequests: 1,
394-
KeepAliveTime: "5s",
395-
KeepAliveTimeout: "10s",
394+
KeepAlive: http.UpstreamKeepAlive{
395+
Connections: 1,
396+
Requests: 1,
397+
Time: "5s",
398+
Timeout: "10s",
399+
},
396400
},
397401
msg: "single upstreamSettingsPolicy",
398402
},
@@ -441,10 +445,12 @@ func TestCreateUpstream(t *testing.T) {
441445
Address: "10.0.0.1:80",
442446
},
443447
},
444-
KeepAliveConnections: 1,
445-
KeepAliveRequests: 1,
446-
KeepAliveTime: "5s",
447-
KeepAliveTimeout: "10s",
448+
KeepAlive: http.UpstreamKeepAlive{
449+
Connections: 1,
450+
Requests: 1,
451+
Time: "5s",
452+
Timeout: "10s",
453+
},
448454
},
449455
msg: "multiple upstreamSettingsPolicies",
450456
},
@@ -511,10 +517,12 @@ func TestCreateUpstream(t *testing.T) {
511517
Address: "10.0.0.1:80",
512518
},
513519
},
514-
KeepAliveConnections: 1,
515-
KeepAliveRequests: 1,
516-
KeepAliveTime: "5s",
517-
KeepAliveTimeout: "10s",
520+
KeepAlive: http.UpstreamKeepAlive{
521+
Connections: 1,
522+
Requests: 1,
523+
Time: "5s",
524+
Timeout: "10s",
525+
},
518526
},
519527
msg: "upstreamSettingsPolicy with only keep alive settings",
520528
},
@@ -791,43 +799,53 @@ func TestKeepAliveEnabled(t *testing.T) {
791799
{
792800
msg: "upstream with all keepAlive fields set",
793801
upstream: http.Upstream{
794-
Name: "upAllKeepAliveFieldsSet",
795-
KeepAliveConnections: 1,
796-
KeepAliveRequests: 1,
797-
KeepAliveTime: "5s",
798-
KeepAliveTimeout: "10s",
802+
Name: "upAllKeepAliveFieldsSet",
803+
KeepAlive: http.UpstreamKeepAlive{
804+
Connections: 1,
805+
Requests: 1,
806+
Time: "5s",
807+
Timeout: "10s",
808+
},
799809
},
800810
expKeepAliveEnabled: true,
801811
},
802812
{
803813
msg: "upstream with keepAlive connection field set",
804814
upstream: http.Upstream{
805-
Name: "upKeepAliveConnectionsSet",
806-
KeepAliveConnections: 1,
815+
Name: "upKeepAliveConnectionsSet",
816+
KeepAlive: http.UpstreamKeepAlive{
817+
Connections: 1,
818+
},
807819
},
808820
expKeepAliveEnabled: true,
809821
},
810822
{
811823
msg: "upstream with keepAlive requests field set",
812824
upstream: http.Upstream{
813-
Name: "upKeepAliveRequestsSet",
814-
KeepAliveRequests: 1,
825+
Name: "upKeepAliveRequestsSet",
826+
KeepAlive: http.UpstreamKeepAlive{
827+
Requests: 1,
828+
},
815829
},
816830
expKeepAliveEnabled: true,
817831
},
818832
{
819833
msg: "upstream with keepAlive time field set",
820834
upstream: http.Upstream{
821-
Name: "upKeepAliveTimeSet",
822-
KeepAliveTime: "5s",
835+
Name: "upKeepAliveTimeSet",
836+
KeepAlive: http.UpstreamKeepAlive{
837+
Time: "5s",
838+
},
823839
},
824840
expKeepAliveEnabled: true,
825841
},
826842
{
827843
msg: "upstream with keepAlive timeout field set",
828844
upstream: http.Upstream{
829-
Name: "upKeepAliveTimeoutSet",
830-
KeepAliveTimeout: "10s",
845+
Name: "upKeepAliveTimeoutSet",
846+
KeepAlive: http.UpstreamKeepAlive{
847+
Timeout: "10s",
848+
},
831849
},
832850
expKeepAliveEnabled: true,
833851
},
@@ -841,11 +859,13 @@ func TestKeepAliveEnabled(t *testing.T) {
841859
{
842860
msg: "upstream with keepAlive fields set to empty values",
843861
upstream: http.Upstream{
844-
Name: "upNoKeepAliveFieldsSet",
845-
KeepAliveConnections: 0,
846-
KeepAliveRequests: 0,
847-
KeepAliveTime: "",
848-
KeepAliveTimeout: "",
862+
Name: "upNoKeepAliveFieldsSet",
863+
KeepAlive: http.UpstreamKeepAlive{
864+
Connections: 0,
865+
Requests: 0,
866+
Time: "",
867+
Timeout: "",
868+
},
849869
},
850870
expKeepAliveEnabled: false,
851871
},

0 commit comments

Comments
 (0)