优化网络底层写超时

This commit is contained in:
orgin
2022-10-13 10:39:29 +08:00
parent 7f93aa5ff9
commit 64fb9368bf
5 changed files with 55 additions and 32 deletions

View File

@@ -21,9 +21,6 @@ type TcpService struct {
mapClientLocker sync.RWMutex
mapClient map[uint64] *Client
process processor.IProcessor
ReadDeadline time.Duration
WriteDeadline time.Duration
}
type TcpPackType int8
@@ -34,14 +31,6 @@ const(
TPT_UnknownPack TcpPackType = 3
)
const Default_MaxConnNum = 3000
const Default_PendingWriteNum = 10000
const Default_LittleEndian = false
const Default_MinMsgLen = 2
const Default_MaxMsgLen = 65535
const Default_ReadDeadline = 180 //30s
const Default_WriteDeadline = 180 //30s
const (
MaxNodeId = 1<<14 - 1 //最大值 16383
MaxSeed = 1<<19 - 1 //最大值 524287
@@ -89,14 +78,6 @@ func (tcpService *TcpService) OnInit() error{
}
tcpService.tcpServer.Addr = addr.(string)
tcpService.tcpServer.MaxConnNum = Default_MaxConnNum
tcpService.tcpServer.PendingWriteNum = Default_PendingWriteNum
tcpService.tcpServer.LittleEndian = Default_LittleEndian
tcpService.tcpServer.MinMsgLen = Default_MinMsgLen
tcpService.tcpServer.MaxMsgLen = Default_MaxMsgLen
tcpService.ReadDeadline = Default_ReadDeadline
tcpService.WriteDeadline = Default_WriteDeadline
MaxConnNum,ok := tcpCfg["MaxConnNum"]
if ok == true {
tcpService.tcpServer.MaxConnNum = int(MaxConnNum.(float64))
@@ -120,12 +101,12 @@ func (tcpService *TcpService) OnInit() error{
readDeadline,ok := tcpCfg["ReadDeadline"]
if ok == true {
tcpService.ReadDeadline = time.Second*time.Duration(readDeadline.(float64))
tcpService.tcpServer.ReadDeadline = time.Second*time.Duration(readDeadline.(float64))
}
writeDeadline,ok := tcpCfg["WriteDeadline"]
if ok == true {
tcpService.WriteDeadline = time.Second*time.Duration(writeDeadline.(float64))
tcpService.tcpServer.WriteDeadline = time.Second*time.Duration(writeDeadline.(float64))
}
tcpService.mapClient = make( map[uint64] *Client, tcpService.tcpServer.MaxConnNum)
@@ -195,7 +176,7 @@ func (slf *Client) Run() {
break
}
slf.tcpConn.SetReadDeadline(slf.tcpService.ReadDeadline)
slf.tcpConn.SetReadDeadline(slf.tcpService.tcpServer.ReadDeadline)
bytes,err := slf.tcpConn.ReadMsg()
if err != nil {
log.SDebug("read client id ",slf.id," is error:",err.Error())
@@ -231,7 +212,6 @@ func (tcpService *TcpService) SendMsg(clientId uint64,msg interface{}) error{
if err != nil {
return err
}
client.tcpConn.SetWriteDeadline(tcpService.WriteDeadline)
return client.tcpConn.WriteMsg(bytes)
}
@@ -271,7 +251,6 @@ func (tcpService *TcpService) SendRawMsg(clientId uint64,msg []byte) error{
return fmt.Errorf("client %d is disconnect!",clientId)
}
tcpService.mapClientLocker.Unlock()
client.tcpConn.SetWriteDeadline(tcpService.WriteDeadline)
return client.tcpConn.WriteMsg(msg)
}
@@ -283,7 +262,6 @@ func (tcpService *TcpService) SendRawData(clientId uint64,data []byte) error{
return fmt.Errorf("client %d is disconnect!",clientId)
}
tcpService.mapClientLocker.Unlock()
client.tcpConn.SetWriteDeadline(tcpService.WriteDeadline)
return client.tcpConn.WriteRawMsg(data)
}