mirror of https://github.com/bjdgyc/anylink.git
优化gc性能
This commit is contained in:
parent
af6f87ee0f
commit
0512ce197a
|
@ -2,5 +2,5 @@ package base
|
||||||
|
|
||||||
const (
|
const (
|
||||||
APP_NAME = "AnyLink"
|
APP_NAME = "AnyLink"
|
||||||
APP_VER = "0.2.1"
|
APP_VER = "0.3.1"
|
||||||
)
|
)
|
||||||
|
|
|
@ -33,7 +33,8 @@ func LinkCstp(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
base.Error("SetDeadline: ", err)
|
base.Error("SetDeadline: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hdata := make([]byte, BufferSize)
|
// hdata := make([]byte, BufferSize)
|
||||||
|
hdata := getByteFull()
|
||||||
n, err = conn.Read(hdata)
|
n, err = conn.Read(hdata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
base.Error("read hdata: ", err)
|
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]) {
|
if payloadIn(cSess, sessdata.LTypeIPData, 0x00, hdata[8:8+dataLen]) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putByte(hdata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,9 +80,9 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
n int
|
n int
|
||||||
header []byte
|
// header []byte
|
||||||
payload *sessdata.Payload
|
payload *sessdata.Payload
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -95,7 +97,9 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
continue
|
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
|
if payload.PType == 0x00 { // data
|
||||||
binary.BigEndian.PutUint16(header[4:6], uint16(len(payload.Data)))
|
binary.BigEndian.PutUint16(header[4:6], uint16(len(payload.Data)))
|
||||||
header = append(header, payload.Data...)
|
header = append(header, payload.Data...)
|
||||||
|
@ -106,6 +110,9 @@ func cstpWrite(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putByte(header)
|
||||||
|
putPayload(payload)
|
||||||
|
|
||||||
// 限流设置
|
// 限流设置
|
||||||
err = cSess.RateLimit(n, false)
|
err = cSess.RateLimit(n, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
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()
|
dSess := cSess.NewDtlsConn()
|
||||||
if dSess == nil {
|
if dSess == nil {
|
||||||
// 创建失败,直接关闭链接
|
// 创建失败,直接关闭链接
|
||||||
|
@ -35,7 +35,9 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
base.Error("SetDeadline: ", err)
|
base.Error("SetDeadline: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hdata := make([]byte, BufferSize)
|
|
||||||
|
// hdata := make([]byte, BufferSize)
|
||||||
|
hdata := getByteFull()
|
||||||
n, err := conn.Read(hdata)
|
n, err := conn.Read(hdata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
base.Error("read hdata: ", err)
|
base.Error("read hdata: ", err)
|
||||||
|
@ -51,9 +53,9 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
switch hdata[0] {
|
switch hdata[0] {
|
||||||
case 0x07: // KEEPALIVE
|
case 0x07: // KEEPALIVE
|
||||||
// do nothing
|
// do nothing
|
||||||
base.Debug("recv keepalive", cSess.IpAddr)
|
// base.Debug("recv keepalive", cSess.IpAddr)
|
||||||
case 0x05: // DISCONNECT
|
case 0x05: // DISCONNECT
|
||||||
base.Debug("DISCONNECT", cSess.IpAddr)
|
base.Debug("DISCONNECT DTLS", cSess.IpAddr)
|
||||||
return
|
return
|
||||||
case 0x03: // DPD-REQ
|
case 0x03: // DPD-REQ
|
||||||
// base.Debug("recv DPD-REQ", cSess.IpAddr)
|
// base.Debug("recv DPD-REQ", cSess.IpAddr)
|
||||||
|
@ -67,6 +69,8 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putByte(hdata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +82,7 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
header []byte
|
// header []byte
|
||||||
payload *sessdata.Payload
|
payload *sessdata.Payload
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -94,14 +98,21 @@ func dtlsWrite(conn net.Conn, dSess *sessdata.DtlsSession, cSess *sessdata.ConnS
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
header = []byte{payload.PType}
|
// header = []byte{payload.PType}
|
||||||
header = append(header, payload.Data...)
|
header := getByteZero()
|
||||||
|
header = append(header, payload.PType)
|
||||||
|
if payload.PType == 0x00 { // data
|
||||||
|
header = append(header, payload.Data...)
|
||||||
|
}
|
||||||
n, err := conn.Write(header)
|
n, err := conn.Write(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
base.Error("write err", err)
|
base.Error("write err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putByte(header)
|
||||||
|
putPayload(payload)
|
||||||
|
|
||||||
// 限流设置
|
// 限流设置
|
||||||
err = cSess.RateLimit(n, false)
|
err = cSess.RateLimit(n, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -84,6 +84,8 @@ func tunWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||||
base.Error("tun Write err", err)
|
base.Error("tun Write err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putPayload(payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,15 +100,15 @@ func tunRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||||
)
|
)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
data := make([]byte, BufferSize)
|
// data := make([]byte, BufferSize)
|
||||||
|
data := getByteFull()
|
||||||
n, err = ifce.Read(data)
|
n, err = ifce.Read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
base.Error("tun Read err", n, err)
|
base.Error("tun Read err", n, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data = data[:n]
|
// data = data[:n]
|
||||||
|
|
||||||
// ip_src := waterutil.IPv4Source(data)
|
// ip_src := waterutil.IPv4Source(data)
|
||||||
// ip_dst := waterutil.IPv4Destination(data)
|
// ip_dst := waterutil.IPv4Destination(data)
|
||||||
// ip_port := waterutil.IPv4DestinationPort(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)
|
// packet := gopacket.NewPacket(data, layers.LayerTypeIPv4, gopacket.Default)
|
||||||
// fmt.Println("read:", packet)
|
// fmt.Println("read:", packet)
|
||||||
|
|
||||||
if payloadOut(cSess, sessdata.LTypeIPData, 0x00, data) {
|
if payloadOut(cSess, sessdata.LTypeIPData, 0x00, data[:n]) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
putByte(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func payloadIn(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
|
func payloadIn(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
|
||||||
payload := &sessdata.Payload{
|
pl := getPayload()
|
||||||
LType: lType,
|
pl.LType = lType
|
||||||
PType: pType,
|
pl.PType = pType
|
||||||
Data: data,
|
pl.Data = append(pl.Data, data...)
|
||||||
}
|
|
||||||
|
|
||||||
return payloadInData(cSess, payload)
|
return payloadInData(cSess, pl)
|
||||||
}
|
}
|
||||||
|
|
||||||
func payloadInData(cSess *sessdata.ConnSession, payload *sessdata.Payload) bool {
|
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 {
|
func payloadOutCstp(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, data []byte) bool {
|
||||||
payload := &sessdata.Payload{
|
pl := getPayload()
|
||||||
LType: lType,
|
pl.LType = lType
|
||||||
PType: pType,
|
pl.PType = pType
|
||||||
Data: data,
|
pl.Data = append(pl.Data, data...)
|
||||||
}
|
|
||||||
|
|
||||||
closed := false
|
closed := false
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case cSess.PayloadOutCstp <- payload:
|
case cSess.PayloadOutCstp <- pl:
|
||||||
case <-cSess.CloseChan:
|
case <-cSess.CloseChan:
|
||||||
closed = true
|
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 {
|
func payloadOutDtls(cSess *sessdata.ConnSession, dSess *sessdata.DtlsSession, lType sessdata.LType, pType byte, data []byte) bool {
|
||||||
payload := &sessdata.Payload{
|
pl := getPayload()
|
||||||
LType: lType,
|
pl.LType = lType
|
||||||
PType: pType,
|
pl.PType = pType
|
||||||
Data: data,
|
pl.Data = append(pl.Data, data...)
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case cSess.PayloadOutDtls <- payload:
|
case cSess.PayloadOutDtls <- pl:
|
||||||
case <-dSess.CloseChan:
|
case <-dSess.CloseChan:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue