英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论