lease_queue.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // LeaseWithTime contains lease object with expire information.
  16. type LeaseWithTime struct {
  17. id LeaseID
  18. expiration int64
  19. index int
  20. }
  21. type LeaseQueue []*LeaseWithTime
  22. func (pq LeaseQueue) Len() int { return len(pq) }
  23. func (pq LeaseQueue) Less(i, j int) bool {
  24. return pq[i].expiration < pq[j].expiration
  25. }
  26. func (pq LeaseQueue) Swap(i, j int) {
  27. pq[i], pq[j] = pq[j], pq[i]
  28. pq[i].index = i
  29. pq[j].index = j
  30. }
  31. func (pq *LeaseQueue) Push(x interface{}) {
  32. n := len(*pq)
  33. item := x.(*LeaseWithTime)
  34. item.index = n
  35. *pq = append(*pq, item)
  36. }
  37. func (pq *LeaseQueue) Pop() interface{} {
  38. old := *pq
  39. n := len(old)
  40. item := old[n-1]
  41. item.index = -1 // for safety
  42. *pq = old[0 : n-1]
  43. return item
  44. }