为什么在Go语言中,TCP保持活动(TCP Keep-Alive)会影响TCP关闭(TCP Close)?

huangapple go评论84阅读模式
英文:

why does tcp-keep-alive affect the tcp-close in go?

问题

我有一个服务器,在接受连接时,我设置了tcp-keep-alive为120秒。但是当我关闭连接时,实际上连接并没有关闭。通过运行netstat -anp | grep 9999命令,我发现连接的状态是ESTABLISHED。而且客户端也没有从套接字接收到任何错误。我想知道tcp-keep-alive会影响tcp-close吗?

PS:go版本为1.4,操作系统为CentOS。

package main

import (
    "github.com/felixge/tcpkeepalive"
    "net"
    "runtime"
    "time"
)

func Start() {
    tcpAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:9999")
    if err != nil {
        return
    }
    listener, err := net.ListenTCP("tcp", tcpAddr)
    if err != nil {
        return
    }
    for {
        conn, err := listener.AcceptTCP()
        if err != nil {
            continue
        }
        go handleClient(conn)
    }
}

func handleClient(conn *net.TCPConn) {
    kaConn, err := tcpkeepalive.EnableKeepAlive(conn)
    if err != nil {
    } else {
        kaConn.SetKeepAliveIdle(120 * time.Second)
        kaConn.SetKeepAliveCount(4)
        kaConn.SetKeepAliveInterval(5 * time.Second)
    }
    time.Sleep(time.Second * 3)
    conn.Close()
    return
}

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    Start()
}

以上是你提供的代码。

英文:

I have a server, when accepting a connection, I set tcp-keep-alive for 120seconds.But when I close the connection, acctually the connection doesn't close.by netstat -anp | grep 9999, I found the state was ESTABLISHED .And the client didn't receive any error from the socket , either. I want to know will tcp-keep-alive affect the tcp-close?

PS go 1.4 centos

package main
import (
    "github.com/felixge/tcpkeepalive"
    "net"
    "runtime"
    "time"
)
func  Start() {
    tcpAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:9999")
    if err != nil {
        return
    }
    listener, err := net.ListenTCP("tcp", tcpAddr)
    if err != nil {
        return
    }
    for {
        conn, err := listener.AcceptTCP()
        if err != nil {
            continue
        }
        go handleClient(conn)
    }
}

func handleClient(conn *net.TCPConn) {
    kaConn, err := tcpkeepalive.EnableKeepAlive(conn)
    if err != nil {
    } else {
        kaConn.SetKeepAliveIdle(120 * time.Second)
        kaConn.SetKeepAliveCount(4)
        kaConn.SetKeepAliveInterval(5 * time.Second)
    }
    time.Sleep(time.Second * 3)
    conn.Close()
    return
}

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU())
    Start()
}

答案1

得分: 4

不要使用那个keepalive库。它会复制文件描述符,并且无法关闭它们。

如果你需要设置KeepAlive,请使用net包中提供的方法。

你可能不需要设置任何额外的选项,但只有在确定需要时,你可以尝试使用适当的系统调用来应用所需的内容。

英文:

Don't use that keepalive library. It duplicates file descriptors, and fails to close them.

If you need to set KeepAlive, use the methods provided in the net package.

You likely don't need any extra options set, but only if you're certain you do, then you can try to apply what's needed with the appropriate syscalls.

huangapple
  • 本文由 发表于 2015年2月6日 18:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/28363575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定