From 23e26dc7c7c3d14166af27c6f5d2bc26747f9475 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 8 Oct 2024 12:40:47 +0300 Subject: [PATCH 1/3] fix(telegram): reset backoff on connection ready --- telegram/connect.go | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/telegram/connect.go b/telegram/connect.go index f93ebbaf22..a4d484f567 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -61,23 +61,36 @@ func (c *Client) reconnectUntilClosed(ctx context.Context) error { // Note that we currently have no timeout on connection, so this is // potentially eternal. b := tdsync.SyncBackoff(backoff.WithContext(c.connBackoff(), ctx)) - - return backoff.RetryNotify(func() error { - if err := c.runUntilRestart(ctx); err != nil { - if c.isPermanentError(err) { - return backoff.Permanent(err) - } - return err + g := tdsync.NewCancellableGroup(ctx) + g.Go(func(ctx context.Context) error { + select { + case <-ctx.Done(): + return ctx.Err() + case <-c.ready.Ready(): + // Reset backoff on successful connection. + b.Reset() + return nil } + }) + g.Go(func(ctx context.Context) error { + return backoff.RetryNotify(func() error { + if err := c.runUntilRestart(ctx); err != nil { + if c.isPermanentError(err) { + return backoff.Permanent(err) + } + return err + } - return nil - }, b, func(err error, timeout time.Duration) { - c.log.Info("Restarting connection", zap.Error(err), zap.Duration("backoff", timeout)) + return nil + }, b, func(err error, timeout time.Duration) { + c.log.Info("Restarting connection", zap.Error(err), zap.Duration("backoff", timeout)) - c.connMux.Lock() - c.conn = c.createPrimaryConn(nil) - c.connMux.Unlock() + c.connMux.Lock() + c.conn = c.createPrimaryConn(nil) + c.connMux.Unlock() + }) }) + return g.Wait() } func (c *Client) onReady() { From eaeb7899df3f8e2353c283442b8999697db2c221 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 8 Oct 2024 12:40:53 +0300 Subject: [PATCH 2/3] chore(deps): update --- examples/go.mod | 2 +- examples/go.sum | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/go.mod b/examples/go.mod index 4ceadc7371..cdaf8c4005 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -33,7 +33,7 @@ require ( github.com/golang/snappy v0.0.4 // indirect github.com/gotd/ige v0.2.2 // indirect github.com/gotd/neo v0.1.5 // indirect - github.com/klauspost/compress v1.17.9 // indirect + github.com/klauspost/compress v1.17.10 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/examples/go.sum b/examples/go.sum index 4c82ff1af2..e1e6709cdc 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -51,6 +51,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= From 951db34b9d3779b87634681fcce8fa354c24ec51 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 8 Oct 2024 12:45:31 +0300 Subject: [PATCH 3/3] fix(telegram): reset backoff until reconnectUntilClosed --- telegram/connect.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/telegram/connect.go b/telegram/connect.go index a4d484f567..f0140e52e5 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -63,13 +63,14 @@ func (c *Client) reconnectUntilClosed(ctx context.Context) error { b := tdsync.SyncBackoff(backoff.WithContext(c.connBackoff(), ctx)) g := tdsync.NewCancellableGroup(ctx) g.Go(func(ctx context.Context) error { - select { - case <-ctx.Done(): - return ctx.Err() - case <-c.ready.Ready(): - // Reset backoff on successful connection. - b.Reset() - return nil + for { + select { + case <-ctx.Done(): + return ctx.Err() + case <-c.ready.Ready(): + // Reset backoff on successful connection. + b.Reset() + } } }) g.Go(func(ctx context.Context) error {