1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-05 07:50:24 +08:00

optimize(gold): apply more buffer pools

This commit is contained in:
源文雨
2024-07-15 01:22:12 +09:00
parent 32af3ce142
commit 17e1f6cac9
8 changed files with 152 additions and 71 deletions

View File

@@ -70,11 +70,11 @@ func (s *Tunnel) Read(p []byte) (int, error) {
return 0, io.EOF
}
defer pkt.Put()
if len(pkt.Data) < 4 {
logrus.Warnln("[tunnel] unexpected packet data len", len(pkt.Data), "content", pkt.Data)
if pkt.BodyLen() < 4 {
logrus.Warnln("[tunnel] unexpected packet data len", pkt.BodyLen(), "content", hex.EncodeToString(pkt.Body()))
return 0, io.EOF
}
d = pkt.Data[4:]
d = pkt.Body()[4:]
}
if d != nil {
if len(p) >= len(d) {
@@ -111,7 +111,7 @@ func (s *Tunnel) handleWrite() {
}
logrus.Debugln("[tunnel] writing", len(b), "bytes...")
for len(b) > int(s.mtu)-4 {
logrus.Infoln("[tunnel] seq", seq, "split buffer")
logrus.Debugln("[tunnel] seq", seq, "split buffer")
binary.LittleEndian.PutUint32(buf[:4], seq)
seq++
copy(buf[4:], b[:s.mtu-4])
@@ -157,12 +157,12 @@ func (s *Tunnel) handleRead() {
}
end := 64
endl := "..."
if len(p.Data) < 64 {
end = len(p.Data)
if p.BodyLen() < 64 {
end = p.BodyLen()
endl = "."
}
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(p.Data[:end]), endl)
recvseq := binary.LittleEndian.Uint32(p.Data[:4])
logrus.Debugln("[tunnel] read recv", hex.EncodeToString(p.Body()[:end]), endl)
recvseq := binary.LittleEndian.Uint32(p.Body()[:4])
if recvseq == seq {
logrus.Debugln("[tunnel] dispatch seq", seq)
seq++

View File

@@ -169,8 +169,7 @@ type logFormat struct {
// Format implements logrus.Formatter
func (f logFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf := helper.SelectWriter()
defer helper.PutWriter(buf)
buf := helper.SelectWriter() // this writer will not be put back
buf.WriteByte('[')
if f.enableColor {
@@ -184,9 +183,7 @@ func (f logFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf.WriteString(entry.Message)
buf.WriteString("\n")
ret := make([]byte, len(buf.Bytes()))
copy(ret, buf.Bytes()) // copy buffer
return ret, nil
return buf.Bytes(), nil
}
const (