优化代码

This commit is contained in:
GoEdgeLab
2022-12-13 18:08:50 +08:00
parent 595a118cf2
commit c3cdfc2e44
2 changed files with 15 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ package conns
import ( import (
"net" "net"
"sort"
"sync" "sync"
"time" "time"
) )
@@ -126,5 +127,12 @@ func (this *Map) AllConns() []*ConnInfo {
result = append(result, connInfo) result = append(result, connInfo)
} }
} }
// 按时间排序
sort.Slice(result, func(i, j int) bool {
// 创建时间越大Age越小
return result[i].CreatedAt > result[j].CreatedAt
})
return result return result
} }

View File

@@ -69,6 +69,7 @@ func (this *ClientConn) Read(b []byte) (n int, err error) {
} }
// TLS // TLS
// TODO L1 -> L2 时不计算synflood
if this.isTLS { if this.isTLS {
if !this.hasDeadline { if !this.hasDeadline {
_ = this.rawConn.SetReadDeadline(time.Now().Add(time.Duration(nodeconfigs.DefaultTLSHandshakeTimeout) * time.Second)) // TODO 握手超时时间可以设置 _ = this.rawConn.SetReadDeadline(time.Now().Add(time.Duration(nodeconfigs.DefaultTLSHandshakeTimeout) * time.Second)) // TODO 握手超时时间可以设置
@@ -115,9 +116,10 @@ func (this *ClientConn) Read(b []byte) (n int, err error) {
func (this *ClientConn) Write(b []byte) (n int, err error) { func (this *ClientConn) Write(b []byte) (n int, err error) {
// 设置超时时间 // 设置超时时间
var timeoutSeconds = len(b) / 512 // TODO L2 -> L1 写入时不限制时间
if timeoutSeconds < 5 { var timeoutSeconds = len(b) / 4096
timeoutSeconds = 5 if timeoutSeconds < 3 {
timeoutSeconds = 3
} }
_ = this.rawConn.SetWriteDeadline(time.Now().Add(time.Duration(timeoutSeconds) * time.Second)) // TODO 时间可以设置 _ = this.rawConn.SetWriteDeadline(time.Now().Add(time.Duration(timeoutSeconds) * time.Second)) // TODO 时间可以设置
@@ -134,6 +136,8 @@ func (this *ClientConn) Write(b []byte) (n int, err error) {
// 如果是写入超时,则立即关闭连接 // 如果是写入超时,则立即关闭连接
if err != nil && os.IsTimeout(err) { if err != nil && os.IsTimeout(err) {
//logs.Println(this.RemoteAddr(), timeoutSeconds, "seconds", n, "bytes")
// TODO 考虑对多次慢连接的IP做出惩罚 // TODO 考虑对多次慢连接的IP做出惩罚
conn, ok := this.rawConn.(LingerConn) conn, ok := this.rawConn.(LingerConn)
if ok { if ok {