mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:34:02 +03:00
Do not use unnecessary lock in connRewind
This commit is contained in:
+2
-10
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/9seconds/mtg/v2/essentials"
|
"github.com/9seconds/mtg/v2/essentials"
|
||||||
)
|
)
|
||||||
@@ -40,22 +39,15 @@ func (c connTraffic) Write(b []byte) (int, error) {
|
|||||||
type connRewind struct {
|
type connRewind struct {
|
||||||
essentials.Conn
|
essentials.Conn
|
||||||
|
|
||||||
active io.Reader
|
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
mutex sync.RWMutex
|
active io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connRewind) Read(p []byte) (int, error) {
|
func (c *connRewind) Read(p []byte) (int, error) {
|
||||||
c.mutex.RLock()
|
return c.active.Read(p)
|
||||||
defer c.mutex.RUnlock()
|
|
||||||
|
|
||||||
return c.active.Read(p) //nolint: wrapcheck
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connRewind) Rewind() {
|
func (c *connRewind) Rewind() {
|
||||||
c.mutex.Lock()
|
|
||||||
defer c.mutex.Unlock()
|
|
||||||
|
|
||||||
c.active = io.MultiReader(&c.buf, c.Conn)
|
c.active = io.MultiReader(&c.buf, c.Conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -243,14 +243,14 @@ func (p *Proxy) doTelegramCall(ctx *streamContext) error {
|
|||||||
return fmt.Errorf("no addresses to call: %w", err)
|
return fmt.Errorf("no addresses to call: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err = foundAddr.Obfuscator.SendHandshake(conn, ctx.dc)
|
tgConn, err := foundAddr.Obfuscator.SendHandshake(conn, ctx.dc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return fmt.Errorf("cannot perform server handshake: %w", err)
|
return fmt.Errorf("cannot perform server handshake: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.telegramConn = connTraffic{
|
ctx.telegramConn = connTraffic{
|
||||||
Conn: conn,
|
Conn: tgConn,
|
||||||
streamID: ctx.streamID,
|
streamID: ctx.streamID,
|
||||||
stream: p.eventStream,
|
stream: p.eventStream,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
|||||||
Reference in New Issue
Block a user