package telegram
import (
"context"
"time"
"github.com/go-faster/errors"
"github.com/gotd/log"
"github.com/gotd/td/session"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
const guestLogOutTimeout = 5 * time .Second
func (c *Client ) LogOut (ctx context .Context ) (*tg .AuthLoggedOut , error ) {
c .auth .loggedOut .Store (true )
res , err := c .API ().AuthLogOut (ctx )
if err != nil {
if rpcErr , ok := tgerr .As (err ); ok && rpcErr .Code == 401 {
res , err = &tg .AuthLoggedOut {}, nil
} else {
err = errors .Wrap (err , "auth.logOut" )
}
}
c .auth .authorized .Store (false )
c .logOutGuests (ctx )
if wErr := c .wipeSession (ctx ); wErr != nil && err == nil {
err = errors .Wrap (wErr , "wipe session" )
}
return res , err
}
func (c *Client ) logOutGuests (ctx context .Context ) {
home := 0
if c .session != nil {
home = c .session .Load ().DC
}
seen := map [CloseInvoker ]struct {}{}
for _ , cache := range []*dcPoolCache {c .subConns , c .filePools } {
if cache == nil {
continue
}
cache .mux .Lock ()
conns := make (map [int ]CloseInvoker , len (cache .conns ))
for dc , conn := range cache .conns {
conns [dc ] = conn
}
cache .conns = map [int ]CloseInvoker {}
cache .mux .Unlock ()
for dc , conn := range conns {
if _ , ok := seen [conn ]; ok {
continue
}
seen [conn ] = struct {}{}
if dc != home {
lctx , cancel := context .WithTimeout (ctx , guestLogOutTimeout )
if _ , err := tg .NewClient (conn ).AuthLogOut (lctx ); err != nil {
c .log .Debug (ctx , "Guest DC log out failed" , log .Int ("dc_id" , dc ), log .Error (err ))
}
cancel ()
}
if err := conn .Close (); err != nil {
c .log .Debug (ctx , "Close guest DC pool" , log .Int ("dc_id" , dc ), log .Error (err ))
}
}
}
c .fileInvokersMux .Lock ()
c .fileInvokers = map [int ]tg .Invoker {}
c .fileInvokersMux .Unlock ()
c .sessionsMux .Lock ()
for dc := range c .sessions {
delete (c .sessions , dc )
}
for dc := range c .dcAuthorized {
delete (c .dcAuthorized , dc )
}
for dc := range c .sessionStates {
delete (c .sessionStates , dc )
}
c .sessionsMux .Unlock ()
c .selfID .Store (0 )
c .selfBot .Store (false )
}
func (c *Client ) wipeSession (ctx context .Context ) error {
l , ok := c .storage .(*session .Loader )
if !ok || l == nil || l .Storage == nil {
return nil
}
return l .Storage .StoreSession (ctx , nil )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .