dtlh12053 2019-01-23 15:02
浏览 167

Go Transport中的keep-alive TTL永远不会关闭连接

I have an application hosted on AWS which is running on Production creates an http server as mentioned in the below sample code. There is default timeout of 180 seconds in the go library. So, ideally connections not used should be closed after 180 seconds.

myMux := http.NewServeMux()
myMux.Handle("/SOME_PATH", appHandler{myHandler})
err = http.ListenAndServe(viper.GetString("handler.port"), myMux)

The problem is when traffic increases on the application, number of connections increases. But when traffic comes down number of connections remains same.

I am using go version go1.10 linux/amd64 and this application is behind Amazon ALB.

Edited question:

As you can see the rate at which connections are decreasing is very slow when application is behind ALB. So, what could be the problem enter image description here

  • 写回答

1条回答 默认 最新

  • dqg17080 2019-05-11 00:45
    关注

    When you setup your server you should add timeouts appropriate for your application, for example

    srv := &http.Server{
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 20 * time.Second,
        IdleTimeout:  180 * time.Second,
        Handler:      myMux,
    }
    

    With this the idle connections should get turned off. If it's an upstream load balancer sending health checks on that many connections the solution would be different.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算