Files
prysm/container/leaky-bucket/heap.go
Preston Van Loon 2aa52fb56a Add static analyzer to discourage use of panic() (#15075)
* Implement static analysis to prevent panics

* Add nopanic to nogo

* Fix violations and add exclusions

Fix violations and add exclusions for all

* Changelog fragment

* Use pass.Report instead of pass.Reportf

* Remove strings.ToLower for checking init method name

* Add exclusion for herumi init

* Move api/client/beacon template function to init and its own file

* Fix nopanic testcase
2025-03-19 18:04:15 +00:00

49 lines
981 B
Go

package leakybucket
import "fmt"
// Based on the example implementation of priority queue found in the
// container/heap package docs: https://golang.org/pkg/container/heap/
type priorityQueue []*LeakyBucket
func (pq priorityQueue) Len() int {
return len(pq)
}
func (pq priorityQueue) Peak() *LeakyBucket {
if len(pq) <= 0 {
return nil
}
return pq[0]
}
func (pq priorityQueue) Less(i, j int) bool {
return pq[i].p.Before(pq[j].p)
}
func (pq priorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
// Push a LeakyBucket to priorityQueue
func (pq *priorityQueue) Push(x interface{}) {
n := len(*pq)
b, ok := x.(*LeakyBucket)
if !ok {
panic(fmt.Sprintf("%T", x)) // lint:nopanic -- This method should be improved. High risk for misuse!
}
b.index = n
*pq = append(*pq, b)
}
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
b := old[n-1]
b.index = -1 // for safety
*pq = old[0 : n-1]
return b
}