-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
302 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package queues | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type multipleQueue struct { | ||
options *options // 参数 | ||
serial int64 // 序列号 | ||
size int64 // 队列大小 | ||
stopped atomic.Uint32 // 是否关闭 | ||
qs []*singleQueue // 子队列 | ||
} | ||
|
||
// 创建多重队列 | ||
func newMultipleQueue(o *options) *multipleQueue { | ||
qs := make([]*singleQueue, o.size) | ||
for i := int64(0); i < o.size; i++ { | ||
qs[i] = newSingleQueue(o) | ||
} | ||
return &multipleQueue{options: o, qs: qs, size: o.size} | ||
} | ||
|
||
// Push 追加任务 | ||
func (c *multipleQueue) Push(job Job) { | ||
if c.stopped.Load() == 0 { | ||
index := atomic.AddInt64(&c.serial, 1) & (c.size - 1) | ||
c.qs[index].Push(job) | ||
} | ||
} | ||
|
||
// Stop 停止 | ||
// 可能需要等待一段时间, 直到所有任务执行完成或者超时 | ||
func (c *multipleQueue) Stop() error { | ||
ctx, cancel := context.WithTimeout(context.Background(), c.options.timeout) | ||
ticker := time.NewTicker(50 * time.Millisecond) | ||
defer func() { | ||
cancel() | ||
ticker.Stop() | ||
}() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
if c.doStop(false) { | ||
return nil | ||
} | ||
case <-ctx.Done(): | ||
c.doStop(true) | ||
return ErrStopTimeout | ||
} | ||
} | ||
} | ||
|
||
func (c *multipleQueue) doStop(force bool) bool { | ||
if force { | ||
c.stopped.Store(1) | ||
return true | ||
} | ||
|
||
sum := 0 | ||
for _, item := range c.qs { | ||
sum += item.len() | ||
} | ||
if sum == 0 { | ||
c.stopped.Store(1) | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.