Skip to content

Commit

Permalink
feat(consumer): add default timeout in Request new task (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Apr 1, 2022
1 parent d1a0d31 commit 1a06aab
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,24 @@ func (s *Consumer) Queue(task QueuedMessage) error {

// Request a new task from channel
func (s *Consumer) Request() (QueuedMessage, error) {
select {
case task, ok := <-s.taskQueue:
if !ok {
return nil, ErrQueueHasBeenClosed
clock := 0
loop:
for {
select {
case task, ok := <-s.taskQueue:
if !ok {
return nil, ErrQueueHasBeenClosed
}
return task, nil
case <-time.After(1 * time.Second):
if clock == 5 {
break loop
}
clock += 1
}
return task, nil
default:
return nil, ErrNoTaskInQueue
}

return nil, ErrNoTaskInQueue
}

// NewConsumer for create new consumer instance
Expand Down

0 comments on commit 1a06aab

Please sign in to comment.