feat: introduce buf pool and reduce reader bufffer size

This commit is contained in:
SunBK201 2025-12-09 11:52:48 +08:00
parent 992f4ceaba
commit 64afa4a68d
5 changed files with 41 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import (
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/hashicorp/golang-lru/v2/expirable"
@ -24,6 +25,7 @@ type Server struct {
Recorder *statistics.Recorder
Cache *expirable.LRU[string, struct{}]
SkipIpChan chan *net.IP
BufioReaderPool sync.Pool
}
func (s *Server) ServeConnLink(connLink *ConnLink) {
@ -48,7 +50,12 @@ func (s *Server) ServeConnLink(connLink *ConnLink) {
}
func (s *Server) ProcessLR(c *ConnLink) (err error) {
reader := bufio.NewReaderSize(c.LConn, 64*1024)
reader := s.BufioReaderPool.Get().(*bufio.Reader)
reader.Reset(c.LConn)
defer func() {
reader.Reset(nil)
s.BufioReaderPool.Put(reader)
}()
defer func() {
if err != nil {

View File

@ -1,10 +1,12 @@
package http
import (
"bufio"
"fmt"
"io"
"log/slog"
"net/http"
"sync"
"time"
"github.com/hashicorp/golang-lru/v2/expirable"
@ -29,6 +31,11 @@ func New(cfg *config.Config, rw *rewrite.Rewriter, rc *statistics.Recorder) *Ser
Rewriter: rw,
Recorder: rc,
Cache: expirable.NewLRU[string, struct{}](512, nil, 30*time.Minute),
BufioReaderPool: sync.Pool{
New: func() interface{} {
return bufio.NewReaderSize(nil, 16*1024)
},
},
},
so_mark: base.SO_MARK,
}

View File

@ -3,10 +3,12 @@
package redirect
import (
"bufio"
"errors"
"fmt"
"log/slog"
"net"
"sync"
"syscall"
"time"
@ -34,6 +36,11 @@ func New(cfg *config.Config, rw *rewrite.Rewriter, rc *statistics.Recorder) *Ser
Recorder: rc,
Cache: expirable.NewLRU[string, struct{}](512, nil, 30*time.Minute),
SkipIpChan: make(chan *net.IP, 512),
BufioReaderPool: sync.Pool{
New: func() interface{} {
return bufio.NewReaderSize(nil, 16*1024)
},
},
},
so_mark: base.SO_MARK,
}

View File

@ -1,11 +1,13 @@
package socks5
import (
"bufio"
"bytes"
"errors"
"fmt"
"log/slog"
"net"
"sync"
"syscall"
"time"
@ -30,6 +32,11 @@ func New(cfg *config.Config, rw *rewrite.Rewriter, rc *statistics.Recorder) *Ser
Rewriter: rw,
Recorder: rc,
Cache: expirable.NewLRU[string, struct{}](512, nil, 30*time.Minute),
BufioReaderPool: sync.Pool{
New: func() interface{} {
return bufio.NewReaderSize(nil, 16*1024)
},
},
},
so_mark: base.SO_MARK,
}

View File

@ -3,11 +3,13 @@
package tproxy
import (
"bufio"
"context"
"errors"
"fmt"
"log/slog"
"net"
"sync"
"syscall"
"time"
@ -40,6 +42,11 @@ func New(cfg *config.Config, rw *rewrite.Rewriter, rc *statistics.Recorder) *Ser
Recorder: rc,
Cache: expirable.NewLRU[string, struct{}](512, nil, 30*time.Minute),
SkipIpChan: make(chan *net.IP, 512),
BufioReaderPool: sync.Pool{
New: func() interface{} {
return bufio.NewReaderSize(nil, 16*1024)
},
},
},
so_mark: base.SO_MARK,
tproxyFwMark: "0x1c9",