Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fifo queue and map utilising RWMutex #229

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions ocppj/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (q *FIFOClientQueue) Push(element interface{}) error {
}

func (q *FIFOClientQueue) Peek() interface{} {
q.mutex.Lock()
defer q.mutex.Unlock()
q.mutex.RLock()
defer q.mutex.RUnlock()
if len(q.elements) == 0 {
return nil
}
Expand All @@ -78,20 +78,20 @@ func (q *FIFOClientQueue) Pop() interface{} {
}

func (q *FIFOClientQueue) Size() int {
q.mutex.Lock()
defer q.mutex.Unlock()
q.mutex.RLock()
defer q.mutex.RUnlock()
return len(q.elements)
}

func (q *FIFOClientQueue) IsFull() bool {
q.mutex.Lock()
defer q.mutex.Unlock()
q.mutex.RLock()
defer q.mutex.RUnlock()
return len(q.elements) >= q.capacity && q.capacity > 0
}

func (q *FIFOClientQueue) IsEmpty() bool {
q.mutex.Lock()
defer q.mutex.Unlock()
q.mutex.RLock()
defer q.mutex.RUnlock()
return len(q.elements) == 0
}

Expand Down Expand Up @@ -145,18 +145,16 @@ func (f *FIFOQueueMap) Init() {
}

func (f *FIFOQueueMap) Get(clientID string) (RequestQueue, bool) {
f.mutex.Lock()
defer f.mutex.Unlock()
f.mutex.RLock()
defer f.mutex.RUnlock()
q, ok := f.data[clientID]
return q, ok
}

func (f *FIFOQueueMap) GetOrCreate(clientID string) RequestQueue {
f.mutex.Lock()
defer f.mutex.Unlock()
var q RequestQueue
var ok bool
q, ok = f.data[clientID]
q, ok := f.data[clientID]
if !ok {
q = NewFIFOClientQueue(f.queueCapacity)
f.data[clientID] = q
Expand Down