添加互斥锁以保护URLSelector结构体的并发访问

This commit is contained in:
wood chen 2024-10-13 18:17:52 +08:00
parent c6abb02d2f
commit c4bb234c2c

View File

@ -34,6 +34,7 @@ type URLSelector struct {
URLs []string URLs []string
CurrentIndex int CurrentIndex int
RecentUsed map[string]int RecentUsed map[string]int
mu sync.Mutex
} }
func NewURLSelector(urls []string) *URLSelector { func NewURLSelector(urls []string) *URLSelector {
@ -52,6 +53,9 @@ func (us *URLSelector) ShuffleURLs() {
} }
func (us *URLSelector) GetRandomURL() string { func (us *URLSelector) GetRandomURL() string {
us.mu.Lock()
defer us.mu.Unlock()
if us.CurrentIndex == 0 { if us.CurrentIndex == 0 {
us.ShuffleURLs() us.ShuffleURLs()
} }