Skip to content

Commit

Permalink
Add UpstreamKeepAlive struct for http Upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
bjee19 committed Dec 11, 2024
1 parent 02705e0 commit 513351e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 71 deletions.
19 changes: 12 additions & 7 deletions internal/mode/static/nginx/config/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,18 @@ const (

// Upstream holds all configuration for an HTTP upstream.
type Upstream struct {
Name string
ZoneSize string // format: 512k, 1m
KeepAliveTime string
KeepAliveTimeout string
Servers []UpstreamServer
KeepAliveConnections int32
KeepAliveRequests int32
Name string
ZoneSize string // format: 512k, 1m
KeepAlive UpstreamKeepAlive
Servers []UpstreamServer
}

// UpstreamKeepAlive holds the keepalive configuration for an HTTP upstream.
type UpstreamKeepAlive struct {
Time string
Timeout string
Connections int32
Requests int32
}

// UpstreamServer holds all configuration for an HTTP upstream server.
Expand Down
30 changes: 20 additions & 10 deletions internal/mode/static/nginx/config/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3093,8 +3093,10 @@ func TestGetConnectionHeader(t *testing.T) {
upstreamMap: UpstreamMap{
nameToUpstream: map[string]http.Upstream{
"upstream": {
Name: "upstream",
KeepAliveConnections: 1,
Name: "upstream",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
},
},
},
},
Expand All @@ -3110,16 +3112,22 @@ func TestGetConnectionHeader(t *testing.T) {
upstreamMap: UpstreamMap{
nameToUpstream: map[string]http.Upstream{
"upstream1": {
Name: "upstream1",
KeepAliveConnections: 1,
Name: "upstream1",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
},
},
"upstream2": {
Name: "upstream2",
KeepAliveRequests: 1,
Name: "upstream2",
KeepAlive: http.UpstreamKeepAlive{
Requests: 1,
},
},
"upstream3": {
Name: "upstream3",
KeepAliveTime: "5s",
Name: "upstream3",
KeepAlive: http.UpstreamKeepAlive{
Time: "5s",
},
},
},
},
Expand All @@ -3142,8 +3150,10 @@ func TestGetConnectionHeader(t *testing.T) {
upstreamMap: UpstreamMap{
nameToUpstream: map[string]http.Upstream{
"upstream1": {
Name: "upstream1",
KeepAliveConnections: 1,
Name: "upstream1",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
},
},
"upstream2": {
Name: "upstream2",
Expand Down
24 changes: 13 additions & 11 deletions internal/mode/static/nginx/config/upstreams.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ type UpstreamMap struct {

func (um UpstreamMap) keepAliveEnabled(name string) bool {
if upstream, exists := um.nameToUpstream[name]; exists {
return upstream.KeepAliveConnections != 0 ||
upstream.KeepAliveRequests != 0 ||
upstream.KeepAliveTime != "" ||
upstream.KeepAliveTimeout != ""
return upstream.KeepAlive.Connections != 0 ||
upstream.KeepAlive.Requests != 0 ||
upstream.KeepAlive.Time != "" ||
upstream.KeepAlive.Timeout != ""
}

return false
Expand Down Expand Up @@ -176,13 +176,15 @@ func (g GeneratorImpl) createUpstream(
}

return http.Upstream{
Name: up.Name,
ZoneSize: zoneSize,
Servers: upstreamServers,
KeepAliveConnections: upstreamPolicySettings.KeepAlive.Connections,
KeepAliveRequests: upstreamPolicySettings.KeepAlive.Requests,
KeepAliveTime: upstreamPolicySettings.KeepAlive.Time,
KeepAliveTimeout: upstreamPolicySettings.KeepAlive.Timeout,
Name: up.Name,
ZoneSize: zoneSize,
Servers: upstreamServers,
KeepAlive: http.UpstreamKeepAlive{
Connections: upstreamPolicySettings.KeepAlive.Connections,
Requests: upstreamPolicySettings.KeepAlive.Requests,
Time: upstreamPolicySettings.KeepAlive.Time,
Timeout: upstreamPolicySettings.KeepAlive.Timeout,
},
}
}

Expand Down
16 changes: 8 additions & 8 deletions internal/mode/static/nginx/config/upstreams_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ upstream {{ $u.Name }} {
{{ range $server := $u.Servers }}
server {{ $server.Address }};
{{- end }}
{{ if $u.KeepAliveConnections -}}
keepalive {{ $u.KeepAliveConnections }};
{{ if $u.KeepAlive.Connections -}}
keepalive {{ $u.KeepAlive.Connections }};
{{- end }}
{{ if $u.KeepAliveRequests -}}
keepalive_requests {{ $u.KeepAliveRequests }};
{{ if $u.KeepAlive.Requests -}}
keepalive_requests {{ $u.KeepAlive.Requests }};
{{- end }}
{{ if $u.KeepAliveTime -}}
keepalive_time {{ $u.KeepAliveTime }};
{{ if $u.KeepAlive.Time -}}
keepalive_time {{ $u.KeepAlive.Time }};
{{- end }}
{{ if $u.KeepAliveTimeout -}}
keepalive_timeout {{ $u.KeepAliveTimeout }};
{{ if $u.KeepAlive.Timeout -}}
keepalive_timeout {{ $u.KeepAlive.Timeout }};
{{- end }}
}
{{ end -}}
Expand Down
90 changes: 55 additions & 35 deletions internal/mode/static/nginx/config/upstreams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,12 @@ func TestCreateUpstreams(t *testing.T) {
Address: "12.0.0.0:80",
},
},
KeepAliveConnections: 1,
KeepAliveRequests: 1,
KeepAliveTime: "5s",
KeepAliveTimeout: "10s",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
Requests: 1,
Time: "5s",
Timeout: "10s",
},
},
{
Name: invalidBackendRef,
Expand All @@ -262,8 +264,8 @@ func TestCreateUpstream(t *testing.T) {
gen := GeneratorImpl{}
tests := []struct {
msg string
stateUpstream dataplane.Upstream
expectedUpstream http.Upstream
stateUpstream dataplane.Upstream
}{
{
stateUpstream: dataplane.Upstream{
Expand Down Expand Up @@ -389,10 +391,12 @@ func TestCreateUpstream(t *testing.T) {
Address: "10.0.0.1:80",
},
},
KeepAliveConnections: 1,
KeepAliveRequests: 1,
KeepAliveTime: "5s",
KeepAliveTimeout: "10s",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
Requests: 1,
Time: "5s",
Timeout: "10s",
},
},
msg: "single upstreamSettingsPolicy",
},
Expand Down Expand Up @@ -441,10 +445,12 @@ func TestCreateUpstream(t *testing.T) {
Address: "10.0.0.1:80",
},
},
KeepAliveConnections: 1,
KeepAliveRequests: 1,
KeepAliveTime: "5s",
KeepAliveTimeout: "10s",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
Requests: 1,
Time: "5s",
Timeout: "10s",
},
},
msg: "multiple upstreamSettingsPolicies",
},
Expand Down Expand Up @@ -511,10 +517,12 @@ func TestCreateUpstream(t *testing.T) {
Address: "10.0.0.1:80",
},
},
KeepAliveConnections: 1,
KeepAliveRequests: 1,
KeepAliveTime: "5s",
KeepAliveTimeout: "10s",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
Requests: 1,
Time: "5s",
Timeout: "10s",
},
},
msg: "upstreamSettingsPolicy with only keep alive settings",
},
Expand Down Expand Up @@ -791,43 +799,53 @@ func TestKeepAliveEnabled(t *testing.T) {
{
msg: "upstream with all keepAlive fields set",
upstream: http.Upstream{
Name: "upAllKeepAliveFieldsSet",
KeepAliveConnections: 1,
KeepAliveRequests: 1,
KeepAliveTime: "5s",
KeepAliveTimeout: "10s",
Name: "upAllKeepAliveFieldsSet",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
Requests: 1,
Time: "5s",
Timeout: "10s",
},
},
expKeepAliveEnabled: true,
},
{
msg: "upstream with keepAlive connection field set",
upstream: http.Upstream{
Name: "upKeepAliveConnectionsSet",
KeepAliveConnections: 1,
Name: "upKeepAliveConnectionsSet",
KeepAlive: http.UpstreamKeepAlive{
Connections: 1,
},
},
expKeepAliveEnabled: true,
},
{
msg: "upstream with keepAlive requests field set",
upstream: http.Upstream{
Name: "upKeepAliveRequestsSet",
KeepAliveRequests: 1,
Name: "upKeepAliveRequestsSet",
KeepAlive: http.UpstreamKeepAlive{
Requests: 1,
},
},
expKeepAliveEnabled: true,
},
{
msg: "upstream with keepAlive time field set",
upstream: http.Upstream{
Name: "upKeepAliveTimeSet",
KeepAliveTime: "5s",
Name: "upKeepAliveTimeSet",
KeepAlive: http.UpstreamKeepAlive{
Time: "5s",
},
},
expKeepAliveEnabled: true,
},
{
msg: "upstream with keepAlive timeout field set",
upstream: http.Upstream{
Name: "upKeepAliveTimeoutSet",
KeepAliveTimeout: "10s",
Name: "upKeepAliveTimeoutSet",
KeepAlive: http.UpstreamKeepAlive{
Timeout: "10s",
},
},
expKeepAliveEnabled: true,
},
Expand All @@ -841,11 +859,13 @@ func TestKeepAliveEnabled(t *testing.T) {
{
msg: "upstream with keepAlive fields set to empty values",
upstream: http.Upstream{
Name: "upNoKeepAliveFieldsSet",
KeepAliveConnections: 0,
KeepAliveRequests: 0,
KeepAliveTime: "",
KeepAliveTimeout: "",
Name: "upNoKeepAliveFieldsSet",
KeepAlive: http.UpstreamKeepAlive{
Connections: 0,
Requests: 0,
Time: "",
Timeout: "",
},
},
expKeepAliveEnabled: false,
},
Expand Down

0 comments on commit 513351e

Please sign in to comment.