Skip to content

Commit 808ad9a

Browse files
committed
增加查询等待中的任务数量的方法
1 parent 5b5166a commit 808ad9a

7 files changed

Lines changed: 40 additions & 10 deletions

File tree

FUTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* [ ] 支持任务数量优先调度策略
99
* [ ] 支持任务时间优先调度策略
1010
* [ ] 支持 worker 动态扩缩容
11-
* [x] 支持查询当前的 worker 数量
11+
* [x] 支持查询可用的 worker 数量
1212
* [x] 支持设置 worker 任务队列大小
1313
* [x] 支持设置 panic 处理函数
1414
* [x] 支持自定义 sync.Locker 实现

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Limits the number of simultaneous goroutines and reuses them by Executor.
1616
* Supports multiple scheduling strategies, including round robin, random, etc.
1717
* Supports spin lock with backoff strategy.
18-
* Supports getting the number of workers in the executor.
18+
* Supports getting the number of workers available in the executor.
1919

2020
_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._
2121

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* 支持限制同时执行的协程数,且复用协程,使用 Executor。
1616
* 支持多种调度策略,包括轮询、随机等。
1717
* 支持使用退避策略的自旋锁。
18-
* 支持查询当前的 worker 数量。
18+
* 支持查询可用的 worker 数量。
1919

2020
_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_
2121

executor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func NewExecutor(workerNum int, opts ...Option) *Executor {
6262
return executor
6363
}
6464

65-
// WorkerNum returns the number of workers in the executor.
66-
func (e *Executor) WorkerNum() int {
65+
// AvailableWorkers returns the number of workers available in the executor.
66+
func (e *Executor) AvailableWorkers() int {
6767
e.lock.Lock()
6868
defer e.lock.Unlock()
6969

@@ -102,6 +102,6 @@ func (e *Executor) Close() {
102102
worker.Done()
103103
}
104104

105-
e.wg.Wait()
105+
e.Wait()
106106
e.closed = true
107107
}

executor_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ func TestExecutorError(t *testing.T) {
6868
}
6969
}
7070

71-
// go test -v -cover -run=^TestExecutorWorkerNum$
72-
func TestExecutorWorkerNum(t *testing.T) {
71+
// go test -v -cover -run=^TestExecutorAvailableWorkers$
72+
func TestExecutorAvailableWorkers(t *testing.T) {
7373
workerNum := 16
7474
workers := make([]*worker, workerNum)
7575

7676
executor := NewExecutor(workerNum)
7777
executor.workers = workers
7878

79-
if executor.WorkerNum() != workerNum {
80-
t.Fatalf("executor.WorkerNum() %d != workerNum %d", executor.WorkerNum(), workerNum)
79+
if executor.AvailableWorkers() != workerNum {
80+
t.Fatalf("executor.AvailableWorkers() %d != workerNum %d", executor.AvailableWorkers(), workerNum)
8181
}
8282
}

worker.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func newWorker(executor *Executor) *worker {
2727
return w
2828
}
2929

30+
// WaitingTasks returns the number of tasks waiting in the worker.
31+
func (w *worker) WaitingTasks() int {
32+
return len(w.taskQueue)
33+
}
34+
3035
func (w *worker) handle(task Task) {
3136
defer func() {
3237
if r := recover(); r != nil {

worker_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,28 @@ func TestWorkerHandle(t *testing.T) {
3737
t.Fatalf("got %d != want %d", got, want)
3838
}
3939
}
40+
41+
// go test -v -cover -run=^TestWorkerWaitingTasks$
42+
func TestWorkerWaitingTasks(t *testing.T) {
43+
taskQueue := make(chan Task, 4)
44+
worker := &worker{taskQueue: taskQueue}
45+
46+
if worker.WaitingTasks() != len(taskQueue) {
47+
t.Fatalf("got %d != want %d", worker.WaitingTasks(), len(taskQueue))
48+
}
49+
50+
if worker.WaitingTasks() != 0 {
51+
t.Fatalf("got %d != 0", worker.WaitingTasks())
52+
}
53+
54+
taskQueue <- nil
55+
taskQueue <- nil
56+
57+
if worker.WaitingTasks() != len(taskQueue) {
58+
t.Fatalf("got %d != want %d", worker.WaitingTasks(), len(taskQueue))
59+
}
60+
61+
if worker.WaitingTasks() != 2 {
62+
t.Fatalf("got %d != 2", worker.WaitingTasks())
63+
}
64+
}

0 commit comments

Comments
 (0)