Fix session restoring

This commit is contained in:
bodqhrohro 2019-12-04 17:55:15 +02:00
parent 3ce5081800
commit 354a4acd19
2 changed files with 14 additions and 18 deletions

View file

@ -26,7 +26,7 @@ type Session struct {
Login string `yaml:":login"` Login string `yaml:":login"`
} }
var sessionDB SessionsYamlDB var sessionDB *SessionsYamlDB
// SessionMarshaller implementation for YamlDB // SessionMarshaller implementation for YamlDB
func SessionMarshaller() ([]byte, error) { func SessionMarshaller() ([]byte, error) {
@ -34,10 +34,11 @@ func SessionMarshaller() ([]byte, error) {
} }
// LoadSessions restores TDlib sessions from the previous run // LoadSessions restores TDlib sessions from the previous run
func LoadSessions(path string) (SessionsYamlDB, error) { func LoadSessions(path string) (*SessionsYamlDB, error) {
var sessionData SessionsMap var sessionData SessionsMap
var err error
sessionDB, err := initYamlDB(path, &sessionData) sessionDB, err = initYamlDB(path, &sessionData)
if err != nil { if err != nil {
return sessionDB, errors.Wrap(err, "Sessions restore error") return sessionDB, errors.Wrap(err, "Sessions restore error")
} }
@ -49,12 +50,12 @@ func emptySessionsMap(dataPtr *SessionsMap) {
dataPtr.Sessions = make(map[string]Session) dataPtr.Sessions = make(map[string]Session)
} }
func initYamlDB(path string, dataPtr *SessionsMap) (SessionsYamlDB, error) { func initYamlDB(path string, dataPtr *SessionsMap) (*SessionsYamlDB, error) {
file, err := ioutil.ReadFile(path) file, err := ioutil.ReadFile(path)
if err == nil { if err == nil {
err = yaml.Unmarshal(file, dataPtr) err = yaml.Unmarshal(file, dataPtr)
if err != nil { if err != nil {
return SessionsYamlDB{}, errors.Wrap(err, "YamlDB is corrupted") return nil, errors.Wrap(err, "YamlDB is corrupted")
} }
if dataPtr.Sessions == nil { if dataPtr.Sessions == nil {
@ -66,7 +67,7 @@ func initYamlDB(path string, dataPtr *SessionsMap) (SessionsYamlDB, error) {
emptySessionsMap(dataPtr) emptySessionsMap(dataPtr)
} }
return SessionsYamlDB{ return &SessionsYamlDB{
YamlDB: yamldb.YamlDB{ YamlDB: yamldb.YamlDB{
Path: path, Path: path,
PathNew: path + ".new", PathNew: path + ".new",

View file

@ -13,11 +13,9 @@ import (
"gosrc.io/xmpp" "gosrc.io/xmpp"
) )
const pollingInterval time.Duration = 1e7
var tgConf config.TelegramConfig var tgConf config.TelegramConfig
var sessions map[string]*telegram.Client var sessions map[string]*telegram.Client
var db persistence.SessionsYamlDB var db *persistence.SessionsYamlDB
// NewComponent starts a new component and wraps it in // NewComponent starts a new component and wraps it in
// a stream manager that you should start yourself // a stream manager that you should start yourself
@ -53,9 +51,9 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
return nil, nil, err return nil, nil, err
} }
sm := xmpp.NewStreamManager(component, nil) sm := xmpp.NewStreamManager(component, func(s xmpp.Sender) {
go heartbeat(component) go heartbeat(component)
})
return sm, component, nil return sm, component, nil
} }
@ -65,12 +63,9 @@ func heartbeat(component *xmpp.Component) {
probeType := gateway.SPType("probe") probeType := gateway.SPType("probe")
for jid := range sessions { for jid := range sessions {
for {
err = gateway.SendPresence(component, jid, probeType) err = gateway.SendPresence(component, jid, probeType)
if err == nil { if err != nil {
break log.Error(err)
}
time.Sleep(pollingInterval)
} }
} }