dpb75177 2018-10-17 11:17
浏览 219

客户端重新连接到我的TCP服务器时内存不断增加

I'm new to Go and I'm trying to build a simple TCP server. I've attached my code below. Whenever a client disconnects, it will be added to server.deadConns.

If I use a simple Client and keep connecting and disconnecting to the TCP Server (code below), the memory usage of my Golang app just keeps going up. If I then stop connecting and reconnecting my Client and wait, the memory usage will slowly start to go down. It takes a long time for it to go down but it will.

I noticed this issue when I was running my TCP server on Azure and the memory usage on the VM just kept going up until the machine crashed. I checked the logs and saw that there was some clients that just kept connecting and disconnecting all the time. I then tried it locally on my own machine and noticed that the same thing happens.

//NewTCPServer Creates the TCP Server
func NewTCPServer(port string, logger *logrus.Logger) *TCPServer {
    server := new(TCPServer)
    server.socketAPI = socketapi.NewSocketAPI(logger)
    newConns := make(chan net.Conn, 128)
    server.deadConns = make(chan net.Conn, 128)
    server.sessions = make(map[net.Conn]*types.ClientSession)
    fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000") + ": Listening to: " + port)
    listener, err := net.Listen("tcp", ":"+port)
    if err != nil {
        panic(err)
    }
    go func() {
        for {
            conn, err := listener.Accept()
            if err != nil {
                panic(err)
            }
            fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000") + ": Connected: " + conn.RemoteAddr().String())
            newConns <- conn
        }
    }()

    go func(server *TCPServer) {
        for {
            select {
            case conn := <-newConns:
                session := new(types.ClientSession)
                tcp := createTCPSocket(&conn, server)
                session.Socket = tcp
                server.sessions[conn] = session
            case deadConn := <-server.deadConns:
                sess, ok := server.sessions[deadConn]

                if ok {
                    fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000") + ": Found session")
                    if server.socketAPI.StopSubscriptionOnClose(sess) {
                        delete(server.sessions, deadConn)
                    }
                }

                fmt.Println(time.Now().Format("2006-01-02 15:04:05.000000") + ": Delete session!")
                _ = deadConn.Close()
                sess.Socket = nil
                sess = nil
            }
        }
    }(server)
    return server
}
  • 写回答

1条回答 默认 最新

  • dsbruqxgt820011351 2018-10-17 12:29
    关注

    I just found the error. I didn't close a channel in my TCP server code.

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题