Fix buffer dead-lock in Watchdog (#33)

* Go sync group to handle blocking on buffered-pipes
This commit is contained in:
Alex Ellis
2017-03-28 19:05:33 +01:00
committed by GitHub
parent badcf7863c
commit c2eb41ee4f

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"sync"
"time" "time"
) )
@ -20,21 +21,34 @@ func (OsEnv) Getenv(key string) string {
return os.Getenv(key) return os.Getenv(key)
} }
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) { func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
parts := strings.Split(config.faasProcess, " ") parts := strings.Split(config.faasProcess, " ")
targetCmd := exec.Command(parts[0], parts[1:]...) targetCmd := exec.Command(parts[0], parts[1:]...)
writer, _ := targetCmd.StdinPipe() writer, _ := targetCmd.StdinPipe()
res, _ := ioutil.ReadAll(r.Body) var out []byte
var err error
var res []byte
var wg sync.WaitGroup
wg.Add(2)
res, _ = ioutil.ReadAll(r.Body)
defer r.Body.Close() defer r.Body.Close()
go func() {
defer wg.Done()
writer.Write(res) writer.Write(res)
writer.Close() writer.Close()
}()
out, err := targetCmd.CombinedOutput() go func() {
defer wg.Done()
out, err = targetCmd.CombinedOutput()
}()
wg.Wait()
if err != nil { if err != nil {
if config.writeDebug == true { if config.writeDebug == true {
@ -56,6 +70,12 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
} }
w.WriteHeader(200) w.WriteHeader(200)
w.Write(out) w.Write(out)
}
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
pipeRequest(config, w, r)
} else { } else {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
} }