Skip to content

Commit

Permalink
User getters to unify default value behavior for FaaSConfig
Browse files Browse the repository at this point in the history
**What**
- Add getters for ReadTimeout MaxIdleConns and MaxIdleConnsPerHost so
  that the usage with support for the defaults can be used consistently

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
  • Loading branch information
LucasRoesler authored and alexellis committed Oct 16, 2019
1 parent 1df1c4e commit 78c25aa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
10 changes: 5 additions & 5 deletions proxy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_NewHandlerFunc_Panic(t *testing.T) {
}
}()

config := types.FaaSConfig{ReadTimeout: time.Second}
config := types.FaaSConfig{ReadTimeout: 100 * time.Millisecond}
NewHandlerFunc(config, nil)
}

Expand All @@ -48,15 +48,15 @@ func Test_NewHandlerFunc_NoPanic(t *testing.T) {
}
}()

config := types.FaaSConfig{ReadTimeout: time.Second}
config := types.FaaSConfig{ReadTimeout: 100 * time.Millisecond}
proxyFunc := NewHandlerFunc(config, &testBaseURLResolver{})
if proxyFunc == nil {
t.Errorf("proxy handler func is nil")
}
}

func Test_ProxyHandler_NonAllowedMethods(t *testing.T) {
config := types.FaaSConfig{ReadTimeout: time.Second}
config := types.FaaSConfig{ReadTimeout: 100 * time.Millisecond}
proxyFunc := NewHandlerFunc(config, &testBaseURLResolver{})

nonAllowedMethods := []string{
Expand All @@ -77,7 +77,7 @@ func Test_ProxyHandler_NonAllowedMethods(t *testing.T) {
}

func Test_ProxyHandler_MissingFunctionNameError(t *testing.T) {
config := types.FaaSConfig{ReadTimeout: time.Second}
config := types.FaaSConfig{ReadTimeout: 100 * time.Millisecond}
proxyFunc := NewHandlerFunc(config, &testBaseURLResolver{"", nil})

w := httptest.NewRecorder()
Expand All @@ -102,7 +102,7 @@ func Test_ProxyHandler_ResolveError(t *testing.T) {

resolveErr := errors.New("can not find test service `foo`")

config := types.FaaSConfig{ReadTimeout: time.Second}
config := types.FaaSConfig{ReadTimeout: 100 * time.Millisecond}
proxyFunc := NewHandlerFunc(config, &testBaseURLResolver{"", resolveErr})

w := httptest.NewRecorder()
Expand Down
17 changes: 1 addition & 16 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,7 @@ func NewHandlerFunc(config types.FaaSConfig, resolver BaseURLResolver) http.Hand
// NewProxyClientFromConfig creates a new http.Client designed for proxying requests and enforcing
// certain minimum configuration values.
func NewProxyClientFromConfig(config types.FaaSConfig) *http.Client {
maxIdleConns := config.MaxIdleConns
if maxIdleConns < 1 {
maxIdleConns = 1024
}

maxIdleConnsPerHost := config.MaxIdleConnsPerHost
if maxIdleConnsPerHost < 1 {
maxIdleConnsPerHost = 1024
}

timeout := config.ReadTimeout
if timeout <= 0*time.Second {
timeout = 10 * time.Second
}

return NewProxyClient(timeout, maxIdleConns, maxIdleConnsPerHost)
return NewProxyClient(config.GetReadTimeout(), config.GetMaxIdleConns(), config.GetMaxIdleConnsPerHost())
}

// NewProxyClient creates a new http.Client designed for proxying requests, this is exposed as a
Expand Down
32 changes: 32 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"time"
)

const (
defaultReadTimeout = 10 * time.Second
defaultMaxIdleConns = 1024
)

// FaaSHandlers provide handlers for OpenFaaS
type FaaSHandlers struct {
// FunctionProxy provides the function invocation proxy logic. Use proxy.NewHandlerFunc to
Expand Down Expand Up @@ -48,3 +53,30 @@ type FaaSConfig struct {
// MaxIdleConnsPerHost with a default value of 1024, can be used for tuning HTTP proxy performance.
MaxIdleConnsPerHost int
}

// GetReadTimeout is a helper to safely return the configured ReadTimeout or the default value of 10s
func (c *FaaSConfig) GetReadTimeout() time.Duration {
if c.ReadTimeout <= 0*time.Second {
return defaultReadTimeout
}
return c.ReadTimeout
}

// GetMaxIdleConns is a helper to safely return the configured MaxIdleConns or the default value of 1024
func (c *FaaSConfig) GetMaxIdleConns() int {
if c.MaxIdleConns < 1 {
return defaultMaxIdleConns
}

return c.MaxIdleConns
}

// GetMaxIdleConns is a helper to safely return the configured MaxIdleConns or the default value which
// should then match the MaxIdleConns
func (c *FaaSConfig) GetMaxIdleConnsPerHost() int {
if c.MaxIdleConnsPerHost < 1 {
return c.GetMaxIdleConns()
}

return c.MaxIdleConnsPerHost
}

0 comments on commit 78c25aa

Please sign in to comment.