From 0e57ff7eee4ac71d923f970d15889ad4d50f97a9 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Wed, 12 Oct 2022 07:18:26 +0200 Subject: Add generic set type (#21408) This PR adds a generic set type to get rid of maps used as sets. Co-authored-by: wxiaoguang --- modules/sync/status_pool.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'modules/sync') 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 } -- cgit v1.2.3