Skip to content

Commit d9638c5

Browse files
committed
Fix afterburn for writing to response
Signed-off-by: Alex Ellis <[email protected]>
1 parent 651a6b1 commit d9638c5

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func New(env []string) (WatchdogConfig, error) {
3535
FunctionProcess: os.Getenv("fprocess"),
3636
InjectCGIHeaders: true,
3737
HardTimeout: 5 * time.Second,
38-
OperationalMode: ModeAfterBurn,
38+
OperationalMode: ModeStreaming,
3939
}
4040

4141
envMap := mapEnv(env)

config/config_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ func Test_OperationalMode_AfterBurn(t *testing.T) {
2727
"mode=afterburn",
2828
}
2929

30-
defaults, err := New(env)
30+
actual, err := New(env)
3131
if err != nil {
3232
t.Errorf("Expected no errors")
3333
}
34-
if defaults.OperationalMode != ModeStreaming {
35-
t.Errorf("Want %s. got: %s", WatchdogMode(ModeStreaming), WatchdogMode(defaults.OperationalMode))
34+
35+
if actual.OperationalMode != ModeAfterBurn {
36+
t.Errorf("Want %s. got: %s", WatchdogMode(ModeAfterBurn), WatchdogMode(actual.OperationalMode))
3637
}
3738
}

functions/afterburn_runner.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package functions
33
import (
44
"bufio"
55
"io"
6+
"io/ioutil"
67
"log"
78
"net/http"
89
"os/exec"
@@ -61,26 +62,43 @@ func (f *AfterBurnFunctionRunner) Start() error {
6162

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

66+
// Submit body to function via stdin
6667
writeErr := r.Write(f.StdinPipe)
68+
6769
if writeErr != nil {
6870
return writeErr
6971
}
7072

71-
processRes, err := http.ReadResponse(buffReader, r)
72-
if err != nil {
73-
return err
73+
var processRes *http.Response
74+
75+
// Read response back from stdout
76+
buffReader := bufio.NewReader(f.StdoutPipe)
77+
var err1 error
78+
processRes, err1 = http.ReadResponse(buffReader, r)
79+
if err1 != nil {
80+
return err1
7481
}
7582

76-
if processRes.Body != nil {
77-
defer processRes.Body.Close()
83+
for h := range processRes.Header {
84+
w.Header().Set(h, processRes.Header.Get(h))
7885
}
7986

8087
w.WriteHeader(processRes.StatusCode)
81-
processRes.Write(w)
88+
if processRes.Body != nil {
89+
defer processRes.Body.Close()
90+
bodyBytes, bodyErr := ioutil.ReadAll(processRes.Body)
91+
if bodyErr != nil {
92+
log.Println("read body err", bodyErr)
93+
}
8294

83-
log.Printf("%s %s - %s - ContentLength: %d\n", r.Method, r.RequestURI, processRes.Status, processRes.ContentLength)
95+
// processRes.Write(w)
96+
w.Write(bodyBytes)
97+
}
98+
99+
if processRes != nil {
100+
log.Printf("%s %s - %s - ContentLength: %d\n", r.Method, r.RequestURI, processRes.Status, processRes.ContentLength)
101+
}
84102

85103
return nil
86104
}

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@ func makeAfterBurnRequestHandler(watchdogConfig config.WatchdogConfig) func(http
6969
InputReader: r.Body,
7070
OutputWriter: w,
7171
}
72+
7273
functionInvoker.Mutex.Lock()
7374

7475
err := functionInvoker.Run(req, r.ContentLength, r, w)
76+
7577
if err != nil {
7678
w.WriteHeader(500)
7779
w.Write([]byte(err.Error()))
7880
}
81+
7982
functionInvoker.Mutex.Unlock()
8083
}
8184
}

0 commit comments

Comments
 (0)