feat: configurable proxy buffer number

This commit is contained in:
Jim Zhang 2019-02-20 18:05:09 +08:00
parent d74dea7585
commit c92d29d462
8 changed files with 29 additions and 4 deletions

View file

@ -29,6 +29,7 @@ type Config struct {
ConnectTimeout int `json:"connectTimeout"`
SendTimeout int `json:"sendTimeout"`
ReadTimeout int `json:"readTimeout"`
BufferNumber int `json:"bufferNumber"`
BufferSize string `json:"bufferSize"`
CookieDomain string `json:"cookieDomain"`
CookiePath string `json:"cookiePath"`
@ -60,6 +61,9 @@ func (l1 *Config) Equal(l2 *Config) bool {
if l1.ReadTimeout != l2.ReadTimeout {
return false
}
if l1.BufferNumber != l2.BufferNumber {
return false
}
if l1.BufferSize != l2.BufferSize {
return false
}
@ -123,6 +127,11 @@ func (a proxy) Parse(ing *extensions.Ingress) (interface{}, error) {
config.ReadTimeout = defBackend.ProxyReadTimeout
}
config.BufferNumber, err = parser.GetIntAnnotation("proxy-buffer-number", ing)
if err != nil {
config.BufferNumber = defBackend.ProxyBufferNumber
}
config.BufferSize, err = parser.GetStringAnnotation("proxy-buffer-size", ing)
if err != nil {
config.BufferSize = defBackend.ProxyBufferSize

View file

@ -73,6 +73,7 @@ func (m mockBackend) GetDefaultBackend() defaults.Backend {
ProxyConnectTimeout: 10,
ProxySendTimeout: 15,
ProxyReadTimeout: 20,
ProxyBufferNumber: 4,
ProxyBufferSize: "10k",
ProxyBodySize: "3k",
ProxyNextUpstream: "error",
@ -89,6 +90,7 @@ func TestProxy(t *testing.T) {
data[parser.GetAnnotationWithPrefix("proxy-connect-timeout")] = "1"
data[parser.GetAnnotationWithPrefix("proxy-send-timeout")] = "2"
data[parser.GetAnnotationWithPrefix("proxy-read-timeout")] = "3"
data[parser.GetAnnotationWithPrefix("proxy-buffer-number")] = "8"
data[parser.GetAnnotationWithPrefix("proxy-buffer-size")] = "1k"
data[parser.GetAnnotationWithPrefix("proxy-body-size")] = "2k"
data[parser.GetAnnotationWithPrefix("proxy-next-upstream")] = "off"
@ -114,6 +116,9 @@ func TestProxy(t *testing.T) {
if p.ReadTimeout != 3 {
t.Errorf("expected 3 as read-timeout but returned %v", p.ReadTimeout)
}
if p.BufferNumber != 8 {
t.Errorf("expected 8 as proxy-buffer-number but returned %v", p.BufferNumber)
}
if p.BufferSize != "1k" {
t.Errorf("expected 1k as buffer-size but returned %v", p.BufferSize)
}
@ -157,6 +162,9 @@ func TestProxyWithNoAnnotation(t *testing.T) {
if p.ReadTimeout != 20 {
t.Errorf("expected 20 as read-timeout but returned %v", p.ReadTimeout)
}
if p.BufferNumber != 4 {
t.Errorf("expected 4 as buffer-number but returned %v", p.BufferNumber)
}
if p.BufferSize != "10k" {
t.Errorf("expected 10k as buffer-size but returned %v", p.BufferSize)
}

View file

@ -671,6 +671,7 @@ func NewDefault() Configuration {
ProxyConnectTimeout: 5,
ProxyReadTimeout: 60,
ProxySendTimeout: 60,
ProxyBufferNumber: 4,
ProxyBufferSize: "4k",
ProxyCookieDomain: "off",
ProxyCookiePath: "off",

View file

@ -887,6 +887,7 @@ func (n *NGINXController) createServers(data []*ingress.Ingress,
ConnectTimeout: bdef.ProxyConnectTimeout,
SendTimeout: bdef.ProxySendTimeout,
ReadTimeout: bdef.ProxyReadTimeout,
BufferNumber: bdef.ProxyBufferNumber,
BufferSize: bdef.ProxyBufferSize,
CookieDomain: bdef.ProxyCookieDomain,
CookiePath: bdef.ProxyCookiePath,

View file

@ -50,6 +50,10 @@ type Backend struct {
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout
ProxySendTimeout int `json:"proxy-send-timeout"`
// Sets the number of the buffers used for reading a response from the proxied server
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers
ProxyBufferNumber int `json:"proxy-buffer-number"`
// Sets the size of the buffer used for reading the first part of the response received from the
// proxied server. This part usually contains a small response header.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffer_size)

View file

@ -969,7 +969,7 @@ stream {
proxy_buffering {{ $location.Proxy.ProxyBuffering }};
proxy_buffer_size {{ $location.Proxy.BufferSize }};
proxy_buffers 4 {{ $location.Proxy.BufferSize }};
proxy_buffers {{$location.Proxy.BufferNumber}} {{ $location.Proxy.BufferSize }};
proxy_request_buffering {{ $location.Proxy.RequestBuffering }};
proxy_http_version 1.1;
@ -1267,7 +1267,7 @@ stream {
proxy_buffering {{ $location.Proxy.ProxyBuffering }};
proxy_buffer_size {{ $location.Proxy.BufferSize }};
proxy_buffers 4 {{ $location.Proxy.BufferSize }};
proxy_buffers {{$location.Proxy.BufferNumber}} {{ $location.Proxy.BufferSize }};
proxy_request_buffering {{ $location.Proxy.RequestBuffering }};
proxy_http_version 1.1;

View file

@ -7,6 +7,7 @@
"bind-address-ipv6": [ "[2001:db8:a0b:12f0::1]" ,"[3731:54:65fe:2::a7]" ,"[33:33:33::33::33]" ],
"backend": {
"custom-http-errors": [404],
"proxy-buffer-number": "4",
"proxy-buffer-size": "4k",
"proxy-connect-timeout": 5,
"proxy-read-timeout": 60,

View file

@ -147,8 +147,9 @@ var _ = framework.IngressNginxDescribe("Annotations - Proxy", func() {
It("should turn on proxy-buffering", func() {
annotations := map[string]string{
"nginx.ingress.kubernetes.io/proxy-buffering": "on",
"nginx.ingress.kubernetes.io/proxy-buffer-size": "8k",
"nginx.ingress.kubernetes.io/proxy-buffering": "on",
"nginx.ingress.kubernetes.io/proxy-buffer-number": "4",
"nginx.ingress.kubernetes.io/proxy-buffer-size": "8k",
}
ing := framework.NewSingleIngress(host, "/", host, f.IngressController.Namespace, "http-svc", 80, &annotations)