handletelegram,gotd: add missing log context

This commit is contained in:
Tulir Asokan
2026-03-03 14:04:46 +02:00
parent 4511c82cb0
commit 7738fc21f5
6 changed files with 60 additions and 9 deletions
+5 -3
View File
@@ -175,7 +175,7 @@ func New(dialer Dialer, opt Options) *Conn {
// handleClose closes rpc engine and underlying connection on context done.
func (c *Conn) handleClose(ctx context.Context) error {
<-ctx.Done()
c.log.Info("Connection context done, closing")
c.log.Info("Connection context done, closing", zap.NamedError("ctx_err", context.Cause(ctx)))
// Close RPC Engine.
c.rpc.ForceClose()
@@ -187,6 +187,8 @@ func (c *Conn) handleClose(ctx context.Context) error {
return nil
}
var errRunReturned = errors.New("Conn.Run() returned")
// Run initializes MTProto connection to server and blocks until disconnection.
//
// When connection is ready, Handler.OnSession is called.
@@ -199,8 +201,8 @@ func (c *Conn) Run(ctx context.Context, f func(ctx context.Context) error) error
return errors.New("do Run on closed connection")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, cancel := context.WithCancelCause(ctx)
defer cancel(errRunReturned)
c.log.Info("Run: start")
defer c.log.Info("Run: end")
+1 -1
View File
@@ -114,7 +114,7 @@ type Client struct {
// Client context. Will be canceled by Run on exit.
ctx context.Context
cancel context.CancelFunc
cancel context.CancelCauseFunc
// Client config.
appID int // immutable
+1 -1
View File
@@ -84,7 +84,7 @@ func newTestClient(h testHandler) *Client {
appHash: TestAppHash,
conn: &testConn{engine: engine, ready: ready},
ctx: context.Background(),
cancel: func() {},
cancel: func(error) {},
updateHandler: UpdateHandlerFunc(func(ctx context.Context, u tg.UpdatesClass) error { return nil }),
onTransfer: noopOnTransfer,
}
+6 -3
View File
@@ -2,6 +2,7 @@ package telegram
import (
"context"
"fmt"
"time"
"github.com/cenkalti/backoff/v4"
@@ -117,6 +118,8 @@ func (c *Client) resetReady() {
c.ready.Reset()
}
var errRunReturned = errors.New("Client.Run() returned")
// Run starts client session and blocks until connection close.
// The f callback is called on successful session initialization and Run
// will return on f() result.
@@ -137,12 +140,12 @@ func (c *Client) Run(ctx context.Context, f func(ctx context.Context) error) (er
// Setting up client context for background operations like updates
// handling or pool creation.
c.ctx, c.cancel = context.WithCancel(ctx)
c.ctx, c.cancel = context.WithCancelCause(ctx)
c.log.Info("Client starting")
defer c.log.Info("Client closed")
// Cancel client on exit.
defer c.cancel()
defer c.cancel(errRunReturned)
defer func() {
c.subConnsMux.Lock()
defer c.subConnsMux.Unlock()
@@ -164,7 +167,7 @@ func (c *Client) Run(ctx context.Context, f func(ctx context.Context) error) (er
g.Go(func(ctx context.Context) error {
select {
case <-ctx.Done():
c.cancel()
c.cancel(fmt.Errorf("Client.Run group context done: %w", context.Cause(ctx)))
return ctx.Err()
case <-c.ctx.Done():
return c.ctx.Err()
+1 -1
View File
@@ -112,7 +112,7 @@ func newMigrationClient(t *testing.T, h migrationTestHandler) *Client {
}),
newConnBackoff: defaultBackoff(clock.System),
ctx: context.Background(),
cancel: func() {},
cancel: func(error) {},
migrationTimeout: 10 * time.Second,
}
client.init()