mirror of
https://github.com/SunBK201/UA3F.git
synced 2025-12-19 18:26:12 +00:00
feat: introduce buf pool and reduce reader bufffer size
This commit is contained in:
parent
992f4ceaba
commit
64afa4a68d
@ -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 {
|
||||||
|
|||||||
@ -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,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user