lease_queue.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package lease
  15. type LeaseWithTime struct {
  16. leaseId LeaseID
  17. expiration int64
  18. index int
  19. }
  20. type LeaseQueue []*LeaseWithTime
  21. func (pq LeaseQueue) Len() int { return len(pq) }
  22. func (pq LeaseQueue) Less(i, j int) bool {
  23. return pq[i].expiration < pq[j].expiration
  24. }
  25. func (pq LeaseQueue) Swap(i, j int) {
  26. pq[i], pq[j] = pq[j], pq[i]
  27. pq[i].index = i
  28. pq[j].index = j
  29. }
  30. func (pq *LeaseQueue) Push(x interface{}) {
  31. n := len(*pq)
  32. item := x.(*LeaseWithTime)
  33. item.index = n
  34. *pq = append(*pq, item)
  35. }
  36. func (pq *LeaseQueue) Pop() interface{} {
  37. old := *pq
  38. n := len(old)
  39. item := old[n-1]
  40. item.index = -1 // for safety
  41. *pq = old[0 : n-1]
  42. return item
  43. }