Skip to content

Commit

Permalink
feat(clint): add OnTransfer callback
Browse files Browse the repository at this point in the history
Support external locking for auth transfer.
  • Loading branch information
ernado committed Jan 29, 2024
1 parent b8980c5 commit b2da34b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
4 changes: 4 additions & 0 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ type Client struct {

// Tracing.
tracer trace.Tracer

// onTransfer is called on authorization transfer.
onTransfer func(ctx context.Context, tx func(ctx context.Context) error) error
}

// NewClient creates new unstarted client.
Expand Down Expand Up @@ -161,6 +164,7 @@ func NewClient(appID int, appHash string, opt Options) *Client {
migrationTimeout: opt.MigrationTimeout,
noUpdatesMode: opt.NoUpdates,
mw: opt.Middlewares,
onTransfer: opt.OnTransfer,
}
if opt.TracerProvider != nil {
client.tracer = opt.TracerProvider.Tracer(oteltg.Name)
Expand Down
11 changes: 11 additions & 0 deletions telegram/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ type Options struct {

// OpenTelemetry.
TracerProvider trace.TracerProvider

// OnTransfer is called on authorization transfer to acquire external lock.
//
// The function should call tx function to perform transfer, serializing it
// with external lock.
//
// The function must return error if tx function returned error.
OnTransfer func(ctx context.Context, tx func(context.Context) error) error
}

func (opt *Options) setDefaults() {
Expand Down Expand Up @@ -140,6 +148,9 @@ func (opt *Options) setDefaults() {
return nil
})
}
if opt.OnTransfer == nil {
opt.OnTransfer = noopOnTransfer
}
}

func defaultBackoff(c clock.Clock) func() backoff.BackOff {
Expand Down
33 changes: 21 additions & 12 deletions telegram/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,29 @@ func (c *Client) exportAuth(ctx context.Context, dcID int) (*tg.AuthExportedAuth
return export, nil
}

func noopOnTransfer(ctx context.Context, fn func(context.Context) error) error { return fn(ctx) }

// transfer exports current authorization and imports it to another DC.
// See https://core.telegram.org/api/datacenter#authorization-transfer.
func (c *Client) transfer(ctx context.Context, to *tg.Client, dc int) (tg.AuthAuthorizationClass, error) {
auth, err := c.exportAuth(ctx, dc)
if err != nil {
return nil, errors.Wrapf(err, "export to %d", dc)
}

req := &tg.AuthImportAuthorizationRequest{}
req.FillFrom(auth)
r, err := to.AuthImportAuthorization(ctx, req)
if err != nil {
return nil, errors.Wrapf(err, "import from %d", dc)
var out tg.AuthAuthorizationClass
if err := c.onTransfer(ctx, func(ctx context.Context) error {
auth, err := c.exportAuth(ctx, dc)
if err != nil {
return errors.Wrapf(err, "export to %d", dc)
}

req := &tg.AuthImportAuthorizationRequest{}
req.FillFrom(auth)
r, err := to.AuthImportAuthorization(ctx, req)
if err != nil {
return errors.Wrapf(err, "import from %d", dc)
}

out = r
return nil
}); err != nil {
return nil, errors.Wrap(err, "onTransfer")
}

return r, nil
return out, nil
}

0 comments on commit b2da34b

Please sign in to comment.