Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 0 additions & 17 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ type Consumer struct {
logger Logger
stopOnce sync.Once
stopFlag int32
metric Metric
}

func (s *Consumer) incBusyWorker() {
s.metric.IncBusyWorker()
}

func (s *Consumer) decBusyWorker() {
s.metric.DecBusyWorker()
}

// BusyWorkers returns the numbers of workers has been busy.
func (s *Consumer) BusyWorkers() uint64 {
return s.metric.BusyWorkers()
}

func (s *Consumer) handle(job Job) error {
Expand All @@ -43,10 +29,8 @@ func (s *Consumer) handle(job Job) error {
panicChan := make(chan interface{}, 1)
startTime := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), job.Timeout)
s.incBusyWorker()
defer func() {
cancel()
s.decBusyWorker()
}()

// run the job
Expand Down Expand Up @@ -157,7 +141,6 @@ func NewConsumer(opts ...Option) *Consumer {
stop: make(chan struct{}),
logger: o.logger,
runFunc: o.fn,
metric: o.metric,
}

return w
Expand Down
27 changes: 0 additions & 27 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,30 +321,3 @@ func TestTaskJobComplete(t *testing.T) {
}
assert.Equal(t, context.DeadlineExceeded, w.handle(job))
}

func TestBusyWorkerCount(t *testing.T) {
job := Job{
Timeout: 200 * time.Millisecond,
Task: func(ctx context.Context) error {
time.Sleep(100 * time.Millisecond)
return nil
},
}

w := NewConsumer()

assert.Equal(t, uint64(0), w.BusyWorkers())
go func() {
assert.NoError(t, w.handle(job))
}()
go func() {
assert.NoError(t, w.handle(job))
}()

time.Sleep(50 * time.Millisecond)
assert.Equal(t, uint64(2), w.BusyWorkers())
time.Sleep(100 * time.Millisecond)
assert.Equal(t, uint64(0), w.BusyWorkers())

assert.NoError(t, w.Shutdown())
}