github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/rootd/dns/client_queue_linux.go (about)

     1  package dns
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/miekg/dns"
     7  )
     8  
     9  // Most of this was aped off the docs at https://pkg.go.dev/container/heap@go1.17.8
    10  
    11  type waitingClient struct {
    12  	returnCh    chan *dns.Conn
    13  	doneCh      <-chan struct{}
    14  	arrivalTime time.Time
    15  	index       int // The index of the item in the heap.
    16  }
    17  
    18  // A clientQueue implements heap.Interface and holds waitingClients.
    19  type clientQueue []*waitingClient
    20  
    21  func (pq clientQueue) Len() int { return len(pq) }
    22  
    23  func (pq clientQueue) Less(i, j int) bool {
    24  	return pq[i].arrivalTime.Before(pq[j].arrivalTime)
    25  }
    26  
    27  func (pq clientQueue) Swap(i, j int) {
    28  	pq[i], pq[j] = pq[j], pq[i]
    29  	pq[i].index = i
    30  	pq[j].index = j
    31  }
    32  
    33  func (pq *clientQueue) Push(x any) {
    34  	n := len(*pq)
    35  	item := x.(*waitingClient)
    36  	item.index = n
    37  	*pq = append(*pq, item)
    38  }
    39  
    40  func (pq *clientQueue) Pop() any {
    41  	old := *pq
    42  	n := len(old)
    43  	item := old[n-1]
    44  	old[n-1] = nil  // avoid memory leak
    45  	item.index = -1 // for safety
    46  	*pq = old[0 : n-1]
    47  	return item
    48  }