From f2257a643cc5ba6a6d6d1517567766436083d148 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Mon, 27 Apr 2015 19:56:48 -0700 Subject: [PATCH] Critical sections are now protected by lock --- pkg/api/quota/quota_handler.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/api/quota/quota_handler.go b/pkg/api/quota/quota_handler.go index d4ecfc0b9..7f4e69ad6 100644 --- a/pkg/api/quota/quota_handler.go +++ b/pkg/api/quota/quota_handler.go @@ -33,6 +33,8 @@ type quotaMap struct { } func (q *quotaMap) CanExpire() { + q.Lock() + defer q.Unlock() currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds() // divide by segmentSize, otherwise expiredQuotas will always be negative expiredQuotas := currentMinute - (q.duration.Nanoseconds() / q.segmentSize.Nanoseconds()) @@ -44,9 +46,9 @@ func (q *quotaMap) CanExpire() { } func (q *quotaMap) Add(ip uint32, size int64) { + q.CanExpire() q.Lock() defer q.Unlock() - q.CanExpire() currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds() if _, ok := q.data[currentMinute]; !ok { q.data[currentMinute] = make(map[uint32]int64) @@ -65,10 +67,9 @@ func (q *quotaMap) IsQuotaMet(ip uint32) bool { } func (q *quotaMap) GetQuotaUsed(ip uint32) (total int64) { - currentMinute := time.Now().UnixNano() / q.segmentSize.Nanoseconds() - if _, ok := q.data[currentMinute]; !ok { - q.data[currentMinute] = make(map[uint32]int64) - } + q.CanExpire() + q.RLock() + defer q.RUnlock() for _, segment := range q.data { if used, ok := segment[ip]; ok { total += used