1 Star 0 Fork 0

jack/protoactor-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
priority_queue.go 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
490689386@qq.com 提交于 2025-05-19 14:50 +08:00 . 初始化
package actor
// A priority queue is a sort of meta-queue that uses a queue per priority level.
// The underlying queues can be anything that implements the queue interface.
//
// Messages that implement the PriorityMessage interface (i.e. have a GetPriority
// method) will be consumed in priority order first, queue order second. So if a
// higher priority message arrives, it will jump to the front of the queue from
// the consumer's perspective.
//
// There are 8 priority levels (0-7) because having too many levels impacts
// performance. And 8 priority levels ought to be enough for anybody. ;)
// This means your GetPriority method should return int8s between 0 and 7. If any
// return values are higher or lower, they will be reset to 7 or 0, respectively.
//
// The default priority level is 4 for messages that don't implement PriorityMessage.
// If you want your message processed sooner than un-prioritized messages, have its
// GetPriority method return a larger int8 value.
// Likewise, if you'd like to de-prioritize your message, have its GetPriority method
// return an int8 less than 4.
const (
priorityLevels = 8
DefaultPriority = int8(priorityLevels / 2)
)
type PriorityMessage interface {
GetPriority() int8
}
type priorityQueue struct {
priorityQueues []queue
}
func NewPriorityQueue(queueProducer func() queue) *priorityQueue {
q := &priorityQueue{
priorityQueues: make([]queue, priorityLevels),
}
for p := 0; p < priorityLevels; p++ {
q.priorityQueues[p] = queueProducer()
}
return q
}
func (q *priorityQueue) Push(item interface{}) {
itemPriority := DefaultPriority
if priorityItem, ok := item.(PriorityMessage); ok {
itemPriority = priorityItem.GetPriority()
if itemPriority < 0 {
itemPriority = 0
}
if itemPriority > priorityLevels-1 {
itemPriority = priorityLevels - 1
}
}
q.priorityQueues[itemPriority].Push(item)
}
func (q *priorityQueue) Pop() interface{} {
for p := priorityLevels - 1; p >= 0; p-- {
if item := q.priorityQueues[p].Pop(); item != nil {
return item
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wujianhai/protoactor-go.git
git@gitee.com:wujianhai/protoactor-go.git
wujianhai
protoactor-go
protoactor-go
5633fe2499dd

搜索帮助