summaryrefslogtreecommitdiffstats
path: root/modules/sync
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-10-12 07:18:26 +0200
committerGitHub <noreply@github.com>2022-10-12 13:18:26 +0800
commit0e57ff7eee4ac71d923f970d15889ad4d50f97a9 (patch)
treee5a92c55af5366924bd40ae14b4bf12842239193 /modules/sync
parente84558b0931309cf1f4f2767bc47296483b9b3e1 (diff)
downloadgitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.tar.gz
gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.zip
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/sync')
-rw-r--r--modules/sync/status_pool.go21
1 files changed, 10 insertions, 11 deletions
diff --git a/modules/sync/status_pool.go b/modules/sync/status_pool.go
index acbd93ab17..99e5ce9cb3 100644
--- a/modules/sync/status_pool.go
+++ b/modules/sync/status_pool.go
@@ -6,6 +6,8 @@ package sync
import (
"sync"
+
+ "code.gitea.io/gitea/modules/container"
)
// StatusTable is a table maintains true/false values.
@@ -14,13 +16,13 @@ import (
// in different goroutines.
type StatusTable struct {
lock sync.RWMutex
- pool map[string]struct{}
+ pool container.Set[string]
}
// NewStatusTable initializes and returns a new StatusTable object.
func NewStatusTable() *StatusTable {
return &StatusTable{
- pool: make(map[string]struct{}),
+ pool: make(container.Set[string]),
}
}
@@ -28,32 +30,29 @@ func NewStatusTable() *StatusTable {
// Returns whether set value was set to true
func (p *StatusTable) StartIfNotRunning(name string) bool {
p.lock.Lock()
- _, ok := p.pool[name]
- if !ok {
- p.pool[name] = struct{}{}
- }
+ added := p.pool.Add(name)
p.lock.Unlock()
- return !ok
+ return added
}
// Start sets value of given name to true in the pool.
func (p *StatusTable) Start(name string) {
p.lock.Lock()
- p.pool[name] = struct{}{}
+ p.pool.Add(name)
p.lock.Unlock()
}
// Stop sets value of given name to false in the pool.
func (p *StatusTable) Stop(name string) {
p.lock.Lock()
- delete(p.pool, name)
+ p.pool.Remove(name)
p.lock.Unlock()
}
// IsRunning checks if value of given name is set to true in the pool.
func (p *StatusTable) IsRunning(name string) bool {
p.lock.RLock()
- _, ok := p.pool[name]
+ exists := p.pool.Contains(name)
p.lock.RUnlock()
- return ok
+ return exists
}