Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
count methods to prevent bsod (#20)
Browse files Browse the repository at this point in the history
* count methods to prevent bsod

* only disable account if it's not yet disabled

* reset counter
  • Loading branch information
Fabio1988 authored Sep 14, 2023
1 parent 297ec1f commit 41314f2
Show file tree
Hide file tree
Showing 9 changed files with 192,385 additions and 116,134 deletions.
36 changes: 26 additions & 10 deletions accounts/accountManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package accounts

import (
"errors"
"flygon/config"
"flygon/db"
"flygon/pogo"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -95,20 +96,30 @@ func (a *AccountManager) GetNextAccount(testAccount func(a db.Account) bool) *Ac
a.accountLock.Lock()
defer a.accountLock.Unlock()

time24hrAgo := time.Now().Add(-24 * time.Hour).Unix()
timeNow := time.Now().Unix()
hoursDisabled := []int{24, 7 * 24, 30 * 24}
timeNow := time.Now()
timeNowUnix := timeNow.Unix()
minimumTimeForReuse := timeNow

if config.Config.Tuning.MinimumAccountReuseHours > 0 {
minimumTimeForReuse = minimumTimeForReuse.Add(-1 * time.Duration(config.Config.Tuning.MinimumAccountReuseHours) * time.Hour)
}
minimumTimeForReuseUnix := minimumTimeForReuse.Unix()

minReleased := int64(0)
bestAccount := -1
for x := 0; x < len(a.accounts); x++ {
account := a.accounts[x]
disabledTimeout := time.Duration(hoursDisabled[0]) * time.Hour
// TODO consecutive count

if a.inUse[x] ||
account.Suspended ||
account.Banned ||
account.Invalid ||
int64(account.WarnExpiration) > timeNow ||
(account.LastDisabled.Valid && account.LastDisabled.Int64 > time24hrAgo) {
int64(account.WarnExpiration) > timeNowUnix ||
(account.LastDisabled.Valid && account.LastDisabled.Int64 > timeNow.Add(-disabledTimeout).Unix()) ||
(account.LastSelected.Valid && account.LastSelected.Int64 > minimumTimeForReuseUnix) {
continue
}

Expand All @@ -132,8 +143,13 @@ func (a *AccountManager) GetNextAccount(testAccount func(a db.Account) bool) *Ac
}

account := &a.accounts[bestAccount]
if minReleased > time24hrAgo {
log.Warnf("Selected account %s was last released %d minutes ago, which is less than 24 hours ago. This is probably not what you want.", account.Username, (time.Now().Unix()-minReleased)/60)
if minReleased > timeNow.Add(-24*time.Hour).Unix() {
log.Warnf("Selected account %s was last released %d minutes ago, which is less than 24 hours ago. This is probably not what you want.", account.Username,
(time.Now().Unix()-minReleased)/60)
} else if minReleased > timeNow.Add(-24*7*time.Hour).Unix() {
log.Warnf("Selected account %s was last released %d minutes ago (%s), which is less than 1 week ago. This is probably not what you want.", account.Username,
(time.Now().Unix()-minReleased)/60,
time.Since(time.Unix(minReleased, 0)))
}

a.inUse[bestAccount] = true
Expand Down Expand Up @@ -194,11 +210,12 @@ func (a *AccountManager) AccountExists(username string) bool {
func (a *AccountManager) IsValidAccount(username string) (bool, error) {
for x := range a.accounts {
if a.accounts[x].Username == username {
time24hrAgo := time.Now().Add(-24 * time.Hour).Unix()
timeNow := time.Now().Unix()
timeNow := time.Now()
timeNowUnix := timeNow.Unix()
time24hrAgo := timeNow.Add(-24 * time.Hour).Unix()
return !(a.accounts[x].Suspended ||
a.accounts[x].Banned ||
int64(a.accounts[x].WarnExpiration) > timeNow ||
int64(a.accounts[x].WarnExpiration) > timeNowUnix ||
(a.accounts[x].LastDisabled.Valid && a.accounts[x].LastDisabled.Int64 > time24hrAgo)), nil
}
}
Expand Down Expand Up @@ -257,7 +274,6 @@ func (a *AccountManager) MarkDisabled(username string) {

for x := range a.accounts {
if a.accounts[x].Username == username {

a.accounts[x].LastDisabled = null.IntFrom(time.Now().Unix())
}
}
Expand Down
5 changes: 5 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ user = ""
password = ""
name = "flygon"

[tuning]
recycle_gmo_limit = 4950
recycle_encounter_limit = 9950
minimum_account_reuse_hours = 168

[sentry]
dsn = ""

Expand Down
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type configDefinition struct {
Processors processorDefinition `koanf:"processors"`
Worker workerDefinition `koanf:"worker"`
Db DbDefinition `koanf:"db"`
Tuning tuningDefinition `koanf:"tuning"`
Sentry sentry `koanf:"sentry"`
Prometheus prometheus `koanf:"prometheus"`
Pyroscope pyroscope `koanf:"pyroscope"`
Expand Down Expand Up @@ -43,6 +44,12 @@ type DbDefinition struct {
MaxPool int `koanf:"max_pool"`
}

type tuningDefinition struct {
RecycleGmoLimit int `koanf:"recycle_gmo_limit"`
RecycleEncounterLimit int `koanf:"recycle_encounter_limit"`
MinimumAccountReuseHours int `koanf:"minimum_account_reuse_hours"`
}

type sentry struct {
DSN string `koanf:"dsn"`
Debug bool `koanf:"debug"`
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"flygon/external"
"flygon/golbatapi"
"flygon/koji"
"flygon/pogo"
"flygon/routes"
"flygon/tz"
"flygon/util"
Expand Down Expand Up @@ -59,16 +60,25 @@ func main() {
if config.Config.Koji.LoadAtStartup {
koji.LoadKojiAreas(&dbDetails)
}

requestLimits := make(map[int]int)
if config.Config.Tuning.RecycleGmoLimit > 0 {
requestLimits[int(pogo.Method_METHOD_GET_MAP_OBJECTS)] = config.Config.Tuning.RecycleGmoLimit
}
if config.Config.Tuning.RecycleEncounterLimit > 0 {
requestLimits[int(pogo.Method_METHOD_ENCOUNTER)] = config.Config.Tuning.RecycleEncounterLimit
}

routes.ConnectDatabase(&dbDetails)
routes.LoadAccountManager(&am)
worker.StartAreas(dbDetails)
worker.InitWorkerState()
worker.SetWorkerUnseen()
//worker.StartUnbound(dbDetails, &am, authenticationQueue)
if config.Config.Processors.GolbatEndpoint != "" {
golbatapi.SetApiUrl(config.Config.Processors.GolbatEndpoint,
config.Config.Processors.GolbatApiSecret)
}
worker.SetRequestLimits(requestLimits)
routes.SetRawEndpoints(getRawEndpointsFromConfig())
routes.StartGin()

Expand Down
Loading

0 comments on commit 41314f2

Please sign in to comment.