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

Fixed race condition #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (q *Queue) UpdateObjectAsJSON(id uint64, newValue interface{}) (*Item, erro

// Length returns the total number of items in the queue.
func (q *Queue) Length() uint64 {
q.RLock()
defer q.RUnlock()
return q.tail - q.head
}

Expand Down Expand Up @@ -295,7 +297,7 @@ func (q *Queue) Drop() error {
// getItemByID returns an item, if found, for the given ID.
func (q *Queue) getItemByID(id uint64) (*Item, error) {
// Check if empty or out of bounds.
if q.Length() == 0 {
if q.tail-q.head == 0 {
return nil, ErrEmpty
} else if id <= q.head || id > q.tail {
return nil, ErrOutOfBounds
Expand Down
4 changes: 3 additions & 1 deletion stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (s *Stack) UpdateObjectAsJSON(id uint64, newValue interface{}) (*Item, erro

// Length returns the total number of items in the stack.
func (s *Stack) Length() uint64 {
s.RLock()
defer s.RUnlock()
return s.head - s.tail
}

Expand Down Expand Up @@ -295,7 +297,7 @@ func (s *Stack) Drop() error {
// getItemByID returns an item, if found, for the given ID.
func (s *Stack) getItemByID(id uint64) (*Item, error) {
// Check if empty or out of bounds.
if s.Length() == 0 {
if s.head-s.tail == 0 {
return nil, ErrEmpty
} else if id <= s.tail || id > s.head {
return nil, ErrOutOfBounds
Expand Down