Golang TCP 服务器 - GC 问题
我有一个 TCP 服务器,它接收响应的十六进制编码消息。这在流量较低的情况下效果很好。它每秒可以处理 100、200 条消息。我在较高流量时遇到 GC(垃圾收集)问题,并且脚本停止响应并在一段时间后恢复。感谢您对此提出的宝贵意见。
var running bool = true
func main() {
flag.IntVar(&lport, "lport", 5555, "Listen Port")
flag.Parse()
sockAddr := fmt.Sprintf(":%d", lport)
TcpService(sockAddr)
}
func TcpService(addr string) {
sockAddr, err := net.ResolveTCPAddr("tcp", addr)
checkError(err)
listener, err := net.ListenTCP("tcp", sockAddr)
checkError(err)
for running {
conn, err := listener.Accept()
if err != nil {
continue
}
go serveConnection(conn, addr)
}
}
func serveConnection(conn net.Conn, addr string) {
defer conn.Close()
var buf []byte
data := make([]byte, 1024*1024)
for running {
n, err := conn.Read(data[0:])
if err == io.EOF {
//conn.Close()
//return
}
if n > 0 {
buf = append(buf, data[0:n]...)
for {
if len(buf) > 4 {
// Every message is of fixed length and length is encoded in buf[1:4]
msgLen := decodeLength(buf[1:4])
if len(buf) >= msgLen {
go handleMsg(conn, buf[:msgLen])
buf = buf[msgLen:]
} else {
break
}
} else {
break
}
}
}
}
}
func handleMsg(conn net.Conn, buf []byte) {
/* validate the recieved message ie buf
....
*/
var resp []byte
ressp = append(resp, value1...)
ressp = append(resp, value2...)
ressp = append(resp, value3...)
ressp = append(resp, value4...)
//resp is a hex coded message ( hex bytes) of length varying from 1k to 5k bytes
conn.Write(resp)
}
I have a TCP server which receives responds hex encoded messages. This works fine with lower traffic. It can handle 100, 200 messages per second. I face GC (garbage collection) issue at higher traffic and scripts stops responding and recovers after some time. Appreciate your valuable inputs on this.
var running bool = true
func main() {
flag.IntVar(&lport, "lport", 5555, "Listen Port")
flag.Parse()
sockAddr := fmt.Sprintf(":%d", lport)
TcpService(sockAddr)
}
func TcpService(addr string) {
sockAddr, err := net.ResolveTCPAddr("tcp", addr)
checkError(err)
listener, err := net.ListenTCP("tcp", sockAddr)
checkError(err)
for running {
conn, err := listener.Accept()
if err != nil {
continue
}
go serveConnection(conn, addr)
}
}
func serveConnection(conn net.Conn, addr string) {
defer conn.Close()
var buf []byte
data := make([]byte, 1024*1024)
for running {
n, err := conn.Read(data[0:])
if err == io.EOF {
//conn.Close()
//return
}
if n > 0 {
buf = append(buf, data[0:n]...)
for {
if len(buf) > 4 {
// Every message is of fixed length and length is encoded in buf[1:4]
msgLen := decodeLength(buf[1:4])
if len(buf) >= msgLen {
go handleMsg(conn, buf[:msgLen])
buf = buf[msgLen:]
} else {
break
}
} else {
break
}
}
}
}
}
func handleMsg(conn net.Conn, buf []byte) {
/* validate the recieved message ie buf
....
*/
var resp []byte
ressp = append(resp, value1...)
ressp = append(resp, value2...)
ressp = append(resp, value3...)
ressp = append(resp, value4...)
//resp is a hex coded message ( hex bytes) of length varying from 1k to 5k bytes
conn.Write(resp)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论