Fix crash on login

calls
Bohdan Horbeshko 1 year ago
parent d66f87485d
commit 18a17cb7a8

@ -63,7 +63,7 @@ type Client struct {
} }
type clientLocks struct { type clientLocks struct {
authorizationReady sync.WaitGroup authorizationReady sync.Mutex
chatMessageLocks map[int64]*sync.Mutex chatMessageLocks map[int64]*sync.Mutex
resourcesLock sync.Mutex resourcesLock sync.Mutex
} }

@ -96,11 +96,14 @@ func (stateHandler *clientAuthorizer) Close() {
// Connect starts TDlib connection // Connect starts TDlib connection
func (c *Client) Connect(resource string) error { func (c *Client) Connect(resource string) error {
log.Warn("Attempting to connect to Telegram network...")
// avoid conflict if another authorization is pending already // avoid conflict if another authorization is pending already
c.locks.authorizationReady.Wait() c.locks.authorizationReady.Lock()
if c.Online() { if c.Online() {
c.roster(resource) c.roster(resource)
c.locks.authorizationReady.Unlock()
return nil return nil
} }
@ -116,15 +119,13 @@ func (c *Client) Connect(resource string) error {
LastName: make(chan string, 1), LastName: make(chan string, 1),
} }
c.locks.authorizationReady.Add(1)
go c.interactor() go c.interactor()
c.authorizer.TdlibParameters <- c.parameters c.authorizer.TdlibParameters <- c.parameters
tdlibClient, err := client.NewClient(c.authorizer, c.options...) tdlibClient, err := client.NewClient(c.authorizer, c.options...)
if err != nil { if err != nil {
c.locks.authorizationReady.Done() c.locks.authorizationReady.Unlock()
return errors.Wrap(err, "Couldn't initialize a Telegram client instance") return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
} }
@ -142,7 +143,7 @@ func (c *Client) Connect(resource string) error {
go c.updateHandler() go c.updateHandler()
c.online = true c.online = true
c.locks.authorizationReady.Done() c.locks.authorizationReady.Unlock()
c.addResource(resource) c.addResource(resource)
go func() { go func() {

Loading…
Cancel
Save