Skip to content

Commit

Permalink
Fix afterburn for writing to response
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Nov 3, 2017
1 parent 651a6b1 commit d9638c5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New(env []string) (WatchdogConfig, error) {
FunctionProcess: os.Getenv("fprocess"),
InjectCGIHeaders: true,
HardTimeout: 5 * time.Second,
OperationalMode: ModeAfterBurn,
OperationalMode: ModeStreaming,
}

envMap := mapEnv(env)
Expand Down
7 changes: 4 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func Test_OperationalMode_AfterBurn(t *testing.T) {
"mode=afterburn",
}

defaults, err := New(env)
actual, err := New(env)
if err != nil {
t.Errorf("Expected no errors")
}
if defaults.OperationalMode != ModeStreaming {
t.Errorf("Want %s. got: %s", WatchdogMode(ModeStreaming), WatchdogMode(defaults.OperationalMode))

if actual.OperationalMode != ModeAfterBurn {
t.Errorf("Want %s. got: %s", WatchdogMode(ModeAfterBurn), WatchdogMode(actual.OperationalMode))
}
}
34 changes: 26 additions & 8 deletions functions/afterburn_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package functions
import (
"bufio"
"io"
"io/ioutil"
"log"
"net/http"
"os/exec"
Expand Down Expand Up @@ -61,26 +62,43 @@ func (f *AfterBurnFunctionRunner) Start() error {

// Run a function with a long-running process with a HTTP protocol for communication
func (f *AfterBurnFunctionRunner) Run(req FunctionRequest, contentLength int64, r *http.Request, w http.ResponseWriter) error {
buffReader := bufio.NewReader(f.StdoutPipe)

// Submit body to function via stdin
writeErr := r.Write(f.StdinPipe)

if writeErr != nil {
return writeErr
}

processRes, err := http.ReadResponse(buffReader, r)
if err != nil {
return err
var processRes *http.Response

// Read response back from stdout
buffReader := bufio.NewReader(f.StdoutPipe)
var err1 error
processRes, err1 = http.ReadResponse(buffReader, r)
if err1 != nil {
return err1
}

if processRes.Body != nil {
defer processRes.Body.Close()
for h := range processRes.Header {
w.Header().Set(h, processRes.Header.Get(h))
}

w.WriteHeader(processRes.StatusCode)
processRes.Write(w)
if processRes.Body != nil {
defer processRes.Body.Close()
bodyBytes, bodyErr := ioutil.ReadAll(processRes.Body)
if bodyErr != nil {
log.Println("read body err", bodyErr)
}

log.Printf("%s %s - %s - ContentLength: %d\n", r.Method, r.RequestURI, processRes.Status, processRes.ContentLength)
// processRes.Write(w)
w.Write(bodyBytes)
}

if processRes != nil {
log.Printf("%s %s - %s - ContentLength: %d\n", r.Method, r.RequestURI, processRes.Status, processRes.ContentLength)
}

return nil
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ func makeAfterBurnRequestHandler(watchdogConfig config.WatchdogConfig) func(http
InputReader: r.Body,
OutputWriter: w,
}

functionInvoker.Mutex.Lock()

err := functionInvoker.Run(req, r.ContentLength, r, w)

if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}

functionInvoker.Mutex.Unlock()
}
}
Expand Down

0 comments on commit d9638c5

Please sign in to comment.