最近一个 golang 写的 http.client 的,获取远程服务器数据,有时候会报错,尤其在数量很大的时候,老是收到 Connection reset by peer 这样的 提醒,都有点想用重试机制

百度,goolge 了一翻后,说的是 它会阻止连接被重用,可以有效的防止这个问题,也就是Http的短连接

1.在客户端关闭 http 连接

func main() {
req, err := http.NewRequest("GET", "http://localhost",nil )
if err != nil {
log.Errorf("")
}
req.Close = true
resp, err := http.Client.Do(req)
...
}

  

  1. 在头部设置连接状态为关闭

    func main() {
    req, err := http.NewRequest("GET", "http://localhost",nil )
    if err != nil {
    log.Errorf("")
    }
    req.Header.Add("Connection", "close")
    resp, err := http.Client.Do(req)
    ...
    }
    1. 使用 Transport 取消 HTTP利用连接

      func main() {
      tr := http.Transport{DisableKeepAlives: true}
      client := http.Client{Transport: &tr} resp, err := client.Get("https://golang.google.cn/")
      if resp != nil {
      defer resp.Body.Close()
      }
      checkError(err) fmt.Println(resp.StatusCode) // 200 body, err := ioutil.ReadAll(resp.Body)
      checkError(err) fmt.Println(len(string(body)))
      }

        

      服务器端设置短连接

    2. func main(){
      server := &http.Server{
      Handler:handle,
      ReadTimeout: 20 * time.Second,
      WriteTimeout: 20 * time.Second,
      }
      listen, err := net.Listen("tcp4",s.addr)
      if err != nil {
      logger.Errorf("Failed to listen,err:%s",err.Error())
      panic(err)
      }
      server.SetKeepAlivesEnabled(false)
      err = server.Serve(listen)
      }

        

    参考 了

  2. https://blog.csdn.net/cyberspecter/article/details/83308348  

  3. https://my.oschina.net/shou1156226/blog/808613

golang http.client 遇到了 Connection reset by peer 问题的更多相关文章

  1. ”Connection reset by peer“引发的思考

    闲来无事,把之前写的一个游戏服务器框架(<一个java页游服务器框架>),部署到阿里云服务器上,测试运行了下,结果看到后台log中打印出了“Connection reset by peer ...

  2. Connection reset by peer问题分析

    extremetable导出excel,弹出一个下载窗口,这时不点下载而点取消,则报下面的异常: ClientAbortException Caused by: java.net.SocketExce ...

  3. pytorch bug: for step,data in enumerate(loader)+Connection reset by peer

    单GPU跑的程序,而且是在docker中,迭代了几百步后,程序突然崩掉了, 程序停在了 for step,data in enumerate(loader),下面是部分bug信息 Traceback ...

  4. Netty 中 IOException: Connection reset by peer 与 java.nio.channels.ClosedChannelException: null

    最近发现系统中出现了很多 IOException: Connection reset by peer 与 ClosedChannelException: null 深入看了看代码, 做了些测试, 发现 ...

  5. Error -27780: Connection reset by peer: socket write error

    Problem Description: Error: "-27780: read to host failed: [10054] Connection reset by peer" ...

  6. nginx php fastcgi Connection reset by peer的原因及解决办法

    Connection reset by peer 这个错误是在nginx的错误日志中发现的,为了更全面的掌握nginx运行的异常,强烈建议在nginx的全局配置中增加 error_log   logs ...

  7. python使用pika链接rabbitmq Connection reset by peer 解决办法

    记录一下, 最近在用机器学习打算做一个Rest API, 数据存入mongo,任务采用消息队列,rabbitmq 由于引擎采用python编写,所以WEB也直接打算用python编写了,比较省事. W ...

  8. 重新分析connection reset by peer, socket write error错误原因

    上次写<connection reset by peer, socket write error问题排查>已经过去大半年,当时把问题"敷衍"过去了. 但是此后每隔一段时 ...

  9. nginx错误分析 `104: Connection reset by peer`

    故障描述 应用从虚拟机环境迁移到kubernetes环境中,有些应用不定时出现请求失败的情况,且应用没有记录任何日志,而在NGINX中记录502错误.我们查看了之前虚拟机中的访问情况,没有发现该问题. ...

随机推荐

  1. 牛客小白月赛13 小A买彩票 (记忆化搜索)

    链接:https://ac.nowcoder.com/acm/contest/549/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  2. 主成分分析_PCA解释

    粘贴自:http://blog.codinglabs.org/articles/pca-tutorial.html 数据的向量表示及降维问题 向量的表示及基变换 协方差矩阵及优化目标 协方差矩阵对角化 ...

  3. 分布式锁与实现(一)——基于Redis实现 【比较靠谱】

    转: 分布式锁与实现(一)——基于Redis实现 概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统 ...

  4. linux同步测试机文件到开发机

    rsync -vrtL --progress /bckup/* root@192.168.1.101:/bckup/ 参考博客: https://www.cnblogs.com/liuquan/p/5 ...

  5. Lending Club—构建贷款违约预测模型

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  6. 前端面试题整理—Node篇

    1.node有哪些特征,与其他服务器端对比 特征:单线程.事件驱动.非阻塞I/O node 无法直接渲染静态页面,提供静态服务 node 没有根目录的概念 node 必须通过路由程序指定文件才能渲染文 ...

  7. jmeter中的函数

    1.函数帮助 1.tool-------->function helper dialog 可以查看函数帮助 2.jmeter中的函数 1.函数格式   ${__funcctionname(var ...

  8. Unknown system variable 'query_cache_size'] with root cause

    Unknown system variable 'query_cache_size'] with root cause 出现这个错误是因为mysql连接数据库的版本不对, mysql-connecto ...

  9. jQuery mouse and lunbo

    自动轮播 和 鼠标事件var arr = ["images/d.jpg", "images/q.jpg", "images/c.jpg", ...

  10. app接口中Http请求头示例

    POST /v5/simProduct/getProductDetails HTTP/1.1 Accept:image/jsonHost: netroam.mihome.xiaomi.netUser- ...