RWMutex does not help here and could hurt

Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
Derek Collison
2023-04-05 20:26:45 -07:00
parent d14968cb4f
commit b6ebf0fe43
2 changed files with 8 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 The NATS Authors
// Copyright 2021-2023 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
@@ -23,7 +23,7 @@ const ipQueueDefaultMaxRecycleSize = 4 * 1024
// This is a generic intra-process queue.
type ipQueue[T any] struct {
inprogress int64
sync.RWMutex
sync.Mutex
ch chan struct{}
elts []T
pos int
@@ -180,9 +180,9 @@ func (q *ipQueue[T]) recycle(elts *[]T) {
// Returns the current length of the queue.
func (q *ipQueue[T]) len() int {
q.RLock()
q.Lock()
l := len(q.elts) - q.pos
q.RUnlock()
q.Unlock()
return l
}

View File

@@ -221,9 +221,9 @@ func TestIPQueuePopOne(t *testing.T) {
q.push(1)
q.push(2)
// Capture current capacity
q.RLock()
q.Lock()
c := cap(q.elts)
q.RUnlock()
q.Unlock()
e, ok = q.popOne()
if !ok || e != 1 {
t.Fatalf("Invalid value: %v", e)
@@ -343,9 +343,9 @@ func TestIPQueueRecycle(t *testing.T) {
values = q.pop()
q.recycle(&values)
q.push(1002)
q.RLock()
q.Lock()
recycled := &q.elts == &values
q.RUnlock()
q.Unlock()
if recycled {
t.Fatalf("Unexpected recycled slice")
}