mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-02 00:11:56 +03:00
Merge pull request #139 from 9seconds/memory-direct
Memory optimizations for direct mode
This commit is contained in:
+4
-3
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/9seconds/mtg/protocol"
|
"github.com/9seconds/mtg/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
const directPipeBufferSize = 1024 * 1024
|
const directPipeBufferSize = 1024
|
||||||
|
|
||||||
func directConnection(request *protocol.TelegramRequest) error {
|
func directConnection(request *protocol.TelegramRequest) error {
|
||||||
telegramConnRaw, err := obfuscated2.TelegramProtocol(request)
|
telegramConnRaw, err := obfuscated2.TelegramProtocol(request)
|
||||||
@@ -42,8 +42,9 @@ func directPipe(dst io.WriteCloser, src io.ReadCloser, wg *sync.WaitGroup, logge
|
|||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
buf := make([]byte, directPipeBufferSize)
|
buf := [directPipeBufferSize]byte{}
|
||||||
if _, err := io.CopyBuffer(dst, src, buf); err != nil {
|
|
||||||
|
if _, err := io.CopyBuffer(dst, src, buf[:]); err != nil {
|
||||||
logger.Debugw("Cannot pump sockets", "error", err)
|
logger.Debugw("Cannot pump sockets", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package stream
|
package stream
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -11,6 +13,23 @@ import (
|
|||||||
"github.com/9seconds/mtg/conntypes"
|
"github.com/9seconds/mtg/conntypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
poolWrapperObfuscated2WritePool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &bytes.Buffer{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func poolWrapperObfuscated2WritePoolAcquire() *bytes.Buffer {
|
||||||
|
return poolWrapperObfuscated2WritePool.Get().(*bytes.Buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func poolWrapperObfuscated2WritePoolRelease(buf *bytes.Buffer) {
|
||||||
|
buf.Reset()
|
||||||
|
poolWrapperObfuscated2WritePool.Put(buf)
|
||||||
|
}
|
||||||
|
|
||||||
type wrapperObfuscated2 struct {
|
type wrapperObfuscated2 struct {
|
||||||
encryptor cipher.Stream
|
encryptor cipher.Stream
|
||||||
decryptor cipher.Stream
|
decryptor cipher.Stream
|
||||||
@@ -40,16 +59,26 @@ func (w *wrapperObfuscated2) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapperObfuscated2) WriteTimeout(p []byte, timeout time.Duration) (int, error) {
|
func (w *wrapperObfuscated2) WriteTimeout(p []byte, timeout time.Duration) (int, error) {
|
||||||
buf := make([]byte, len(p))
|
buffer := poolWrapperObfuscated2WritePoolAcquire()
|
||||||
copy(buf, p)
|
defer poolWrapperObfuscated2WritePoolRelease(buffer)
|
||||||
|
|
||||||
|
buffer.Write(p)
|
||||||
|
|
||||||
|
buf := buffer.Bytes()
|
||||||
|
|
||||||
w.encryptor.XORKeyStream(buf, buf)
|
w.encryptor.XORKeyStream(buf, buf)
|
||||||
|
|
||||||
return w.parent.WriteTimeout(buf, timeout)
|
return w.parent.WriteTimeout(buf, timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapperObfuscated2) Write(p []byte) (int, error) {
|
func (w *wrapperObfuscated2) Write(p []byte) (int, error) {
|
||||||
buf := make([]byte, len(p))
|
buffer := poolWrapperObfuscated2WritePoolAcquire()
|
||||||
copy(buf, p)
|
defer poolWrapperObfuscated2WritePoolRelease(buffer)
|
||||||
|
|
||||||
|
buffer.Write(p)
|
||||||
|
|
||||||
|
buf := buffer.Bytes()
|
||||||
|
|
||||||
w.encryptor.XORKeyStream(buf, buf)
|
w.encryptor.XORKeyStream(buf, buf)
|
||||||
|
|
||||||
return w.parent.Write(buf)
|
return w.parent.Write(buf)
|
||||||
|
|||||||
Reference in New Issue
Block a user