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

[ISSUE #754] close msgCh when pq droped #859

Merged
merged 1 commit into from
Jul 25, 2022
Merged
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
20 changes: 18 additions & 2 deletions consumer/process_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type processQueue struct {
lockConsume sync.Mutex
msgCh chan []*primitive.MessageExt
order bool
closeChanOnce *sync.Once
closeChan chan struct{}
}

func newProcessQueue(order bool) *processQueue {
Expand All @@ -80,6 +82,8 @@ func newProcessQueue(order bool) *processQueue {
msgCh: make(chan []*primitive.MessageExt, 32),
consumingMsgOrderlyTreeMap: consumingMsgOrderlyTreeMap,
order: order,
closeChanOnce: &sync.Once{},
closeChan: make(chan struct{}),
locked: uatomic.NewBool(false),
dropped: uatomic.NewBool(false),
}
Expand All @@ -96,7 +100,11 @@ func (pq *processQueue) putMessage(messages ...*primitive.MessageExt) {
return
}
if !pq.order {
pq.msgCh <- messages
select {
case <-pq.closeChan:
return
case pq.msgCh <- messages:
}
}
validMessageCount := 0
for idx := range messages {
Expand Down Expand Up @@ -142,6 +150,9 @@ func (pq *processQueue) IsLock() bool {

func (pq *processQueue) WithDropped(dropped bool) {
pq.dropped.Store(dropped)
pq.closeChanOnce.Do(func() {
close(pq.closeChan)
})
}

func (pq *processQueue) IsDroppd() bool {
Expand Down Expand Up @@ -274,7 +285,12 @@ func (pq *processQueue) getMaxSpan() int {
}

func (pq *processQueue) getMessages() []*primitive.MessageExt {
return <-pq.msgCh
select {
case <-pq.closeChan:
return nil
case mq := <-pq.msgCh:
return mq
}
}

func (pq *processQueue) takeMessages(number int) []*primitive.MessageExt {
Expand Down