Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce CPU usage (RefreshGameData) #702

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ func (b *Bot) Run(ctx context.Context, firstRun bool, runs []run.Run) error {
for {
select {
case <-ctx.Done():
cancel()
b.Stop()
ticker.Stop()
return nil
case <-b.ctx.RefreshRequest:
ticker.Reset(100 * time.Millisecond)
case <-ticker.C:
if b.ctx.ExecutionPriority == botCtx.PriorityPause {
continue
if b.ctx.ExecutionPriority != botCtx.PriorityPause {
*b.ctx.Data = b.ctx.GameReader.GetData()
b.ctx.LastRefreshTime = time.Now()
}
b.ctx.RefreshGameData()
}
}
})
Expand Down
16 changes: 13 additions & 3 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Context struct {
LastBuffAt time.Time
ContextDebug map[Priority]*Debug
CurrentGame *CurrentGameHelper
RefreshRequest chan struct{}
LastRefreshTime time.Time
}

type Debug struct {
Expand Down Expand Up @@ -82,7 +84,9 @@ func NewContext(name string) *Status {
PriorityPause: {},
PriorityStop: {},
},
CurrentGame: NewGameHelper(),
CurrentGame: NewGameHelper(),
RefreshRequest: make(chan struct{}, 1),
LastRefreshTime: time.Now(),
}
botContexts[getGoroutineID()] = &Status{Priority: PriorityNormal, Context: ctx}

Expand Down Expand Up @@ -122,9 +126,15 @@ func getGoroutineID() uint64 {
}

func (ctx *Context) RefreshGameData() {
*ctx.Data = ctx.GameReader.GetData()
if time.Since(ctx.LastRefreshTime) >= 20*time.Millisecond {
*ctx.Data = ctx.GameReader.GetData()
ctx.LastRefreshTime = time.Now()
select {
case ctx.RefreshRequest <- struct{}{}: // Notify background goroutine to reset timer
default:
}
}
}

func (ctx *Context) Detach() {
mu.Lock()
defer mu.Unlock()
Expand Down