From e43a0c314461b5e5b107a723e2cc3f9abe38b1f0 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Sun, 15 Dec 2019 04:26:07 +0200 Subject: Fix breaking the TDlib data on graceful exit --- telegram/connect.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'telegram/connect.go') diff --git a/telegram/connect.go b/telegram/connect.go index 7e9154c..b19e4ed 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -87,7 +87,7 @@ func (stateHandler *clientAuthorizer) Close() { // Connect starts TDlib connection func (c *Client) Connect() error { - if c.online { + if c.Online() { return nil } @@ -113,8 +113,8 @@ func (c *Client) Connect() error { } c.client = tdlibClient - c.online = true c.locks.authorizationReady.Done() + c.listener = tdlibClient.GetListener() go c.updateHandler() @@ -124,7 +124,7 @@ func (c *Client) Connect() error { // Disconnect drops TDlib connection func (c *Client) Disconnect() { // already disconnected - if !c.online { + if !c.Online() { return } @@ -140,14 +140,17 @@ func (c *Client) Disconnect() { ) } - c.client.Destroy() - c.online = false + _, err := c.client.Close() + if err != nil { + log.Fatalf("Couldn't close the Telegram instance: %#v", c) + } } func (c *Client) interactor() { for { state, ok := <-c.authorizer.State if !ok { + log.Error("Interactor is disconnected") return } @@ -192,8 +195,14 @@ func (c *Client) interactor() { } gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in "+c.Session.Login)) - - return + case client.TypeAuthorizationStateClosed: + log.Warn("Closing the updates listener") + c.listener.Close() } } } + +// Online checks if the updates listener is alive +func (c *Client) Online() bool { + return c.listener != nil && c.listener.IsActive() +} -- cgit v1.2.3