From ec4ba2090dd5e4f3a3bd5d2c9c53b2307c389156 Mon Sep 17 00:00:00 2001 From: gusaul Date: Tue, 3 Nov 2020 09:37:50 +0700 Subject: [PATCH] change invoke handler directly to bytes buffer --- core/resource.go | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/core/resource.go b/core/resource.go index 6cbc461..c2ac7cd 100644 --- a/core/resource.go +++ b/core/resource.go @@ -191,38 +191,17 @@ func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (str } defer r.closeDescriptor() - // because of grpcurl directly fmt.Printf on their invoke function - // so we stub the Stdout using os.Pipe - backUpStdout := os.Stdout - defer func() { - os.Stdout = backUpStdout - }() - - f, w, err := os.Pipe() - if err != nil { - return "", 0, err - } - os.Stdout = w - - // copy the output in a separate goroutine so printing can't block indefinitely - outC := make(chan string, 1) - var buf bytes.Buffer - go func() { - io.Copy(&buf, f) - outC <- buf.String() - f.Close() - }() + var resultBuffer bytes.Buffer rf, formatter, err := grpcurl.RequestParserAndFormatterFor(grpcurl.Format("json"), r.descSource, false, true, in) if err != nil { return "", 0, err } - h := grpcurl.NewDefaultEventHandler(os.Stdout, r.descSource, formatter, false) + h := grpcurl.NewDefaultEventHandler(&resultBuffer, r.descSource, formatter, false) start := time.Now() err = grpcurl.InvokeRPC(ctx, r.descSource, r.clientConn, symbol, r.headers, h, rf.Next) end := time.Now().Sub(start) / time.Millisecond - w.Close() if err != nil { return "", end, err } @@ -231,9 +210,7 @@ func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (str return "", end, fmt.Errorf(h.Status.Message()) } - out := <-outC - - return out, end, nil + return resultBuffer.String(), end, nil } // Close - to close all resources that was opened before