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/exec"
"strings"
"sync"
"time"
)
@ -20,21 +21,34 @@ func (OsEnv) Getenv(key string) string {
return os.Getenv(key)
}
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) {
parts := strings.Split(config.faasProcess, " ")
targetCmd := exec.Command(parts[0], parts[1:]...)
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()
go func() {
defer wg.Done()
writer.Write(res)
writer.Close()
}()
out, err := targetCmd.CombinedOutput()
go func() {
defer wg.Done()
out, err = targetCmd.CombinedOutput()
}()
wg.Wait()
if err != nil {
if config.writeDebug == true {
@ -56,6 +70,12 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
}
w.WriteHeader(200)
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 {
w.WriteHeader(http.StatusMethodNotAllowed)
}