fix: make local queue PopItem can be notified (#39011)

This commit is contained in:
wxiaoguang
2026-08-22 01:30:41 +00:00
committed by GitHub
parent 5eba4f92ce
commit d2bc0097bc
11 changed files with 145 additions and 88 deletions
+17 -12
View File
@@ -25,35 +25,40 @@ type baseLevelQueuePushPoper interface {
}
type baseLevelQueueCommonImpl struct {
*baseQueueNotifiable
length int
internalFunc func() baseLevelQueuePushPoper
mu *sync.Mutex
muCommon *sync.Mutex
}
func (q *baseLevelQueueCommonImpl) PushItem(ctx context.Context, data []byte) error {
return backoffErr(ctx, backoffBegin, backoffUpper, time.After(pushBlockTime), func() (retry bool, err error) {
if q.mu != nil {
q.mu.Lock()
defer q.mu.Unlock()
_, err := backoffCall(ctx, backoffOptionsDefault(noNotifyChan, time.After(pushBlockTime)), func() (retry bool, ret any, err error) {
if q.muCommon != nil {
q.muCommon.Lock()
defer q.muCommon.Unlock()
}
cnt := int(q.internalFunc().Len())
if cnt >= q.length {
return true, nil
return true, nil, nil
}
retry, err = false, q.internalFunc().RPush(data)
if err == levelqueue.ErrAlreadyInQueue {
err = ErrAlreadyInQueue
}
return retry, err
if err == nil {
q.notifyPushItem()
}
return retry, nil, err
})
return err
}
func (q *baseLevelQueueCommonImpl) PopItem(ctx context.Context) ([]byte, error) {
return backoffRetErr(ctx, backoffBegin, backoffUpper, infiniteTimerC, func() (retry bool, data []byte, err error) {
if q.mu != nil {
q.mu.Lock()
defer q.mu.Unlock()
return backoffCall(ctx, backoffOptionsDefault(q.notifySignal, infiniteTimerC), func() (retry bool, data []byte, err error) {
if q.muCommon != nil {
q.muCommon.Lock()
defer q.muCommon.Unlock()
}
data, err = q.internalFunc().LPop()
@@ -68,7 +73,7 @@ func (q *baseLevelQueueCommonImpl) PopItem(ctx context.Context) ([]byte, error)
}
func baseLevelQueueCommon(cfg *BaseConfig, mu *sync.Mutex, internalFunc func() baseLevelQueuePushPoper) *baseLevelQueueCommonImpl {
return &baseLevelQueueCommonImpl{length: cfg.Length, mu: mu, internalFunc: internalFunc}
return &baseLevelQueueCommonImpl{length: cfg.Length, muCommon: mu, internalFunc: internalFunc, baseQueueNotifiable: newBaseQueueNotifiable()}
}
func prepareLevelDB(cfg *BaseConfig) (conn string, db *leveldb.DB, err error) {