From 8aa67b65294ebe0006793b64e07d93239d2dc589 Mon Sep 17 00:00:00 2001 From: gusaul Date: Mon, 2 Nov 2020 20:02:07 +0700 Subject: [PATCH] update pipe commit output to handling huge output --- core/resource.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core/resource.go b/core/resource.go index 04819d1..6cbc461 100644 --- a/core/resource.go +++ b/core/resource.go @@ -204,6 +204,15 @@ func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (str } 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() + }() + rf, formatter, err := grpcurl.RequestParserAndFormatterFor(grpcurl.Format("json"), r.descSource, false, true, in) if err != nil { return "", 0, err @@ -213,6 +222,7 @@ func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (str 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 } @@ -221,15 +231,6 @@ func (r *Resource) Invoke(ctx context.Context, symbol string, in io.Reader) (str return "", end, fmt.Errorf(h.Status.Message()) } - // copy the output in a separate goroutine so printing can't block indefinitely - outC := make(chan string) - go func() { - var buf bytes.Buffer - io.Copy(&buf, f) - outC <- buf.String() - }() - - w.Close() out := <-outC return out, end, nil