From 955b294bc678c6d781db30aff3cde85e326710c2 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Sat, 30 Sep 2017 01:01:16 +0300 Subject: [PATCH] Trying to avoid any buffering --- api/agent/protocol/json.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/api/agent/protocol/json.go b/api/agent/protocol/json.go index bb55441cd..0e2bc2f7e 100644 --- a/api/agent/protocol/json.go +++ b/api/agent/protocol/json.go @@ -1,7 +1,6 @@ package protocol import ( - "bufio" "bytes" "encoding/json" "fmt" @@ -9,7 +8,7 @@ import ( "net/http" ) -// JSONIn is what's sent into the function +// This is sent into the function // All HTTP request headers should be set in env type JSONIO struct { Headers http.Header `json:"headers,omitempty"` @@ -40,29 +39,26 @@ func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { } defer req.Body.Close() } - - // convert to JSON func format - jin := &JSONIO{ + err := json.NewEncoder(h.in).Encode(&JSONIO{ Headers: req.Header, Body: body.String(), - } - err := json.NewEncoder(h.in).Encode(&jin) + }) if err != nil { // this shouldn't happen return respondWithError( w, fmt.Errorf("error marshalling JSONInput: %s", err.Error())) } + jout := new(JSONIO) + dec := json.NewDecoder(h.out) + if err := dec.Decode(jout); err != nil { + return respondWithError( + w, fmt.Errorf("unable to decode JSON response object: %s", err.Error())) + } if rw, ok := w.(http.ResponseWriter); ok { // this has to be done for pulling out: // - status code // - body - jout := new(JSONIO) - dec := json.NewDecoder(h.out) - if err := dec.Decode(jout); err != nil { - return respondWithError( - w, fmt.Errorf("unable to decode JSON response object: %s", err.Error())) - } rw.WriteHeader(jout.StatusCode) _, err = rw.Write([]byte(jout.Body)) // TODO timeout if err != nil { @@ -71,10 +67,10 @@ func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { } } else { // logs can just copy the full thing in there, headers and all. - _, err = io.Copy(w, bufio.NewReader(h.out)) + err = json.NewEncoder(w).Encode(jout) if err != nil { return respondWithError( - w, fmt.Errorf("error reading function response: %s", err.Error())) + w, fmt.Errorf("error writing function response: %s", err.Error())) } } return nil