限制连接到网络服务的客户端数量

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

Limit number of clients connected to a Network Service

问题

如何限制连接到我的服务的客户端数量?
我尝试使用一个简单的计数器,但是如果一个客户端退出而没有关闭他们的连接,我就无法得到它。

请问有人可以给我一些想法来解决这个问题吗?

const MAX_CLIENTS = 5
var ConnectedClients int
func main() {
    ConnectedClients = 0
    server, err := net.Listen(CONN_TYPE, net.JoinHostPort(CONN_HOST, CONN_PORT))
    if err != nil {
        fmt.Println("Error Listening", err.Error())
        os.Exit(1)
    }
    defer server.Close()
    fmt.Println("Listening on ", net.JoinHostPort(CONN_HOST, CONN_PORT))
    for {
        conn, err := server.Accept()
        if err != nil {
            fmt.Println("Error acepting: ", err.Error())
            os.Exit(1)
        }

        ConnectedClients += 1
        fmt.Println("Connected with:", conn.RemoteAddr())
        fmt.Println("Clients:", ConnectedClients)

        if ConnectedClients > MAX_CLIENTS {
            fmt.Println("Limit reached! Disconnecting:", conn.RemoteAddr())
            conn.Close()
        }
        go handleRequest(conn)
    }
}

func handleRequest(conn net.Conn) {
    //This defer will never run.... :-(
    defer func() {
        fmt.Println("Connection closed with client:", conn.RemoteAddr())
        ConnectedClients -= 1
        conn.Close()
    }()
    ...
    ...
}

以上是你提供的代码的翻译。

英文:

How can I limit the number of clients connected to my service?
I tried with a simple counter, but if a client exits without close
their connection I don't have how get it.

Please could somebody give me ideas in order to get it?

const MAX_CLIENTS = 5
var ConnectedClients int
func main() {
	ConnectedClients = 0
	server, err := net.Listen(CONN_TYPE, net.JoinHostPort(CONN_HOST, CONN_PORT))
	if err != nil {
		fmt.Println("Error Listening", err.Error())
		os.Exit(1)
	}
	defer server.Close()
	fmt.Println("Listening on ", net.JoinHostPort(CONN_HOST, CONN_PORT))
	for {
		conn, err := server.Accept()
		if err != nil {
			fmt.Println("Error acepting: ", err.Error())
			os.Exit(1)
		}

		ConnectedClients += 1
		fmt.Println("Connected with:", conn.RemoteAddr())
		fmt.Println("Clients:", ConnectedClients)

		if ConnectedClients > MAX_CLIENTS {
			fmt.Println("Limit reached! Disconnecting:", conn.RemoteAddr())
			conn.Close()
		}
		go handleRequest(conn)
	}
}

func handleRequest(conn net.Conn) {
    //This defer will never run.... :-(
	defer func() {
		fmt.Println("Connection closed with client:", conn.RemoteAddr())
		ConnectedClients -= 1
        conn.Close()
	}()
    ...
    ...
}

答案1

得分: 0

实际上,可能是你没有与我们分享的代码中存在问题(那些...)

连接迟早会断开。假设在你的连接处理程序中需要进行一些读写操作,那么最好的方法是使用读写超时(截止时间)来引发错误,然后根据需要进行处理。

请查看文档中SetReadDeadline/SetWriteDeadline的使用方法,链接为http://golang.org/pkg/net/#IPConn.SetReadDeadline,或者可以参考这个答案中的用法:https://stackoverflow.com/a/12741495/93767

一旦你修复了处理程序部分,就可以无问题地使用客户端计数。

英文:

Actually it might be the issue in the code you have not shared with us (those ...)

Connection will exit - sooner or later. The assumption is that in your connection handler you have to do some read/write operations, so the best way forward would be use read/write timeout (deadlines) to raise errors which you going to handle as needed.

Please, check the use of SetReadDeadline/SetWriteDeadline in documentation http://golang.org/pkg/net/#IPConn.SetReadDeadline or as used in this answer - https://stackoverflow.com/a/12741495/93767

Once you fix the handler part you can use client counting without any issues.

huangapple
  • 本文由 发表于 2014年10月14日 01:29:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/26345592.html
匿名

发表评论

匿名网友

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

确定