Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
Adds configs for timeouts, updates year in license
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Feb 25, 2018
1 parent 9f928a6 commit b2a6cb9
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ RUN mkdir -p /go/src/github.com/openfaas/faas-swarm/
WORKDIR /go/src/github.com/openfaas/faas-swarm

COPY vendor vendor
COPY handlers handlers

COPY handlers handlers
COPY types types
COPY server.go .

RUN curl -sL https://github.com/alexellis/license-check/releases/download/0.2.2/license-check > /usr/bin/license-check \
Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2016-2017 Alex Ellis
Copyright (c) 2016-2018 Alex Ellis
Copyright (c) 2017-2018 OpenFaaS Project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +20,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

12 changes: 10 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/openfaas/faas-provider"
bootTypes "github.com/openfaas/faas-provider/types"
"github.com/openfaas/faas-swarm/handlers"
"github.com/openfaas/faas-swarm/types"
)

func main() {
Expand All @@ -37,6 +38,13 @@ func main() {
// Delay between container restarts
restartDelay := time.Second * 5

readConfig := types.ReadConfig{}
osEnv := types.OsEnv{}
cfg := readConfig.Read(osEnv)

log.Printf("HTTP Read Timeout: %s\n", cfg.ReadTimeout)
log.Printf("HTTP Write Timeout: %s\n", cfg.WriteTimeout)

bootstrapHandlers := bootTypes.FaaSHandlers{
FunctionProxy: handlers.FunctionProxy(true, dockerClient),
DeleteHandler: handlers.DeleteHandler(dockerClient),
Expand All @@ -51,8 +59,8 @@ func main() {
var port int
port = 8080
bootstrapConfig := bootTypes.FaaSConfig{
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
TCPPort: &port,
}

Expand Down
82 changes: 82 additions & 0 deletions test/read_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package test

import (
"testing"
"time"

"github.com/openfaas/faas-swarm/types"
)

type EnvBucket struct {
Items map[string]string
}

func NewEnvBucket() EnvBucket {
return EnvBucket{
Items: make(map[string]string),
}
}

func (e EnvBucket) Getenv(key string) string {
return e.Items[key]
}

func (e EnvBucket) Setenv(key string, value string) {
e.Items[key] = value
}

func TestRead_EmptyTimeoutConfig(t *testing.T) {
defaults := NewEnvBucket()
readConfig := types.ReadConfig{}

config := readConfig.Read(defaults)
defaultVal := time.Duration(10) * time.Second

if (config.ReadTimeout) != defaultVal {
t.Logf("ReadTimeout want: %s, got %s", defaultVal.String(), config.ReadTimeout.String())
t.Fail()
}
if (config.WriteTimeout) != defaultVal {
t.Logf("WriteTimeout want: %s, got %s", defaultVal.String(), config.ReadTimeout.String())
t.Fail()
}
}

func TestRead_ReadAndWriteIntegerTimeoutConfig(t *testing.T) {
defaults := NewEnvBucket()
defaults.Setenv("read_timeout", "10")
defaults.Setenv("write_timeout", "60")

readConfig := types.ReadConfig{}
config := readConfig.Read(defaults)

if (config.ReadTimeout) != time.Duration(10)*time.Second {
t.Logf("ReadTimeout incorrect, got: %d\n", config.ReadTimeout)
t.Fail()
}
if (config.WriteTimeout) != time.Duration(60)*time.Second {
t.Logf("WriteTimeout incorrect, got: %d\n", config.WriteTimeout)
t.Fail()
}
}

func TestRead_ReadAndWriteDurationTimeoutConfig(t *testing.T) {
defaults := NewEnvBucket()
defaults.Setenv("read_timeout", "10s")
defaults.Setenv("write_timeout", "60s")

readConfig := types.ReadConfig{}
config := readConfig.Read(defaults)

if (config.ReadTimeout) != time.Duration(10)*time.Second {
t.Logf("ReadTimeout incorrect, got: %d\n", config.ReadTimeout)
t.Fail()
}
if (config.WriteTimeout) != time.Duration(60)*time.Second {
t.Logf("WriteTimeout incorrect, got: %d\n", config.WriteTimeout)
t.Fail()
}
}
2 changes: 1 addition & 1 deletion tests/reader_test.go → test/reader_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tests
package test

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion tests/registryauth_test.go → test/registryauth_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package tests
package test

import (
"encoding/base64"
Expand Down
5 changes: 4 additions & 1 deletion tests/resources_test.go → test/resources_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package tests
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package test

import (
"testing"
Expand Down
69 changes: 69 additions & 0 deletions types/read_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package types

import (
"os"
"strconv"
"time"
)

// OsEnv implements interface to wrap os.Getenv
type OsEnv struct {
}

// Getenv wraps os.Getenv
func (OsEnv) Getenv(key string) string {
return os.Getenv(key)
}

// HasEnv provides interface for os.Getenv
type HasEnv interface {
Getenv(key string) string
}

// ReadConfig constitutes config from env variables
type ReadConfig struct {
}

func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return time.Duration(parsedVal) * time.Second
}
}

duration, durationErr := time.ParseDuration(val)
if durationErr != nil {
return fallback
}

return duration
}

func parseBoolValue(val string, fallback bool) bool {
if len(val) > 0 {
return val == "true"
}
return fallback
}

// Read fetches config from environmental variables.
func (ReadConfig) Read(hasEnv HasEnv) BootstrapConfig {
cfg := BootstrapConfig{}

readTimeout := parseIntOrDurationValue(hasEnv.Getenv("read_timeout"), time.Second*10)
writeTimeout := parseIntOrDurationValue(hasEnv.Getenv("write_timeout"), time.Second*10)

cfg.ReadTimeout = readTimeout
cfg.WriteTimeout = writeTimeout

return cfg
}

// BootstrapConfig for the process.
type BootstrapConfig struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
}

0 comments on commit b2a6cb9

Please sign in to comment.