mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 20:46:41 +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,42 +21,61 @@ func (OsEnv) Getenv(key string) string {
|
|||||||
return os.Getenv(key)
|
return os.Getenv(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
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()
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
out, err = targetCmd.CombinedOutput()
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if config.writeDebug == true {
|
||||||
|
log.Println(targetCmd, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(500)
|
||||||
|
response := bytes.NewBufferString(err.Error())
|
||||||
|
w.Write(response.Bytes())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if config.writeDebug == true {
|
||||||
|
os.Stdout.Write(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Match header for strict services
|
||||||
|
if r.Header.Get("Content-Type") == "application/json" {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
}
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Write(out)
|
||||||
|
}
|
||||||
|
|
||||||
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
|
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
parts := strings.Split(config.faasProcess, " ")
|
pipeRequest(config, w, r)
|
||||||
|
|
||||||
targetCmd := exec.Command(parts[0], parts[1:]...)
|
|
||||||
writer, _ := targetCmd.StdinPipe()
|
|
||||||
|
|
||||||
res, _ := ioutil.ReadAll(r.Body)
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
writer.Write(res)
|
|
||||||
writer.Close()
|
|
||||||
|
|
||||||
out, err := targetCmd.CombinedOutput()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if config.writeDebug == true {
|
|
||||||
log.Println(targetCmd, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(500)
|
|
||||||
response := bytes.NewBufferString(err.Error())
|
|
||||||
w.Write(response.Bytes())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if config.writeDebug == true {
|
|
||||||
os.Stdout.Write(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match header for strict services
|
|
||||||
if r.Header.Get("Content-Type") == "application/json" {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
w.WriteHeader(200)
|
|
||||||
w.Write(out)
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user