mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 22:34:02 +03:00
Reuse buffers on socket pumping
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
const copyBufferSize = 30 * 1024
|
||||||
|
|
||||||
|
var copyPool sync.Pool
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
copyPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
data := make([]byte, copyBufferSize)
|
||||||
|
return &data
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
-8
@@ -83,14 +83,10 @@ func (s *Server) accept(conn net.Conn) {
|
|||||||
|
|
||||||
wait := &sync.WaitGroup{}
|
wait := &sync.WaitGroup{}
|
||||||
wait.Add(2)
|
wait.Add(2)
|
||||||
go func() {
|
|
||||||
defer wait.Done()
|
go s.pipe(clientConn, tgConn, wait)
|
||||||
io.Copy(clientConn, tgConn) // nolint: errcheck
|
go s.pipe(tgConn, clientConn, wait)
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
defer wait.Done()
|
|
||||||
io.Copy(tgConn, clientConn) // nolint: errcheck
|
|
||||||
}()
|
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
wait.Wait()
|
wait.Wait()
|
||||||
|
|
||||||
@@ -131,6 +127,15 @@ func (s *Server) getTelegramStream(ctx context.Context, cancel context.CancelFun
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) pipe(dst io.Writer, src io.Reader, wait *sync.WaitGroup) {
|
||||||
|
defer wait.Done()
|
||||||
|
|
||||||
|
buf := copyPool.Get().(*[]byte)
|
||||||
|
defer copyPool.Put(buf)
|
||||||
|
|
||||||
|
io.CopyBuffer(dst, src, *buf) // nolint: errcheck
|
||||||
|
}
|
||||||
|
|
||||||
// NewServer creates new instance of MTPROTO proxy.
|
// NewServer creates new instance of MTPROTO proxy.
|
||||||
func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
|
func NewServer(conf *config.Config, logger *zap.SugaredLogger, stat *Stats) *Server {
|
||||||
return &Server{
|
return &Server{
|
||||||
|
|||||||
Reference in New Issue
Block a user