-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(media): integrate TTL cleanup into FileMediaStore #728
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
Merged
alexhoshina
merged 7 commits into
sipeed:refactor/channel-system
from
ex-takashima:feat/media-cleanup-ttl
Feb 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e4d3650
feat(media): integrate TTL cleanup into FileMediaStore
ex-takashima 9a16f3b
fix(media): address review comments on TTL cleanup
ex-takashima 8e20644
fix(media): guard Interval<=0 panic, two-phase ReleaseAll
ex-takashima ad736d7
feat(line): add StartTyping and PlaceholderRecorder integration
ex-takashima af97540
Revert "feat(line): add StartTyping and PlaceholderRecorder integration"
ex-takashima 13a8ae4
fix(media): use project logger and harden map cleanup
ex-takashima 8979683
fix(media): separate import groups for gci linter
ex-takashima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ package media | |
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| ) | ||
|
|
@@ -35,8 +37,16 @@ type MediaStore interface { | |
|
|
||
| // mediaEntry holds the path and metadata for a stored media file. | ||
| type mediaEntry struct { | ||
| path string | ||
| meta MediaMeta | ||
| path string | ||
| meta MediaMeta | ||
| storedAt time.Time | ||
| } | ||
|
|
||
| // MediaCleanerConfig configures the background TTL cleanup. | ||
| type MediaCleanerConfig struct { | ||
| Enabled bool | ||
| MaxAge time.Duration | ||
| Interval time.Duration | ||
| } | ||
|
|
||
| // FileMediaStore is a pure in-memory implementation of MediaStore. | ||
|
|
@@ -45,13 +55,33 @@ type FileMediaStore struct { | |
| mu sync.RWMutex | ||
| refs map[string]mediaEntry | ||
| scopeToRefs map[string]map[string]struct{} | ||
| refToScope map[string]string | ||
|
|
||
| cleanerCfg MediaCleanerConfig | ||
| stop chan struct{} | ||
| once sync.Once | ||
| nowFunc func() time.Time // for testing | ||
| } | ||
|
|
||
| // NewFileMediaStore creates a new FileMediaStore. | ||
| // NewFileMediaStore creates a new FileMediaStore without background cleanup. | ||
| func NewFileMediaStore() *FileMediaStore { | ||
| return &FileMediaStore{ | ||
| refs: make(map[string]mediaEntry), | ||
| scopeToRefs: make(map[string]map[string]struct{}), | ||
| refToScope: make(map[string]string), | ||
| nowFunc: time.Now, | ||
| } | ||
| } | ||
|
|
||
| // NewFileMediaStoreWithCleanup creates a FileMediaStore with TTL-based background cleanup. | ||
| func NewFileMediaStoreWithCleanup(cfg MediaCleanerConfig) *FileMediaStore { | ||
| return &FileMediaStore{ | ||
| refs: make(map[string]mediaEntry), | ||
| scopeToRefs: make(map[string]map[string]struct{}), | ||
| refToScope: make(map[string]string), | ||
| cleanerCfg: cfg, | ||
| stop: make(chan struct{}), | ||
| nowFunc: time.Now, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -66,11 +96,12 @@ func (s *FileMediaStore) Store(localPath string, meta MediaMeta, scope string) ( | |
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
|
|
||
| s.refs[ref] = mediaEntry{path: localPath, meta: meta} | ||
| s.refs[ref] = mediaEntry{path: localPath, meta: meta, storedAt: s.nowFunc()} | ||
| if s.scopeToRefs[scope] == nil { | ||
| s.scopeToRefs[scope] = make(map[string]struct{}) | ||
| } | ||
| s.scopeToRefs[scope][ref] = struct{}{} | ||
| s.refToScope[ref] = scope | ||
|
|
||
| return ref, nil | ||
| } | ||
|
|
@@ -115,9 +146,79 @@ func (s *FileMediaStore) ReleaseAll(scope string) error { | |
| // Log but continue — best effort cleanup | ||
| } | ||
| delete(s.refs, ref) | ||
| delete(s.refToScope, ref) | ||
| } | ||
| } | ||
|
|
||
| delete(s.scopeToRefs, scope) | ||
| return nil | ||
| } | ||
|
|
||
| // CleanExpired removes all entries older than MaxAge. | ||
| // Both the file on disk and the in-memory references are deleted atomically | ||
| // under the same mutex, preventing dangling references. | ||
| func (s *FileMediaStore) CleanExpired() int { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
|
|
||
| cutoff := s.nowFunc().Add(-s.cleanerCfg.MaxAge) | ||
| removed := 0 | ||
|
|
||
| for ref, entry := range s.refs { | ||
| if entry.storedAt.Before(cutoff) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and should cutoff >= 0 ? |
||
| if err := os.Remove(entry.path); err != nil && !os.IsNotExist(err) { | ||
alexhoshina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Log but continue — best effort cleanup | ||
| } | ||
|
|
||
| scope := s.refToScope[ref] | ||
| if scopeRefs, ok := s.scopeToRefs[scope]; ok { | ||
| delete(scopeRefs, ref) | ||
| if len(scopeRefs) == 0 { | ||
| delete(s.scopeToRefs, scope) | ||
| } | ||
| } | ||
|
|
||
| delete(s.refs, ref) | ||
| delete(s.refToScope, ref) | ||
| removed++ | ||
| } | ||
| } | ||
|
|
||
| return removed | ||
| } | ||
|
|
||
| // Start begins the background cleanup goroutine if cleanup is enabled. | ||
| func (s *FileMediaStore) Start() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Start function need match the Stop function also using |
||
| if !s.cleanerCfg.Enabled || s.stop == nil { | ||
| return | ||
| } | ||
|
|
||
| log.Printf("[media] cleanup enabled: interval=%s, max_age=%s", | ||
| s.cleanerCfg.Interval, s.cleanerCfg.MaxAge) | ||
|
|
||
| go func() { | ||
| ticker := time.NewTicker(s.cleanerCfg.Interval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-ticker.C: | ||
| if n := s.CleanExpired(); n > 0 { | ||
| log.Printf("[media] cleanup: removed %d expired entries", n) | ||
| } | ||
| case <-s.stop: | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| // Stop terminates the background cleanup goroutine. | ||
| func (s *FileMediaStore) Stop() { | ||
| if s.stop == nil { | ||
| return | ||
| } | ||
| s.once.Do(func() { | ||
| close(s.stop) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there err path not close the goroutine?