Add GatewayConfig

Add env configurable readTimeout / writeTimeout
Add env-timeout test
Modify appropriate Dockerfiles

Signed-off-by: leigh schrandt <leigh@null.net>
This commit is contained in:
leigh schrandt 2017-07-02 04:47:29 -06:00 committed by Alex Ellis
parent a9774a9c2a
commit 457d0be78b
7 changed files with 167 additions and 31 deletions

View File

@ -10,13 +10,16 @@ FROM golang:1.7.5
WORKDIR /go/src/github.com/alexellis/faas/gateway WORKDIR /go/src/github.com/alexellis/faas/gateway
COPY vendor vendor COPY vendor vendor
COPY handlers handlers COPY handlers handlers
COPY metrics metrics COPY metrics metrics
COPY requests requests COPY requests requests
COPY tests tests COPY tests tests
COPY server.go . COPY server.go .
COPY readconfig.go .
COPY config_test.go .
RUN go test -v ./tests && \ RUN go test && \
go test -v ./tests && \
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

View File

@ -10,12 +10,15 @@ RUN go get -d github.com/Sirupsen/logrus
WORKDIR /go/src/github.com/alexellis/faas/gateway WORKDIR /go/src/github.com/alexellis/faas/gateway
COPY metrics metrics COPY metrics metrics
COPY requests requests COPY requests requests
COPY tests tests COPY tests tests
COPY handlers handlers COPY handlers handlers
COPY server.go . COPY server.go .
COPY readconfig.go .
COPY config_test.go .
RUN go test -v ./tests && \ RUN go test && \
go test -v ./tests && \
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

View File

@ -1,13 +1,15 @@
FROM golang:1.7.5 FROM golang:1.7.5
WORKDIR /go/src/github.com/alexellis/faas/gateway WORKDIR /go/src/github.com/alexellis/faas/gateway
COPY vendor vendor COPY vendor vendor
COPY handlers handlers COPY handlers handlers
COPY metrics metrics COPY metrics metrics
COPY requests requests COPY requests requests
COPY tests tests COPY tests tests
COPY server.go . COPY server.go .
COPY readconfig.go .
COPY config_test.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway .

View File

@ -2,13 +2,15 @@ FROM golang:1.7.5
# RUN mkdir -p /go/src/github.com/alexellis/faas/gateway # RUN mkdir -p /go/src/github.com/alexellis/faas/gateway
WORKDIR /go/src/github.com/alexellis/faas/gateway WORKDIR /go/src/github.com/alexellis/faas/gateway
COPY vendor vendor COPY vendor vendor
COPY handlers handlers COPY handlers handlers
COPY metrics metrics COPY metrics metrics
COPY requests requests COPY requests requests
COPY tests tests COPY tests tests
COPY server.go . COPY server.go .
COPY readconfig.go .
COPY config_test.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o gateway .

61
gateway/config_test.go Normal file
View File

@ -0,0 +1,61 @@
// 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 main
import (
"testing"
"time"
)
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 := ReadConfig{}
config := readConfig.Read(defaults)
if (config.readTimeout) != time.Duration(8)*time.Second {
t.Log("readTimeout incorrect")
t.Fail()
}
if (config.writeTimeout) != time.Duration(8)*time.Second {
t.Log("writeTimeout incorrect")
t.Fail()
}
}
func TestRead_ReadAndWriteTimeoutConfig(t *testing.T) {
defaults := NewEnvBucket()
defaults.Setenv("read_timeout", "10")
defaults.Setenv("write_timeout", "60")
readConfig := 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()
}
}

61
gateway/readconfig.go Normal file
View File

@ -0,0 +1,61 @@
package main
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 parseBoolValue(val string) bool {
if val == "true" {
return true
}
return false
}
func parseIntValue(val string, fallback int) int {
if len(val) > 0 {
parsedVal, parseErr := strconv.Atoi(val)
if parseErr == nil && parsedVal >= 0 {
return parsedVal
}
}
return fallback
}
// Read fetches config from environmental variables.
func (ReadConfig) Read(hasEnv HasEnv) GatewayConfig {
cfg := GatewayConfig{}
readTimeout := parseIntValue(hasEnv.Getenv("read_timeout"), 8)
writeTimeout := parseIntValue(hasEnv.Getenv("write_timeout"), 8)
cfg.readTimeout = time.Duration(readTimeout) * time.Second
cfg.writeTimeout = time.Duration(writeTimeout) * time.Second
return cfg
}
// GatewayConfig for the process.
type GatewayConfig struct {
readTimeout time.Duration
writeTimeout time.Duration
}

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"log" "log"
"net/http" "net/http"
"time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
faasHandlers "github.com/alexellis/faas/gateway/handlers" faasHandlers "github.com/alexellis/faas/gateway/handlers"
@ -20,6 +19,13 @@ func main() {
logger := logrus.Logger{} logger := logrus.Logger{}
logrus.SetFormatter(&logrus.TextFormatter{}) logrus.SetFormatter(&logrus.TextFormatter{})
osEnv := OsEnv{}
readConfig := ReadConfig{}
config := readConfig.Read(osEnv)
log.Printf("HTTP Read Timeout: %s", config.readTimeout)
log.Printf("HTTP Write Timeout: %s", config.writeTimeout)
var dockerClient *client.Client var dockerClient *client.Client
var err error var err error
dockerClient, err = client.NewEnvClient() dockerClient, err = client.NewEnvClient()
@ -30,7 +36,7 @@ func main() {
if err != nil { if err != nil {
log.Fatal("Error with Docker server.\n", err) log.Fatal("Error with Docker server.\n", err)
} }
log.Printf("API version: %s, %s\n", dockerVersion.APIVersion, dockerVersion.Version) log.Printf("Docker API version: %s, %s\n", dockerVersion.APIVersion, dockerVersion.Version)
metricsOptions := metrics.BuildMetricsOptions() metricsOptions := metrics.BuildMetricsOptions()
metrics.RegisterMetrics(metricsOptions) metrics.RegisterMetrics(metricsOptions)
@ -61,14 +67,12 @@ func main() {
r.Handle("/", http.RedirectHandler("/ui/", http.StatusMovedPermanently)).Methods("GET") r.Handle("/", http.RedirectHandler("/ui/", http.StatusMovedPermanently)).Methods("GET")
readTimeout := 8 * time.Second
writeTimeout := 8 * time.Second
tcpPort := 8080 tcpPort := 8080
s := &http.Server{ s := &http.Server{
Addr: fmt.Sprintf(":%d", tcpPort), Addr: fmt.Sprintf(":%d", tcpPort),
ReadTimeout: readTimeout, ReadTimeout: config.readTimeout,
WriteTimeout: writeTimeout, WriteTimeout: config.writeTimeout,
MaxHeaderBytes: http.DefaultMaxHeaderBytes, // 1MB - can be overridden by setting Server.MaxHeaderBytes. MaxHeaderBytes: http.DefaultMaxHeaderBytes, // 1MB - can be overridden by setting Server.MaxHeaderBytes.
Handler: r, Handler: r,
} }