优化gc性能

This commit is contained in:
bjdgyc 2021-05-29 17:31:41 +08:00
parent af6f87ee0f
commit 0512ce197a
6 changed files with 106 additions and 36 deletions

View File

@ -2,5 +2,5 @@ package base
const (
APP_NAME = "AnyLink"
APP_VER = "0.2.1"
APP_VER = "0.3.1"
)

View File

@ -33,7 +33,8 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
base.Error("SetDeadline: ", err)
return
}
hdata := make([]byte, BufferSize)
// hdata := make([]byte, BufferSize)
hdata := getByteFull()
n, err = conn.Read(hdata)
if err != nil {
base.Error("read hdata: ", err)
@ -65,8 +66,9 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
if payloadIn(cSess, sessdata.LTypeIPData, 0x00, hdata[8:8+dataLen]) {
return
}
}
putByte(hdata)
}
}
@ -78,9 +80,9 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
}()
var (
err error
n int
header []byte
err error
n int
// header []byte
payload *sessdata.Payload
)
@ -95,7 +97,9 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
continue
}
header = []byte{'S', 'T', 'F', 0x01, 0x00, 0x00, payload.PType, 0x00}
h := []byte{'S', 'T', 'F', 0x01, 0x00, 0x00, payload.PType, 0x00}
header := getByteZero()
header = append(header, h...)
if payload.PType == 0x00 { // data
binary.BigEndian.PutUint16(header[4:6], uint16(len(payload.Data)))
header = append(header, payload.Data...)
@ -106,6 +110,9 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
return
}
putByte(header)
putPayload(payload)
// 限流设置
err = cSess.RateLimit(n, false)
if err != nil {

View File

@ -9,7 +9,7 @@ import (
)
func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
base.Debug("LinkDtls connect", cSess.IpAddr, conn.RemoteAddr())
base.Debug("LinkDtls connect ip:", cSess.IpAddr, "udp-rip:", conn.RemoteAddr())
dSess := cSess.NewDtlsConn()
if dSess == nil {
// 创建失败,直接关闭链接
@ -35,7 +35,9 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
base.Error("SetDeadline: ", err)
return
}
hdata := make([]byte, BufferSize)
// hdata := make([]byte, BufferSize)
hdata := getByteFull()
n, err := conn.Read(hdata)
if err != nil {
base.Error("read hdata: ", err)
@ -51,9 +53,9 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
switch hdata[0] {
case 0x07: // KEEPALIVE
// do nothing
base.Debug("recv keepalive", cSess.IpAddr)
// base.Debug("recv keepalive", cSess.IpAddr)
case 0x05: // DISCONNECT
base.Debug("DISCONNECT", cSess.IpAddr)
base.Debug("DISCONNECT DTLS", cSess.IpAddr)
return
case 0x03: // DPD-REQ
// base.Debug("recv DPD-REQ", cSess.IpAddr)
@ -67,6 +69,8 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
return
}
}
putByte(hdata)
}
}
@ -78,7 +82,7 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
}()
var (
header []byte
// header []byte
payload *sessdata.Payload
)
@ -94,14 +98,21 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
continue
}
header = []byte{payload.PType}
header = append(header, payload.Data...)
// header = []byte{payload.PType}
header := getByteZero()
header = append(header, payload.PType)
if payload.PType == 0x00 { // data
header = append(header, payload.Data...)
}
n, err := conn.Write(header)
if err != nil {
base.Error("write err", err)
return
}
putByte(header)
putPayload(payload)
// 限流设置
err = cSess.RateLimit(n, false)
if err != nil {

View File

@ -84,6 +84,8 @@ func tunWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
base.Error("tun Write err", err)
return
}
putPayload(payload)
}
}
@ -98,15 +100,15 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
)
for {
data := make([]byte, BufferSize)
// data := make([]byte, BufferSize)
data := getByteFull()
n, err = ifce.Read(data)
if err != nil {
base.Error("tun Read err", n, err)
return
}
data = data[:n]
// data = data[:n]
// ip_src := waterutil.IPv4Source(data)
// ip_dst := waterutil.IPv4Destination(data)
// ip_port := waterutil.IPv4DestinationPort(data)
@ -114,9 +116,10 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
// packet := gopacket.NewPacket(data, layers.LayerTypeIPv4, gopacket.Default)
// fmt.Println("read:", packet)
if payloadOut(cSess, sessdata.LTypeIPData, 0x00, data) {
if payloadOut(cSess, sessdata.LTypeIPData, 0x00, data[:n]) {
return
}
putByte(data)
}
}

View File

@ -7,13 +7,12 @@ import (
)
func payloadIn(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
payload := &sessdata.Payload{
LType: lType,
PType: pType,
Data: data,
}
pl := getPayload()
pl.LType = lType
pl.PType = pType
pl.Data = append(pl.Data, data...)
return payloadInData(cSess, payload)
return payloadInData(cSess, pl)
}
func payloadInData(cSess *sessdata.ConnSession, payload *sessdata.Payload) bool {
@ -44,16 +43,15 @@ func payloadOut(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, d
}
func payloadOutCstp(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
payload := &sessdata.Payload{
LType: lType,
PType: pType,
Data: data,
}
pl := getPayload()
pl.LType = lType
pl.PType = pType
pl.Data = append(pl.Data, data...)
closed := false
select {
case cSess.PayloadOutCstp <- payload:
case cSess.PayloadOutCstp <- pl:
case <-cSess.CloseChan:
closed = true
}
@ -62,14 +60,13 @@ func payloadOutCstp(cSess *sessdata.ConnSession, lType sessdata.LType, pType byt
}
func payloadOutDtls(cSess *sessdata.ConnSession, dSess *sessdata.DtlsSession, lType sessdata.LType, pType byte, data []byte) bool {
payload := &sessdata.Payload{
LType: lType,
PType: pType,
Data: data,
}
pl := getPayload()
pl.LType = lType
pl.PType = pType
pl.Data = append(pl.Data, data...)
select {
case cSess.PayloadOutDtls <- payload:
case cSess.PayloadOutDtls <- pl:
case <-dSess.CloseChan:
}

52
server/handler/pool.go Normal file
View File

@ -0,0 +1,52 @@
package handler
import (
"sync"
"github.com/bjdgyc/anylink/sessdata"
)
var plPool = sync.Pool{
New: func() interface{} {
pl := sessdata.Payload{
Data: make([]byte, 0, BufferSize),
}
// fmt.Println("plPool-init", len(pl.Data), cap(pl.Data))
return &pl
},
}
func getPayload() *sessdata.Payload {
pl := plPool.Get().(*sessdata.Payload)
return pl
}
func putPayload(pl *sessdata.Payload) {
pl.LType = 0
pl.PType = 0
pl.Data = pl.Data[:0]
plPool.Put(pl)
}
var bytePool = sync.Pool{
New: func() interface{} {
b := make([]byte, 0, BufferSize)
// fmt.Println("bytePool-init")
return b
},
}
func getByteZero() []byte {
b := bytePool.Get().([]byte)
return b
}
func getByteFull() []byte {
b := bytePool.Get().([]byte)
b = b[:BufferSize]
return b
}
func putByte(b []byte) {
b = b[:0]
bytePool.Put(b)
}