Skip to content

Commit a2c34af

Browse files
authored
Merge pull request #1450 from gotd/fix/reset-backoff-on-connection-ready
fix(telegram): reset backoff on connection ready
2 parents 024d4de + 951db34 commit a2c34af

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

examples/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/golang/snappy v0.0.4 // indirect
3434
github.com/gotd/ige v0.2.2 // indirect
3535
github.com/gotd/neo v0.1.5 // indirect
36-
github.com/klauspost/compress v1.17.9 // indirect
36+
github.com/klauspost/compress v1.17.10 // indirect
3737
github.com/kr/pretty v0.3.1 // indirect
3838
github.com/kr/text v0.2.0 // indirect
3939
github.com/pkg/errors v0.9.1 // indirect

examples/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
5151
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
5252
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
5353
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
54+
github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
5455
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
5556
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
5657
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=

telegram/connect.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,37 @@ func (c *Client) reconnectUntilClosed(ctx context.Context) error {
6161
// Note that we currently have no timeout on connection, so this is
6262
// potentially eternal.
6363
b := tdsync.SyncBackoff(backoff.WithContext(c.connBackoff(), ctx))
64-
65-
return backoff.RetryNotify(func() error {
66-
if err := c.runUntilRestart(ctx); err != nil {
67-
if c.isPermanentError(err) {
68-
return backoff.Permanent(err)
64+
g := tdsync.NewCancellableGroup(ctx)
65+
g.Go(func(ctx context.Context) error {
66+
for {
67+
select {
68+
case <-ctx.Done():
69+
return ctx.Err()
70+
case <-c.ready.Ready():
71+
// Reset backoff on successful connection.
72+
b.Reset()
6973
}
70-
return err
7174
}
75+
})
76+
g.Go(func(ctx context.Context) error {
77+
return backoff.RetryNotify(func() error {
78+
if err := c.runUntilRestart(ctx); err != nil {
79+
if c.isPermanentError(err) {
80+
return backoff.Permanent(err)
81+
}
82+
return err
83+
}
7284

73-
return nil
74-
}, b, func(err error, timeout time.Duration) {
75-
c.log.Info("Restarting connection", zap.Error(err), zap.Duration("backoff", timeout))
85+
return nil
86+
}, b, func(err error, timeout time.Duration) {
87+
c.log.Info("Restarting connection", zap.Error(err), zap.Duration("backoff", timeout))
7688

77-
c.connMux.Lock()
78-
c.conn = c.createPrimaryConn(nil)
79-
c.connMux.Unlock()
89+
c.connMux.Lock()
90+
c.conn = c.createPrimaryConn(nil)
91+
c.connMux.Unlock()
92+
})
8093
})
94+
return g.Wait()
8195
}
8296

8397
func (c *Client) onReady() {

0 commit comments

Comments
 (0)