Added parsing of seconds6 millisecond for nginx timeouts*

This commit is contained in:
Vladimir Zhukov 2023-06-19 15:21:59 +03:00
parent 0120a2df48
commit b51334d19b
5 changed files with 51 additions and 12 deletions

View file

@ -19,6 +19,7 @@ package parser
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -139,6 +140,29 @@ func (a ingAnnotations) parseInt(name string) (int, error) {
return 0, errors.ErrMissingAnnotations return 0, errors.ErrMissingAnnotations
} }
func (a ingAnnotations) parseTimeout(name string) (string, error) {
val, ok := a[name]
if ok {
setUnits, _ := regexp.Compile("(\\d+)(s|ms)$")
if setUnits.MatchString(name) {
return name, nil
}
noUnits, _ := regexp.Compile("\\d+$")
if noUnits.MatchString(name) {
return fmt.Sprintf("%ss", name), nil
}
if name == "" {
return "0", errors.NewInvalidAnnotationContent(name, val)
}
}
return "0", errors.ErrMissingAnnotations
}
func (a ingAnnotations) parseFloat32(name string) (float32, error) { func (a ingAnnotations) parseFloat32(name string) (float32, error) {
val, ok := a[name] val, ok := a[name]
if ok { if ok {
@ -179,6 +203,17 @@ func GetIntAnnotation(name string, ing *networking.Ingress, fields AnnotationFie
return ingAnnotations(ing.GetAnnotations()).parseInt(v) return ingAnnotations(ing.GetAnnotations()).parseInt(v)
} }
// GetTimeoutAnnotation extracts a string from an Ingress annotation, special format 10s
func GetTimeoutAnnotation(name string, ing *networking.Ingress) (string, error) {
v := GetAnnotationWithPrefix(name)
err := checkAnnotation(v, ing)
if err != nil {
return "", err
}
return ingAnnotations(ing.GetAnnotations()).parseTimeout(v)
}
// GetFloatAnnotation extracts a float32 from an Ingress annotation // GetFloatAnnotation extracts a float32 from an Ingress annotation
func GetFloatAnnotation(name string, ing *networking.Ingress, fields AnnotationFields) (float32, error) { func GetFloatAnnotation(name string, ing *networking.Ingress, fields AnnotationFields) (float32, error) {
v, err := checkAnnotation(name, ing, fields) v, err := checkAnnotation(name, ing, fields)

View file

@ -162,9 +162,9 @@ var proxyAnnotations = parser.Annotation{
// Config returns the proxy timeout to use in the upstream server/s // Config returns the proxy timeout to use in the upstream server/s
type Config struct { type Config struct {
BodySize string `json:"bodySize"` BodySize string `json:"bodySize"`
ConnectTimeout int `json:"connectTimeout"` ConnectTimeout string `json:"connectTimeout"`
SendTimeout int `json:"sendTimeout"` SendTimeout string `json:"sendTimeout"`
ReadTimeout int `json:"readTimeout"` ReadTimeout string `json:"readTimeout"`
BuffersNumber int `json:"buffersNumber"` BuffersNumber int `json:"buffersNumber"`
BufferSize string `json:"bufferSize"` BufferSize string `json:"bufferSize"`
CookieDomain string `json:"cookieDomain"` CookieDomain string `json:"cookieDomain"`
@ -266,16 +266,20 @@ func (a proxy) Parse(ing *networking.Ingress) (interface{}, error) {
var err error var err error
config.ConnectTimeout, err = parser.GetIntAnnotation(proxyConnectTimeoutAnnotation, ing, a.annotationConfig.Annotations) config.ConnectTimeout, err = parser.GetIntAnnotation(proxyConnectTimeoutAnnotation, ing, a.annotationConfig.Annotations)
if err != nil { if err != nil {
config.ConnectTimeout = defBackend.ProxyConnectTimeout config.ConnectTimeout = defBackend.ProxyConnectTimeout
} }
config.SendTimeout, err = parser.GetIntAnnotation(proxySendTimeoutAnnotation, ing, a.annotationConfig.Annotations) config.SendTimeout, err = parser.GetIntAnnotation(proxySendTimeoutAnnotation, ing, a.annotationConfig.Annotations)
if err != nil { if err != nil {
config.SendTimeout = defBackend.ProxySendTimeout config.SendTimeout = defBackend.ProxySendTimeout
} }
config.ReadTimeout, err = parser.GetIntAnnotation(proxyReadTimeoutAnnotation, ing, a.annotationConfig.Annotations) config.ReadTimeout, err = parser.GetIntAnnotation(proxyReadTimeoutAnnotation, ing, a.annotationConfig.Annotations)
if err != nil { if err != nil {
config.ReadTimeout = defBackend.ProxyReadTimeout config.ReadTimeout = defBackend.ProxyReadTimeout
} }

View file

@ -952,9 +952,9 @@ func NewDefault() Configuration {
ProxyStreamNextUpstreamTries: 3, ProxyStreamNextUpstreamTries: 3,
Backend: defaults.Backend{ Backend: defaults.Backend{
ProxyBodySize: bodySize, ProxyBodySize: bodySize,
ProxyConnectTimeout: 5, ProxyConnectTimeout: "5s",
ProxyReadTimeout: 60, ProxyReadTimeout: "60s",
ProxySendTimeout: 60, ProxySendTimeout: "60s",
ProxyBuffersNumber: 4, ProxyBuffersNumber: 4,
ProxyBufferSize: "4k", ProxyBufferSize: "4k",
ProxyCookieDomain: "off", ProxyCookieDomain: "off",

View file

@ -41,17 +41,17 @@ type Backend struct {
// Defines a timeout for establishing a connection with a proxied server. // Defines a timeout for establishing a connection with a proxied server.
// It should be noted that this timeout cannot usually exceed 75 seconds. // It should be noted that this timeout cannot usually exceed 75 seconds.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_connect_timeout
ProxyConnectTimeout int `json:"proxy-connect-timeout"` ProxyConnectTimeout string `json:"proxy-connect-timeout"`
// Timeout in seconds for reading a response from the proxied server. The timeout is set only between // Timeout in seconds for reading a response from the proxied server. The timeout is set only between
// two successive read operations, not for the transmission of the whole response // two successive read operations, not for the transmission of the whole response
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout
ProxyReadTimeout int `json:"proxy-read-timeout"` ProxyReadTimeout string `json:"proxy-read-timeout"`
// Timeout in seconds for transmitting a request to the proxied server. The timeout is set only between // Timeout in seconds for transmitting a request to the proxied server. The timeout is set only between
// two successive write operations, not for the transmission of the whole request. // two successive write operations, not for the transmission of the whole request.
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_send_timeout
ProxySendTimeout int `json:"proxy-send-timeout"` ProxySendTimeout string `json:"proxy-send-timeout"`
// Sets the number of the buffers used for reading a response from the proxied server // 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 // http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers

View file

@ -1443,9 +1443,9 @@ stream {
{{ $proxySetHeader }} {{ $k }} {{ $v | quote }}; {{ $proxySetHeader }} {{ $k }} {{ $v | quote }};
{{ end }} {{ end }}
proxy_connect_timeout {{ $location.Proxy.ConnectTimeout }}s; proxy_connect_timeout {{ $location.Proxy.ConnectTimeout }};
proxy_send_timeout {{ $location.Proxy.SendTimeout }}s; proxy_send_timeout {{ $location.Proxy.SendTimeout }};
proxy_read_timeout {{ $location.Proxy.ReadTimeout }}s; proxy_read_timeout {{ $location.Proxy.ReadTimeout }};
proxy_buffering {{ $location.Proxy.ProxyBuffering }}; proxy_buffering {{ $location.Proxy.ProxyBuffering }};
proxy_buffer_size {{ $location.Proxy.BufferSize }}; proxy_buffer_size {{ $location.Proxy.BufferSize }};