another try at improving connection handling

This commit is contained in:
2022-03-01 21:21:24 +01:00
parent 7260bedfa4
commit d0679bd3b7

View File

@@ -23,7 +23,9 @@ import (
) )
const ( const (
APPID = 730 APPID = 730
LOGIN_FAILED = iota
LOGIN_SUCCESS
) )
type DemoMatchLoaderConfig struct { type DemoMatchLoaderConfig struct {
@@ -41,21 +43,22 @@ type DemoMatchLoaderConfig struct {
} }
type DemoMatchLoader struct { type DemoMatchLoader struct {
client *steam.Client client *steam.Client
GCReady bool GCReady bool
steamLogin *steam.LogOnDetails steamLogin *steam.LogOnDetails
matchRecv chan *protobuf.CMsgGCCStrike15V2_MatchList matchRecv chan *protobuf.CMsgGCCStrike15V2_MatchList
cmList []*netutil.PortAddr cmList []*netutil.PortAddr
sentryFile string sentryFile string
loginKey string loginKey string
db *ent.Client db *ent.Client
dp *DemoParser dp *DemoParser
parseDemo chan *Demo parseDemo chan *Demo
parseMap map[string]bool parseMap map[string]bool
parseMapL *sync.RWMutex parseMapL *sync.RWMutex
cache *cache.Cache cache *cache.Cache
connecting *sync.Mutex connectionWait uint64
connectionWait uint64 connectFeedback chan int
LoggedIn bool
} }
func AccountId2SteamId(accId uint32) uint64 { func AccountId2SteamId(accId uint32) uint64 {
@@ -185,7 +188,6 @@ func (dml *DemoMatchLoader) Setup(config *DemoMatchLoaderConfig) error {
dml.parseMap = map[string]bool{} dml.parseMap = map[string]bool{}
dml.parseMapL = new(sync.RWMutex) dml.parseMapL = new(sync.RWMutex)
dml.cache = config.Cache dml.cache = config.Cache
dml.connecting = new(sync.Mutex)
err := dml.dp.Setup(config.Db, config.Worker, config.SprayTimeout) err := dml.dp.Setup(config.Db, config.Worker, config.SprayTimeout)
if err != nil { if err != nil {
return err return err
@@ -232,7 +234,7 @@ func (dml *DemoMatchLoader) Setup(config *DemoMatchLoaderConfig) error {
return nil return nil
} }
func (dml DemoMatchLoader) LoadDemo(demo *Demo) error { func (dml *DemoMatchLoader) LoadDemo(demo *Demo) error {
select { select {
case dml.parseDemo <- demo: case dml.parseDemo <- demo:
return nil return nil
@@ -241,21 +243,30 @@ func (dml DemoMatchLoader) LoadDemo(demo *Demo) error {
} }
} }
func (dml DemoMatchLoader) connectLoop() { func (dml *DemoMatchLoader) connectLoop() {
dml.connecting.Lock() for {
defer dml.connecting.Unlock() select {
if !dml.client.Connected() { case res := <-dml.connectFeedback:
for dml.connectToSteam() != nil { switch res {
log.Infof("[DL] Retrying connecting to steam...") case LOGIN_FAILED:
if dml.connectionWait == 0 { time.Sleep(time.Minute * time.Duration(dml.connectionWait))
dml.connectionWait = 1 if !dml.LoggedIn {
log.Infof("[DL] Retrying connecting to steam...")
err := dml.connectToSteam()
if err != nil {
log.Warningf("[DL] Error connecting to steam: %v", err)
}
if dml.connectionWait == 0 {
dml.connectionWait = 1
}
dml.connectionWait *= 2
}
case LOGIN_SUCCESS:
log.Infof("[DL] Steam login successfully restored after %d minutes", dml.connectionWait)
dml.connectionWait = 0
} }
time.Sleep(time.Minute * time.Duration(dml.connectionWait))
dml.connectionWait *= 2
}
if dml.client.Connected() {
log.Infof("[DL] Steam login successfully restored after %d minutes", dml.connectionWait)
dml.connectionWait = 1
} }
} }
} }
@@ -274,6 +285,8 @@ func (dml *DemoMatchLoader) steamEventHandler() {
} }
case *steam.LoggedOnEvent: case *steam.LoggedOnEvent:
log.Debug("[DL] Steam login success!") log.Debug("[DL] Steam login success!")
dml.LoggedIn = true
dml.connectFeedback <- LOGIN_SUCCESS
dml.client.Social.SetPersonaState(steamlang.EPersonaState_Online) dml.client.Social.SetPersonaState(steamlang.EPersonaState_Online)
go dml.setPlaying() go dml.setPlaying()
case *steam.LogOnFailedEvent: case *steam.LogOnFailedEvent:
@@ -285,15 +298,16 @@ func (dml *DemoMatchLoader) steamEventHandler() {
_ = os.Remove(dml.sentryFile) _ = os.Remove(dml.sentryFile)
_ = os.Remove(dml.loginKey) _ = os.Remove(dml.loginKey)
log.Warningf("[DL] Steam login failed: InvalidPassword") log.Warningf("[DL] Steam login failed: InvalidPassword")
go dml.connectLoop()
case steamlang.EResult_InvalidLoginAuthCode: case steamlang.EResult_InvalidLoginAuthCode:
log.Fatalf("[DL] Steam auth code wrong") log.Fatalf("[DL] Steam auth code wrong")
default: default:
log.Warningf("[DL] Unhandled login fasiled event %+v", e) log.Warningf("[DL] Unhandled login failed event %+v", e)
} }
case *steam.DisconnectedEvent: case *steam.DisconnectedEvent:
log.Warningf("Steam disconnected, trying to reconnect...") log.Warningf("Steam disconnected, trying to reconnect...")
go dml.connectLoop() dml.GCReady = false
dml.LoggedIn = false
dml.connectFeedback <- LOGIN_FAILED
case *steam.LoginKeyEvent: case *steam.LoginKeyEvent:
log.Debug("Got login_key!") log.Debug("Got login_key!")
err := ioutil.WriteFile(dml.loginKey, []byte(e.LoginKey), os.ModePerm) err := ioutil.WriteFile(dml.loginKey, []byte(e.LoginKey), os.ModePerm)
@@ -302,8 +316,14 @@ func (dml *DemoMatchLoader) steamEventHandler() {
} }
case *steam.FatalErrorEvent: case *steam.FatalErrorEvent:
log.Debugf("[DL] Got FatalError %+v", e) log.Debugf("[DL] Got FatalError %+v", e)
dml.GCReady = false
dml.LoggedIn = false
dml.connectFeedback <- LOGIN_FAILED
case error: case error:
log.Warningf("[DL] Error: %+v", e) log.Warningf("[DL] Error: %+v", e)
dml.GCReady = false
dml.LoggedIn = false
dml.connectFeedback <- LOGIN_FAILED
default: default:
log.Debugf("[DL] %T: %v", e, e) log.Debugf("[DL] %T: %v", e, e)
} }