mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-28 01:44:47 +08:00
tcp消息内存优化
This commit is contained in:
@@ -5,7 +5,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type INetMempool interface {
|
type INetMempool interface {
|
||||||
MakeByteSlice(size int) []byte
|
MakeReadByteSlice(size int) []byte
|
||||||
|
MakeWriteByteSlice(size int) []byte
|
||||||
ReleaseByteSlice(byteBuff []byte) bool
|
ReleaseByteSlice(byteBuff []byte) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +81,14 @@ func (areaPool *memAreaPool) MakeByteSlice(size int) []byte{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (areaPool *memAreaPool) MakeReadByteSlice(size int) []byte{
|
||||||
|
return areaPool.MakeByteSlice(size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (areaPool *memAreaPool) MakeWriteByteSlice(size int) []byte{
|
||||||
|
return areaPool.MakeByteSlice(size)
|
||||||
|
}
|
||||||
|
|
||||||
func (areaPool *memAreaPool) ReleaseByteSlice(byteBuff []byte) bool {
|
func (areaPool *memAreaPool) ReleaseByteSlice(byteBuff []byte) bool {
|
||||||
for i:=0;i<len(memAreaPoolList);i++{
|
for i:=0;i<len(memAreaPoolList);i++{
|
||||||
if cap(byteBuff) <= memAreaPoolList[i].maxAreaValue {
|
if cap(byteBuff) <= memAreaPoolList[i].maxAreaValue {
|
||||||
|
|||||||
@@ -101,13 +101,13 @@ func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
|||||||
|
|
||||||
// data
|
// data
|
||||||
//msgData := make([]byte, msgLen)
|
//msgData := make([]byte, msgLen)
|
||||||
msgData := p.MakeByteSlice(int(msgLen))
|
msgData := p.MakeReadByteSlice(int(msgLen))
|
||||||
if _, err := io.ReadFull(conn, msgData); err != nil {
|
if _, err := io.ReadFull(conn, msgData[:msgLen]); err != nil {
|
||||||
p.ReleaseByteSlice(msgData)
|
p.ReleaseByteSlice(msgData)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgData, nil
|
return msgData[:msgLen], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// goroutine safe
|
// goroutine safe
|
||||||
@@ -126,7 +126,7 @@ func (p *MsgParser) Write(conn *TCPConn, args ...[]byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//msg := make([]byte, uint32(p.lenMsgLen)+msgLen)
|
//msg := make([]byte, uint32(p.lenMsgLen)+msgLen)
|
||||||
msg := p.MakeByteSlice(p.lenMsgLen+int(msgLen))
|
msg := p.MakeWriteByteSlice(p.lenMsgLen+int(msgLen))
|
||||||
// write len
|
// write len
|
||||||
switch p.lenMsgLen {
|
switch p.lenMsgLen {
|
||||||
case 1:
|
case 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user