Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: per-user locks on authorization transfer #1301

Closed
wants to merge 1 commit into from
Closed

Conversation

sadfun
Copy link

@sadfun sadfun commented Jan 29, 2024

Telegram does not seem to allow parallel import of authorization for the same account, even if this is done from different sessions. This commit adds per-autorization

It also would be more efficient not to call .Self() on every transfer (although this is an infrequent), help appreciated.

Closes #1213.

@ernado
Copy link
Member

ernado commented Jan 29, 2024

Closing in favor of #1302 as more abstracted approach. We may want to use multiple clients and something like redis to handle locks, so I've abstracted it into callback.

You can use it like that:

var mux sync.Mutex
locks := make(map[int64]*sync.Mutex)
var opts Options
opts.OnTransfer = func(ctx context.Context, client *Client, fn func(context.Context) error) error {
	u, err := client.Self(ctx)
	if err != nil {
		return err
	}

	mux.Lock()
	lock, ok := locks[u.ID]
	if !ok {
		lock = &sync.Mutex{}
		locks[u.ID] = lock
	}
	mux.Unlock()

	lock.Lock()
	defer lock.Unlock()
	return fn(ctx)
}

Thats it, fn should be called in "transaction".

@ernado ernado closed this Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AUTH_BYTES_INVALID on parallel download of files
2 participants