Skip to content

Commit

Permalink
Fix: memory auth data race (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier authored May 2, 2021
1 parent 72cf352 commit 6c907b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
26 changes: 18 additions & 8 deletions statistic/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ type User struct {
recv uint64
lastSent uint64
lastRecv uint64
speedLock sync.Mutex
speedLock sync.RWMutex
sendSpeed uint64
recvSpeed uint64
hash string
ipTableLock sync.Mutex
ipTableLock sync.RWMutex
ipTable map[string]struct{}
maxIPNum int
limiterLock sync.RWMutex
sendLimiter *rate.Limiter
recvLimiter *rate.Limiter
ctx context.Context
Expand Down Expand Up @@ -72,8 +73,8 @@ func (u *User) DelIP(ip string) bool {
}

func (u *User) GetIP() int {
u.ipTableLock.Lock()
defer u.ipTableLock.Unlock()
u.ipTableLock.RLock()
defer u.ipTableLock.RUnlock()
return len(u.ipTable)
}

Expand All @@ -86,16 +87,22 @@ func (u *User) GetIPLimit() int {
}

func (u *User) AddTraffic(sent, recv int) {
if u.sendLimiter != nil && sent != 0 {
u.limiterLock.Lock()
defer u.limiterLock.Unlock()

if u.sendLimiter != nil && sent >= 0 {
u.sendLimiter.WaitN(u.ctx, sent)
} else if u.recvLimiter != nil && recv != 0 {
} else if u.recvLimiter != nil && recv >= 0 {
u.recvLimiter.WaitN(u.ctx, recv)
}
atomic.AddUint64(&u.sent, uint64(sent))
atomic.AddUint64(&u.recv, uint64(recv))
}

func (u *User) SetSpeedLimit(send, recv int) {
u.limiterLock.Lock()
defer u.limiterLock.Unlock()

if send <= 0 {
u.sendLimiter = nil
} else {
Expand All @@ -109,6 +116,9 @@ func (u *User) SetSpeedLimit(send, recv int) {
}

func (u *User) GetSpeedLimit() (send, recv int) {
u.limiterLock.RLock()
defer u.limiterLock.RUnlock()

sendLimit := 0
recvLimit := 0
if u.sendLimiter != nil {
Expand Down Expand Up @@ -159,8 +169,8 @@ func (u *User) speedUpdater() {
}

func (u *User) GetSpeed() (uint64, uint64) {
u.speedLock.Lock()
defer u.speedLock.Unlock()
u.speedLock.RLock()
defer u.speedLock.RUnlock()
return u.sendSpeed, u.recvSpeed
}

Expand Down
6 changes: 2 additions & 4 deletions statistic/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ func TestMemoryAuth(t *testing.T) {
go func() {
for {
k := 100
select {
case <-time.After(time.Second / time.Duration(k)):
user.AddTraffic(200/k, 100/k)
}
time.Sleep(time.Second / time.Duration(k))
user.AddTraffic(200/k, 100/k)
}
}()
time.Sleep(time.Second * 4)
Expand Down

0 comments on commit 6c907b7

Please sign in to comment.