Skip to content

Commit

Permalink
Fix timeout error, omit Error when no errors, fix port (#2)
Browse files Browse the repository at this point in the history
Fixed a small bug that broke the timeout error handling. The Error object is no longer included in the json if it is not present. The server will now look for a "PORT" environment variable to set the port; if not present it defaults to 8080.
  • Loading branch information
tenczar authored Jul 25, 2018
1 parent 7387c28 commit 298934b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
const (
serversEnvVar = "SERVERS"
serverCmdEnvVar = "SERVER_CMD"
portEnvVar = "PORT"
)

type funkyHandler struct {
Expand Down Expand Up @@ -91,8 +92,13 @@ func main() {
w.Write([]byte("{}"))
})

port := os.Getenv(portEnvVar)
if port == "" {
port = "8080"
}

server := &http.Server{
Addr: ":8080",
Addr: ":" + port,
Handler: servMux,
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/funky/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type Message struct {

// Context a struct to hold the context of a Dispatch function invocation
type Context struct {
Error *Error `json:"error"`
Logs *Logs `json:"logs"`
Deadline time.Time `json:"deadline,omitempty"`
Error *Error `json:"error,omitempty"`
Logs *Logs `json:"logs"`
Deadline *time.Time `json:"deadline,omitempty"`
}

// Error a struct to hold the error status of a Dispatch function invocation
Expand Down
16 changes: 9 additions & 7 deletions pkg/funky/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *DefaultRouter) Delegate(input *Message) (*Message, error) {
}
}()

var e Error
var e *Error
resp, err := server.Invoke(input)

logs := Logs{
Expand All @@ -77,11 +77,13 @@ func (r *DefaultRouter) Delegate(input *Message) (*Message, error) {
switch v := err.(type) {
case TimeoutError:
defer func() {
recover()
if r := recover(); r != nil {
fmt.Println("recovered", r)
}
}()
terminateErr := server.Terminate()
server = nil
newServer, serverErr := r.serverFactory.CreateServer(server.GetPort())
server = nil
if serverErr != nil || terminateErr != nil {
close(Healthy)
} else {
Expand All @@ -90,23 +92,23 @@ func (r *DefaultRouter) Delegate(input *Message) (*Message, error) {
}
server = newServer
}
e = Error{
e = &Error{
ErrorType: FunctionError,
Message: err.Error(),
}
case FunctionServerError:
e = v.APIError
e = &v.APIError
default:
e = Error{
e = &Error{
ErrorType: SystemError,
Message: err.Error(),
}
}
}

ctx := Context{
Error: e,
Logs: &logs,
Error: &e,
}

response := &Message{
Expand Down
8 changes: 3 additions & 5 deletions pkg/funky/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ func (s *DefaultServer) GetPort() uint16 {
func (s *DefaultServer) Invoke(input *Message) (interface{}, error) {
p, err := json.Marshal(input)

var timeout time.Duration
if (input.Context.Deadline == time.Time{}) {
timeout = 0
} else {
timeout = time.Until(input.Context.Deadline)
timeout := time.Duration(0)
if input.Context.Deadline != nil {
timeout = time.Until(*input.Context.Deadline)
}

if timeout < 0 {
Expand Down

0 comments on commit 298934b

Please sign in to comment.