From 285d7eb4aa512dd8ea666a15a2cd68be7cd14a60 Mon Sep 17 00:00:00 2001 From: stefanodvx <69367859+stefanodvx@users.noreply.github.com> Date: Wed, 16 Apr 2025 11:55:01 +0200 Subject: [PATCH] inline: clean inactive tasks for memory usage --- bot/core/inline.go | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/bot/core/inline.go b/bot/core/inline.go index 6f090f7..6a65c1a 100644 --- a/bot/core/inline.go +++ b/bot/core/inline.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "sync" + "time" "github.com/google/uuid" "github.com/pkg/errors" @@ -18,24 +19,67 @@ import ( "github.com/PaulSonOfLars/gotgbot/v2/ext" ) +type TaskEntry struct { + Task *models.DownloadContext + CreatedAt time.Time +} + var InlineTasks sync.Map +var cleanupActive sync.Once + +const taskTimeout = 5 * time.Minute func GetTask(id string) (*models.DownloadContext, bool) { value, ok := InlineTasks.Load(id) if !ok { return nil, false } - return value.(*models.DownloadContext), true + entry, ok := value.(TaskEntry) + if !ok { + return nil, false + } + return entry.Task, true } func SetTask(id string, task *models.DownloadContext) { - InlineTasks.Store(id, task) + InlineTasks.Store(id, TaskEntry{ + Task: task, + CreatedAt: time.Now(), + }) + cleanupActive.Do(startTasksCleanup) } func DeleteTask(id string) { InlineTasks.Delete(id) } +func startTasksCleanup() { + go func() { + ticker := time.NewTicker(1 * time.Minute) + defer ticker.Stop() + + for range ticker.C { + cleanupStaleTasks() + } + }() +} + +func cleanupStaleTasks() { + now := time.Now() + InlineTasks.Range(func(key, value interface{}) bool { + entry, ok := value.(TaskEntry) + if !ok { + InlineTasks.Delete(key) + return true + } + + if now.Sub(entry.CreatedAt) > taskTimeout { + InlineTasks.Delete(key) + } + return true + }) +} + func HandleInline( bot *gotgbot.Bot, ctx *ext.Context,