dqve65954 2018-08-27 15:02
浏览 69
已采纳

Golang频道串流

I am relatively new to Golang and don't fully understand streams. I have a function (circuit breaker function) that is making Rest calls. I have it working but it is only streaming the "responseBody" back. I would actually like to stream back the entire request of stream back both the Body and the Header together.

When I try to use a similar approach on the "header" then I get an error that the header is not streamable.

Is there a good/best way to accomplish this? Thank you. Below is my function.

func CallWithRetries(req *http.Request, output chan []byte) error {
    r := retrier.New(retrier.ConstantBackoff(RETRIES, 100 * time.Millisecond), nil)
    attempt := 0
    err := r.Run(func() error {
        attempt++
        resp, err := Client.Do(req)
        if err == nil && resp.StatusCode < 299 {
            responseBody, err := ioutil.ReadAll(resp.Body)
            if err == nil {
                output <- responseBody
                return err
            }
            return err
        } else if err == nil {
            customLogger.LogDebug("Status code was: " , transactionId)
            err = fmt.Errorf("Status was %v", resp.StatusCode)
        }
        return err
    })
    return err
}
  • 写回答

1条回答 默认 最新

  • dongzhang6544 2018-08-27 17:17
    关注

    You are looking for the httputil.DumpResponse function.

    The code might be changed to something similar to

    func CallWithRetries(req *http.Request, output chan []byte) error {
        r := retrier.New(retrier.ConstantBackoff(RETRIES, 100*time.Millisecond), nil)
        attempt := 0
        err := r.Run(func() error {
            attempt++
            resp, err := Client.Do(req)
            if err == nil && resp.StatusCode < 299 {
                dump, err := httputil.DumpResponse(resp, true)
                if err == nil {
                    output <- dump
                    return err
                }
                return err
            } else if err == nil {
                customLogger.LogDebug("Status code was: ", transactionId)
                err = fmt.Errorf("Status was %v", resp.StatusCode)
            }
            return err
        })
        return err
    }
    

    Side notes,

    • you might want to consider to close the response body as mentioned in the documentation https://golang.org/pkg/net/http/#Client.Get

    • Looks likes the err variable is shadowed, this should be modified to avoid any surprises.

    This version of the code attempts to return errors early, and to close the response body. It was not tested, only written on the fly, to use with care.

    func CallWithRetries(req *http.Request, output chan []byte) error {
        r := retrier.New(retrier.ConstantBackoff(RETRIES, 100*time.Millisecond), nil)
        attempt := 0
        return r.Run(func() error {
            attempt++
            var resp *http.Response
            {
                r, err := Client.Do(req)
                if err != nil {
                    return err
                }
                defer r.Body.Close()
                if resp.StatusCode > 299 {
                    customLogger.LogDebug("Status code was: ", transactionId)
                    return fmt.Errorf("Status was %v", resp.StatusCode)
                }
                resp = r
            }
            var out []byte
            {
                x, err := httputil.DumpResponse(resp, true)
                if err != nil {
                    return err
                }
                out = x
            }
            output <- out
            return nil
        })
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵