sony / sonyflake

A distributed unique ID generator inspired by Twitter's Snowflake

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multi-coroutine, id generation repeated

wg2019 opened this issue · comments

func TestNewID(t *testing.T) {
	total := 100000
	m := &sync.Map{}
	s := sonyflake.NewSonyflake(sonyflake.Settings{StartTime: time.Now()})
	queue := make(chan int, 10)
	for i := 0; i < 1000; i++ {
		go makeID(s, queue, m)
	}
	for i := 0; i < total; i++ {
		queue <- i
	}
	close(queue)
	var num int64 = 0
	m.Range(func(key, value interface{}) bool {
		atomic.AddInt64(&num, 1)
		return true
	})
	t.Logf("a: %d, b: %d", total, num)
}

func makeID(s *sonyflake.Sonyflake,queue <- chan int, m *sync.Map){
	for  {
		select {
		case i := <- queue:
			id, _ := s.NextID()
			m.Store(id,i)
		}
	}
}

result:a: 100000, b: 99065