mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
添加互斥锁以保护URLSelector结构体的并发访问
This commit is contained in:
parent
c6abb02d2f
commit
c4bb234c2c
4
main.go
4
main.go
@ -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()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user