Skip to content

Commit

Permalink
Fix BufferedChannelQueue.PutWithTimeout() functionalities.
Browse files Browse the repository at this point in the history
# Conflicts:
#	queue.go
  • Loading branch information
johnteee committed Oct 28, 2024
1 parent 9f1f2ca commit 12b604b
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (q *LinkedListQueue) KeepNodePoolCount(n int) {
last.Next = nil
}

// CLear Clear all data
// Clear Clear all data
func (q *LinkedListQueue) Clear() {
q.nodePoolFirst = q.first
q.nodeCount = q.count
Expand Down Expand Up @@ -694,17 +694,35 @@ func (q *BufferedChannelQueue) Put(val interface{}) error {
return q.Offer(val)
}

// // PutWithTimeout Put the val(blocking), with timeout
// func (q BufferedChannelQueue) PutWithTimeout(val interface{}, timeout time.Duration) error {
// // q.lock.Lock()
// // defer q.lock.Unlock()
//
// if q.isClosed.Get() {
// return ErrQueueIsClosed
// }
//
// return q.blockingQueue.PutWithTimeout(val, timeout)
// }
// PutWithTimeout Put the T val(blocking), with timeout
func (q *BufferedChannelQueue) PutWithTimeout(val interface{}, timeout time.Duration) error {
// q.lock.Lock()
// defer q.lock.Unlock()

if q.isClosed.Get() {
return ErrQueueIsClosed
}

deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
//fmt.Println("iteration")
q.loadWorkerCh.Offer(1)

err := q.Offer(val)
if err == nil {
return nil
}
if errors.Is(err, ErrQueueIsFull) {
return err
}
q.loadWorkerCh.Offer(1)
time.Sleep(q.loadFromPoolDuration)
}

//return q.blockingQueue.PutWithTimeout(val, timeout)

return ErrQueuePutTimeout
}

// Take Take the val(blocking)
func (q *BufferedChannelQueue) Take() (interface{}, error) {
Expand Down

0 comments on commit 12b604b

Please sign in to comment.