@@ -6,23 +6,26 @@ import (
66 "sync"
77)
88
9- // fifo represent as FIFO scheduler.
10- type fifo struct {
11- mu sync.Mutex
12- resume chan struct {}
13- scheduled int
14- finished int
15- pendings []Job
16- ctx context.Context
17- cancel context.CancelFunc
9+ // Verify interface compliance
10+ var _ Scheduler = (* Fifo )(nil )
11+
12+ // Fifo represent as FIFO scheduler.
13+ type Fifo struct {
14+ mu sync.Mutex
15+ resume chan struct {}
16+ scheduled int
17+ finished int
18+ pendings []Job
19+ ctx context.Context
20+ cancel context.CancelFunc
1821 finishCond * sync.Cond
1922 donec chan struct {}
2023}
2124
2225// NewFIFOScheduler returns a Scheduler that schedules jobs in FIFO
2326// order sequentially
24- func NewFIFOScheduler () Scheduler {
25- f := & fifo {
27+ func NewFIFOScheduler () * Fifo {
28+ f := & Fifo {
2629 resume : make (chan struct {}, 1 ),
2730 donec : make (chan struct {}, 1 ),
2831 }
@@ -32,8 +35,8 @@ func NewFIFOScheduler() Scheduler {
3235 return f
3336}
3437
35- // Schedule schedules a job that will be ran in FIFO order sequentially.
36- func (f * fifo ) Schedule (j Job ) error {
38+ // Schedule schedules a job that will be run in FIFO order sequentially.
39+ func (f * Fifo ) Schedule (j Job ) error {
3740 f .mu .Lock ()
3841 defer f .mu .Unlock ()
3942
@@ -51,25 +54,25 @@ func (f *fifo) Schedule(j Job) error {
5154 return nil
5255}
5356
54- func (f * fifo ) Pending () int {
57+ func (f * Fifo ) Pending () int {
5558 f .mu .Lock ()
5659 defer f .mu .Unlock ()
5760 return len (f .pendings )
5861}
5962
60- func (f * fifo ) Scheduled () int {
63+ func (f * Fifo ) Scheduled () int {
6164 f .mu .Lock ()
6265 defer f .mu .Unlock ()
6366 return f .scheduled
6467}
6568
66- func (f * fifo ) Finished () int {
69+ func (f * Fifo ) Finished () int {
6770 f .finishCond .L .Lock ()
6871 defer f .finishCond .L .Unlock ()
6972 return f .finished
7073}
7174
72- func (f * fifo ) WaitFinish (n int ) {
75+ func (f * Fifo ) WaitFinish (n int ) {
7376 f .finishCond .L .Lock ()
7477 for f .finished < n || len (f .pendings ) != 0 {
7578 f .finishCond .Wait ()
@@ -78,15 +81,15 @@ func (f *fifo) WaitFinish(n int) {
7881}
7982
8083// Stop stops the scheduler and cancels all pending jobs.
81- func (f * fifo ) Stop () {
84+ func (f * Fifo ) Stop () {
8285 f .mu .Lock ()
8386 f .cancel ()
8487 f .cancel = nil
8588 f .mu .Unlock ()
8689 <- f .donec
8790}
8891
89- func (f * fifo ) run () {
92+ func (f * Fifo ) run () {
9093 // TODO: recover from job panic?
9194 defer func () {
9295 close (f .donec )
@@ -125,4 +128,4 @@ func (f *fifo) run() {
125128 f .finishCond .L .Unlock ()
126129 }
127130 }
128- }
131+ }
0 commit comments