deque icon indicating copy to clipboard operation
deque copied to clipboard

Returns zero and loses messages

Open voscob opened this issue 2 years ago • 1 comments

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()
}

voscob avatar Jul 01 '22 13:07 voscob

UPD: I started working with sync.Mutex and the problem went away.

voscob avatar Jul 04 '22 11:07 voscob

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.

gammazero avatar Aug 11 '22 06:08 gammazero