mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 12:36:40 +00:00
Fix buffer dead-lock in Watchdog (#33)
* Go sync group to handle blocking on buffered-pipes
This commit is contained in:
@ -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)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user