grafana/pkg/services/authn/authnimpl/priority_queue.go
Karl Persson 95ea4bad6f
AuthN: Rebuild Authenticate so we only have to call it once in context handler (#61705)
* API: Add reqSignedIn to router groups

* AuthN: Add fall through in context handler

* AuthN: Add IsAnonymous field

* AuthN: add priority to context aware clients

* ContextHandler: Add comment

* AuthN: Add a simple priority queue

* AuthN: Add Name to client interface

* AuthN: register clients with function

* AuthN: update mock and fake to implement interface

* AuthN: rewrite test without reflection

* AuthN: add comment

* AuthN: fix queue insert

* AuthN: rewrite tests

* AuthN: make the queue generic so we can reuse it for hooks

* ContextHandler: Add fixme for auth headers

* AuthN: remove unused variable

* AuthN: use multierror

* AuthN: write proper tests for queue

* AuthN: Add queue item that can store the value and priority

Co-authored-by: Jo <joao.guerreiro@grafana.com>
2023-01-26 10:50:44 +01:00

35 lines
758 B
Go

package authnimpl
func newQueue[T any]() *queue[T] {
return &queue[T]{items: []queueItem[T]{}}
}
type queue[T any] struct {
items []queueItem[T]
}
type queueItem[T any] struct {
v T
p uint
}
func (q *queue[T]) insert(v T, p uint) {
// no items in the queue so we just add it
if len(q.items) == 0 {
q.items = append(q.items, queueItem[T]{v, p})
return
}
// find the position in the queue the item should be placed based on priority
for i, item := range q.items {
if p < item.p {
q.items = append(q.items[:i+1], q.items[i:]...)
q.items[i] = queueItem[T]{v, p}
return
}
}
// item did not have higher priority then what is in the queue currently, so we need to add it to the end
q.items = append(q.items, queueItem[T]{v, p})
}