英文:
Errors when many clients connect to Go server
问题
full code could download at https://groups.google.com/forum/#!topic/golang-nuts/e1Ir__Dq_gE
Could anyone help me to improve this sample code to zero bug?
I think it will help us to develop a bug free client / server code.
my develop steps:
- Create a server which could handle multiple connections by goroutine.
- Build a client which works fine with simple protocol.
- Expand the client to simulate multiple clients (with option -n=1000 clients as default)
- TODO: try to reduce lock of server
- TODO: try to use bufio to enhance throughput
I found this code is very unstable contains with three problems:
-
launch 1000 clients, one of them occurs a EOF when reading from server.
-
launch 1050 clients, got too many open files soon (No any clients opened).
-
launch 1020 clients, got runtime error with long trace stacks.
Start pollServer: pipe: too many open files panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x28 pc=0x4650d0]
Here I paste my more simplified code.
const ClientCount = 1000
func main() {
srvAddr := "127.0.0.1:10000"
var wg sync.WaitGroup
wg.Add(ClientCount)
for i := 0; i < ClientCount; i++ {
go func(i int) {
client(i, srvAddr)
wg.Done()
}(i)
}
wg.Wait()
}
func client(i int, srvAddr string) {
conn, e := net.Dial("tcp", srvAddr)
if e != nil {
log.Fatalln("Err:Dial():", e)
}
defer conn.Close()
conn.SetTimeout(proto.LINK_TIMEOUT_NS)
defer func() {
conn.Close()
}()
l1 := proto.L1{uint32(i), uint16(rand.Uint32() % 10000)}
log.Println(conn.LocalAddr(), "WL1", l1)
e = binary.Write(conn, binary.BigEndian, &l1)
if e == os.EOF {
return
}
if e != nil {
return
}
// ...
}
英文:
full code could download at https://groups.google.com/forum/#!topic/golang-nuts/e1Ir__Dq_gE
Could anyone help me to improve this sample code to zero bug?
I think it will help us to develop a bug free client / server code.
my develop steps:
- Create a server which could handle multiple connections by goroutine.
- Build a client which works fine with simple protocol.
- Expand the client to simulate multiple clients (with option -n=1000 clients as default)
- TODO: try to reduce lock of server
- TODO: try to use bufio to enhance throughput
I found this code is very unstable contains with three problems:
-
launch 1000 clients, one of them occurs a EOF when reading from server.
-
launch 1050 clients, got too many open files soon (No any clients opened).
-
launch 1020 clients, got runtime error with long trace stacks.
Start pollServer: pipe: too many open files panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x28 pc=0x4650d0]
Here I paste my more simplified code.
const ClientCount = 1000
func main() {
srvAddr := "127.0.0.1:10000"
var wg sync.WaitGroup
wg.Add(ClientCount)
for i := 0; i < ClientCount; i++ {
go func(i int) {
client(i, srvAddr)
wg.Done()
}(i)
}
wg.Wait()
}
func client(i int, srvAddr string) {
conn, e := net.Dial("tcp", srvAddr)
if e != nil {
log.Fatalln("Err:Dial():", e)
}
defer conn.Close()
conn.SetTimeout(proto.LINK_TIMEOUT_NS)
defer func() {
conn.Close()
}()
l1 := proto.L1{uint32(i), uint16(rand.Uint32() % 10000)}
log.Println(conn.LocalAddr(), "WL1", l1)
e = binary.Write(conn, binary.BigEndian, &l1)
if e == os.EOF {
return
}
if e != nil {
return
}
// ...
}
答案1
得分: 1
这个在serverfault上的答案[1]建议对于可以处理大量连接的服务器,设置更高的ulimit是正确的做法。同时,使用lsof检查内存泄漏或文件描述符泄漏的应用程序。
ulimit -n 99999
[1] https://serverfault.com/a/48820/110909
英文:
This answer on serverfault [1] suggests that for servers that can handle a lot of connections, setting a higher ulimit is the thing to do. Also check for application leaks of memory or file descriptor leaks using lsof.
ulimit -n 99999
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论