无法使用流发送块

时间:2018-12-20 10:23:56

标签: go grpc

我正在尝试通过图像处理使用GRPC Client side stream,我也是GRPC流中的新手,这里我将以小块创建图像并将其发送到服务器中,创建块但无法发送。最终我收到EOF错误。

在这里附上示例代码,任何人都可以指导我。

示例:

 func (c *ClientGRPC) UploadFile(ctx context.Context) (stats stats.Stats, err error) {

    var (
        writing = true
        buf     []byte
        n       int
        status  *pb.UploadStatus
    )

    cwd, _ := os.Getwd()
    templatePath := filepath.Join(cwd, "/unnamed.png")

    file, err := os.Open(templatePath)

    if err != nil {
        err = errors.Wrapf(err,
            "failed to open file %s",
            file)
        return
    }

    defer file.Close()

    stream, err := c.client.Upload(ctx)
    if err != nil {
        err = errors.Wrapf(err,
            "failed to create upload stream for file %s",
            file)
        return
    }

    defer stream.CloseSend()



    buf = make([]byte, c.chunkSize)

    for writing {
        n, err = file.Read(buf)

        if err != nil {
            if err == io.EOF {
                writing = false
                err = nil
                continue
            }

            err = errors.Wrapf(err,
                "errored while copying from file to buf")
            return
        }

        err = stream.Send(&pb.Chunk{
            Content: buf[:n],
        })
        if err != nil {
            err = errors.Wrapf(err,
                "failed to send chunk via stream") //`Here, I'm getting EOF error`.
            return
        }
    }



    status, err = stream.CloseAndRecv()
    if err != nil {
        err = errors.Wrapf(err,
            "failed to receive upstream status response")
        return
    }

    if status.Code != pb.UploadStatusCode_Ok {
        err = errors.Errorf(
            "upload failed - msg: %s",
            status.Message)
        return
    }

    return

}

输出:

client=====> failed to send chunk via stream: EOF

1 个答案:

答案 0 :(得分:0)

如果在下面使用grpc,请运行带有环境变量POST的程序,以从grpc获取日志以进行更深入的调试(即,这是连接级问题或流级问题)。