Skip to content

Commit

Permalink
fix: try lowering the chance of hangup
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 21, 2024
1 parent 3cce5d9 commit f621c72
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions internal/mtproto/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func (opt *Options) setDefaults() {
opt.SaltFetchInterval = 1 * time.Hour
}
if opt.PingTimeout == 0 {
opt.PingTimeout = 15 * time.Second
opt.PingTimeout = 10 * time.Second
}
if opt.PingInterval == 0 {
opt.PingInterval = 1 * time.Minute
opt.PingInterval = 15 * time.Second
}
if opt.RequestTimeout == nil {
opt.RequestTimeout = func(req uint32) time.Duration {
Expand Down
19 changes: 14 additions & 5 deletions telegram/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func (c *Client) runUntilRestart(ctx context.Context) error {
if !c.noUpdatesMode {
g.Go(func(ctx context.Context) error {
// Call method which requires authorization, to subscribe for updates.
// See https://core.telegram.org/api/updates#subscribing-to-updates.
// See https://core.telegram.org/api/updates#subscribing-to-updates
c.log.Debug("Calling c.Self to subscribe for updates")
self, err := c.Self(ctx)
if err != nil {
// Ignore unauthorized errors.
Expand Down Expand Up @@ -58,21 +59,29 @@ func (c *Client) isPermanentError(err error) bool {
}

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 ctxErr := ctx.Err(); ctxErr != nil {
c.log.Error("Stopping reconnection attempts due to parent context error",
zap.Error(err),
zap.Errors("error_parent", []error{ctxErr}),
)
return errors.Wrap(err, "parent context closed")
}
if c.isPermanentError(err) {
return backoff.Permanent(err)
}
return err
return errors.Wrap(err, "runUntilRestart")
}

return nil
}, b, func(err error, timeout time.Duration) {
c.log.Info("Restarting connection", zap.Error(err), zap.Duration("backoff", timeout))
c.log.Info("Restarting connection",
zap.Error(err),
zap.Duration("backoff", timeout),
)

c.connMux.Lock()
c.conn = c.createPrimaryConn(nil)
Expand Down
4 changes: 3 additions & 1 deletion telegram/internal/manager/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ func (c *Conn) Run(ctx context.Context) (err error) {
defer func() {
if err != nil && ctx.Err() == nil {
c.log.Debug("Connection dead", zap.Error(err))
} else {
c.log.Debug("Connection closed")
}
}()
return c.proto.Run(ctx, func(ctx context.Context) error {
// Signal death on init error. Otherwise connection shutdown
// Signal death on init error. Otherwise, connection shutdown
// deadlocks in OnSession that occurs before init fails.
err := c.init(ctx)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion telegram/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func defaultBackoff(c clock.Clock) func() backoff.BackOff {
return func() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Clock = c
b.MaxElapsedTime = 0
b.MaxElapsedTime = time.Minute
b.InitialInterval = time.Millisecond * 100
b.MaxInterval = time.Second
return b
}
}

0 comments on commit f621c72

Please sign in to comment.