[golang] bootstrapped a client in yet another language
This commit is contained in:
parent
5aac233c08
commit
427cc77fa3
12 changed files with 447 additions and 0 deletions
golang/pkg/api
44
golang/pkg/api/priority_queue.go
Normal file
44
golang/pkg/api/priority_queue.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package api
|
||||
|
||||
type Request struct {
|
||||
index int
|
||||
priority int
|
||||
|
||||
method string
|
||||
path string
|
||||
payload any
|
||||
resp chan *Response
|
||||
}
|
||||
|
||||
type PriorityQueue []*Request
|
||||
|
||||
func (pq PriorityQueue) Len() int {
|
||||
return len(pq)
|
||||
}
|
||||
|
||||
func (pq PriorityQueue) Less(i, j int) bool {
|
||||
return pq[i].priority < pq[j].priority
|
||||
}
|
||||
|
||||
func (pq PriorityQueue) Swap(i, j int) {
|
||||
pq[i], pq[j] = pq[j], pq[i]
|
||||
pq[i].index = i
|
||||
pq[j].index = j
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Push(x any) {
|
||||
n := len(*pq)
|
||||
item := x.(*Request)
|
||||
item.index = n
|
||||
*pq = append(*pq, item)
|
||||
}
|
||||
|
||||
func (pq *PriorityQueue) Pop() any {
|
||||
old := *pq
|
||||
n := len(old)
|
||||
item := old[n-1]
|
||||
old[n-1] = nil // avoid memory leak
|
||||
item.index = -1 // for safety
|
||||
*pq = old[0 : n-1]
|
||||
return item
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue