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"
"net/http" "net/http"
"strings" "strings"
"sync"
"time" "time"
"github.com/hashicorp/golang-lru/v2/expirable" "github.com/hashicorp/golang-lru/v2/expirable"
@ -19,11 +20,12 @@ import (
) )
type Server struct { type Server struct {
Cfg *config.Config Cfg *config.Config
Rewriter *rewrite.Rewriter Rewriter *rewrite.Rewriter
Recorder *statistics.Recorder Recorder *statistics.Recorder
Cache *expirable.LRU[string, struct{}] Cache *expirable.LRU[string, struct{}]
SkipIpChan chan *net.IP SkipIpChan chan *net.IP
BufioReaderPool sync.Pool
} }
func (s *Server) ServeConnLink(connLink *ConnLink) { func (s *Server) ServeConnLink(connLink *ConnLink) {
@ -48,7 +50,12 @@ func (s *Server) ServeConnLink(connLink *ConnLink) {
} }
func (s *Server) ProcessLR(c *ConnLink) (err error) { 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() { defer func() {
if err != nil { if err != nil {

View File

@ -1,10 +1,12 @@
package http package http
import ( import (
"bufio"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/hashicorp/golang-lru/v2/expirable" "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, Rewriter: rw,
Recorder: rc, Recorder: rc,
Cache: expirable.NewLRU[string, struct{}](512, nil, 30*time.Minute), 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, so_mark: base.SO_MARK,
} }

View File

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

View File

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

View File

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