diff --git a/internal/clientsdb/db.go b/internal/clientsdb/db.go index fd96e25..0c5174d 100644 --- a/internal/clientsdb/db.go +++ b/internal/clientsdb/db.go @@ -295,15 +295,13 @@ func AcceptClientID(conn net.Conn, authorize func(id string, mode byte) error) ( type ackedConn struct { net.Conn - rec []byte - data bool + rec []byte } func (c *ackedConn) Read(b []byte) (int, error) { for { n, err := c.Conn.Read(b) - if err != nil || c.data || !bytes.Equal(b[:n], c.rec) { - c.data = c.data || err == nil + if err != nil || !bytes.Equal(b[:n], c.rec) { return n, err } if _, err := c.Write([]byte{idAck}); err != nil { diff --git a/internal/proxy/tcprelay/tcprelay.go b/internal/proxy/tcprelay/tcprelay.go index 795c2a0..65ad57a 100644 --- a/internal/proxy/tcprelay/tcprelay.go +++ b/internal/proxy/tcprelay/tcprelay.go @@ -313,7 +313,7 @@ func maintainSession(ctx context.Context, deps *Deps, params *Params, peer *net. // локаут и жжёт персону. Джиттер разводит одновременный отказ всех сессий пула. func retryDelay(auth AuthHandler, err error) time.Duration { if errors.Is(err, turndial.ErrAllocQuota) { - return time.Duration(15+randx.Intn(15)) * time.Second + return turndial.QuotaBackoff() } if errors.Is(err, provider.ErrBackoffActive) { if until := auth.BackoffUntilUnix(); until > 0 { diff --git a/internal/proxy/udprelay/loop.go b/internal/proxy/udprelay/loop.go index d0077a3..4c96483 100644 --- a/internal/proxy/udprelay/loop.go +++ b/internal/proxy/udprelay/loop.go @@ -23,9 +23,6 @@ import ( var errPairRecycled = errors.New("udprelay: stream pair recycled") const ( - quotaBackoff = 15 * time.Second - quotaBackoffJitter = 15 * time.Second - pipeBufLimit = 256 << 10 ) @@ -95,9 +92,8 @@ func TURNLoop(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr deps.fatal(err) return } - // Квота может оставаться занятой после deallocate. if errors.Is(err, turndial.ErrAllocQuota) { - wait := quotaBackoff + time.Duration(randx.Intn(int(quotaBackoffJitter/time.Second)))*time.Second + wait := turndial.QuotaBackoff() deps.log().Warnf("[STREAM %d] квота аллокаций занята - пауза %s", streamID, wait) select { case <-ctx.Done(): diff --git a/internal/sub/sub.go b/internal/sub/sub.go index 8b6370e..51694cc 100644 --- a/internal/sub/sub.go +++ b/internal/sub/sub.go @@ -55,8 +55,10 @@ func Fetch(ctx context.Context, url string) (*Sub, error) { dialer := dnsdial.AppDialer(dnsdial.DNSModeAuto) // Клон DefaultTransport, а не пустой: иначе теряются системный прокси и таймауты TLS. - tr, _ := http.DefaultTransport.(*http.Transport) - tr = tr.Clone() + tr := &http.Transport{} + if dt, ok := http.DefaultTransport.(*http.Transport); ok { + tr = dt.Clone() + } tr.DialContext = dialer.DialContext client := &http.Client{Transport: tr} resp, err := client.Do(req) diff --git a/internal/transport/turndial/turndial.go b/internal/transport/turndial/turndial.go index a273104..b8327c6 100644 --- a/internal/transport/turndial/turndial.go +++ b/internal/transport/turndial/turndial.go @@ -21,6 +21,10 @@ import ( // аллокаций. Лечится ожиданием, а не сменой реквизитов (см. IsAuthError в vkauth). var ErrAllocQuota = errors.New("turndial: allocation quota reached") +func QuotaBackoff() time.Duration { + return time.Duration(15+randx.Intn(15)) * time.Second +} + // Config задаёт параметры подключения к TURN-серверу. type Config struct { HostOverride string