Skip to content
Open
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
27 changes: 22 additions & 5 deletions dq/consumernode.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package dq

import (
"sync/atomic"
"time"

"github.com/beanstalkd/go-beanstalk"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/core/threading"
)

type (
consumerNode struct {
conn *connection
tube string
on *syncx.AtomicBool
conn *connection
tube string
on *syncx.AtomicBool
processingNum int64
}

consumeService struct {
Expand Down Expand Up @@ -53,8 +56,14 @@ func (c *consumerNode) consumeEvents(consume Consume) {
conn.TubeSet.Name[c.tube] = true
id, body, err := conn.Reserve(reserveTimeout)
if err == nil {
conn.Delete(id)
consume(body)
if err := conn.Delete(id); err != nil {
logx.Error(err)
}
threading.GoSafe(func() {
atomic.AddInt64(&c.processingNum, 1)
defer atomic.AddInt64(&c.processingNum, -1)
consume(body)
})
continue
}

Expand Down Expand Up @@ -91,4 +100,12 @@ func (cs consumeService) Start() {

func (cs consumeService) Stop() {
cs.c.dispose()
for {
if atomic.LoadInt64(&cs.c.processingNum) == 0 {
// wait all service consumer func process complete
break
}
// wait 100 millisecond check again
time.Sleep(time.Millisecond * 100)
}
}