Skip to content

Commit 1da7dd3

Browse files
minecrafterlunny
authored andcommitted
Improve status table implementation (#879)
* Remove superfluous defer calls * Improve status table implementation as well This would probably only help with large, high-traffic installs
1 parent 1397334 commit 1da7dd3

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

modules/sync/status_pool.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,34 @@ import (
1414
// in different goroutines.
1515
type StatusTable struct {
1616
lock sync.RWMutex
17-
pool map[string]bool
17+
pool map[string]struct{}
1818
}
1919

2020
// NewStatusTable initializes and returns a new StatusTable object.
2121
func NewStatusTable() *StatusTable {
2222
return &StatusTable{
23-
pool: make(map[string]bool),
23+
pool: make(map[string]struct{}),
2424
}
2525
}
2626

2727
// Start sets value of given name to true in the pool.
2828
func (p *StatusTable) Start(name string) {
2929
p.lock.Lock()
30-
defer p.lock.Unlock()
31-
32-
p.pool[name] = true
30+
p.pool[name] = struct{}{}
31+
p.lock.Unlock()
3332
}
3433

3534
// Stop sets value of given name to false in the pool.
3635
func (p *StatusTable) Stop(name string) {
3736
p.lock.Lock()
38-
defer p.lock.Unlock()
39-
40-
p.pool[name] = false
37+
delete(p.pool, name)
38+
p.lock.Unlock()
4139
}
4240

4341
// IsRunning checks if value of given name is set to true in the pool.
4442
func (p *StatusTable) IsRunning(name string) bool {
4543
p.lock.RLock()
46-
defer p.lock.RUnlock()
47-
48-
return p.pool[name]
44+
_, ok := p.pool[name]
45+
p.lock.RUnlock()
46+
return ok
4947
}

modules/sync/status_pool_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sync
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_StatusTable(t *testing.T) {
10+
table := NewStatusTable()
11+
12+
assert.False(t, table.IsRunning("xyz"))
13+
14+
table.Start("xyz")
15+
assert.True(t, table.IsRunning("xyz"))
16+
17+
table.Stop("xyz")
18+
assert.False(t, table.IsRunning("xyz"))
19+
}

modules/sync/unique_queue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (q *UniqueQueue) AddFunc(id interface{}, fn func()) {
5151

5252
idStr := com.ToStr(id)
5353
q.table.lock.Lock()
54-
q.table.pool[idStr] = true
54+
q.table.pool[idStr] = struct{}{}
5555
if fn != nil {
5656
fn()
5757
}

0 commit comments

Comments
 (0)