deque
deque copied to clipboard
Returns zero and loses messages
Hey Andrew,
Can you tell me what I'm doing wrong?
From time to time the PopFront
method returns nil
, and some messages get lost?
Thank you!
package somepackage
import (
"sync"
"testing"
"time"
"github.com/gammazero/deque"
"github.com/google/uuid"
"github.com/jpillora/backoff"
)
func Test(t *testing.T) {
dq := deque.New[*string]()
count := 15000
m := make([]*string, 0, count)
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
for i := 0; i < count; i++ {
if i%3000 == 0 {
b := &backoff.Backoff{
Factor: 2,
Min: time.Second * 2,
Max: time.Second * 10,
}
time.Sleep(b.Duration())
}
s := uuid.New().String()
dq.PushBack(&s)
}
wg.Done()
}()
go func() {
for {
if len(m) == count {
break
}
if dq.Len() == 0 {
continue
}
s := dq.PopFront()
if s == nil {
} // sometime it's true
m = append(m, s)
}
wg.Done()
}()
wg.Wait()
}
UPD: I started working with sync.Mutex
and the problem went away.
Thank you for the follow-up. For maximum speed, this deque implementation leaves concurrency safety up to the application to provide, in whatever way the application chooses, if needed at all.