mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化网络层的GC
This commit is contained in:
@@ -11,4 +11,5 @@ type Conn interface {
|
||||
RemoteAddr() net.Addr
|
||||
Close()
|
||||
Destroy()
|
||||
ReleaseReadMsg(byteBuff []byte)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/log"
|
||||
"net"
|
||||
"sync"
|
||||
@@ -16,26 +17,37 @@ type TCPConn struct {
|
||||
msgParser *MsgParser
|
||||
}
|
||||
|
||||
|
||||
func freeChannel(conn *TCPConn){
|
||||
for;len(conn.writeChan)>0;{
|
||||
byteBuff := <- conn.writeChan
|
||||
if byteBuff != nil {
|
||||
conn.ReleaseReadMsg(byteBuff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newTCPConn(conn net.Conn, pendingWriteNum int, msgParser *MsgParser) *TCPConn {
|
||||
tcpConn := new(TCPConn)
|
||||
tcpConn.conn = conn
|
||||
tcpConn.writeChan = make(chan []byte, pendingWriteNum)
|
||||
tcpConn.msgParser = msgParser
|
||||
|
||||
go func() {
|
||||
for b := range tcpConn.writeChan {
|
||||
if b == nil {
|
||||
break
|
||||
}
|
||||
|
||||
_, err := conn.Write(b)
|
||||
releaseByteSlice(b)
|
||||
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
conn.Close()
|
||||
tcpConn.Lock()
|
||||
freeChannel(tcpConn)
|
||||
tcpConn.closeFlag = true
|
||||
tcpConn.Unlock()
|
||||
}()
|
||||
@@ -77,7 +89,8 @@ func (tcpConn *TCPConn) GetRemoteIp() string {
|
||||
|
||||
func (tcpConn *TCPConn) doWrite(b []byte) {
|
||||
if len(tcpConn.writeChan) == cap(tcpConn.writeChan) {
|
||||
log.Debug("close conn: channel full")
|
||||
tcpConn.ReleaseReadMsg(b)
|
||||
log.Error("close conn: channel full")
|
||||
tcpConn.doDestroy()
|
||||
return
|
||||
}
|
||||
@@ -90,6 +103,7 @@ func (tcpConn *TCPConn) Write(b []byte) {
|
||||
tcpConn.Lock()
|
||||
defer tcpConn.Unlock()
|
||||
if tcpConn.closeFlag || b == nil {
|
||||
tcpConn.ReleaseReadMsg(b)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -112,7 +126,14 @@ func (tcpConn *TCPConn) ReadMsg() ([]byte, error) {
|
||||
return tcpConn.msgParser.Read(tcpConn)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) ReleaseReadMsg(byteBuff []byte){
|
||||
releaseByteSlice(byteBuff)
|
||||
}
|
||||
|
||||
func (tcpConn *TCPConn) WriteMsg(args ...[]byte) error {
|
||||
if tcpConn.closeFlag == true {
|
||||
return fmt.Errorf("conn is close")
|
||||
}
|
||||
return tcpConn.msgParser.Write(tcpConn, args...)
|
||||
}
|
||||
|
||||
|
||||
@@ -99,8 +99,10 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||
}
|
||||
|
||||
// data
|
||||
msgData := make([]byte, msgLen)
|
||||
//msgData := make([]byte, msgLen)
|
||||
msgData := makeByteSlice(int(msgLen))
|
||||
if _, err := io.ReadFull(conn, msgData); err != nil {
|
||||
releaseByteSlice(msgData)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -123,8 +125,8 @@ func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error {
|
||||
}
|
||||
|
||||
//msgLen -= 2
|
||||
msg := make([]byte, uint32(p.lenMsgLen)+msgLen)
|
||||
|
||||
//msg := make([]byte, uint32(p.lenMsgLen)+msgLen)
|
||||
msg := makeByteSlice(p.lenMsgLen+int(msgLen))
|
||||
// write len
|
||||
switch p.lenMsgLen {
|
||||
case 1:
|
||||
|
||||
Reference in New Issue
Block a user