queue.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package mux
  2. import (
  3. "container/list"
  4. "github.com/cnlh/nps/lib/common"
  5. "sync"
  6. )
  7. type Queue struct {
  8. list *list.List
  9. readOp chan struct{}
  10. cleanOp chan struct{}
  11. popWait bool
  12. mutex sync.Mutex
  13. }
  14. func (Self *Queue) New() {
  15. Self.list = list.New()
  16. Self.readOp = make(chan struct{})
  17. Self.cleanOp = make(chan struct{}, 2)
  18. }
  19. func (Self *Queue) Push(packager *common.MuxPackager) {
  20. Self.mutex.Lock()
  21. if Self.popWait {
  22. defer Self.allowPop()
  23. }
  24. if packager.Flag == common.MUX_CONN_CLOSE {
  25. Self.insert(packager) // the close package may need priority,
  26. // prevent wait too long to close
  27. } else {
  28. Self.list.PushBack(packager)
  29. }
  30. Self.mutex.Unlock()
  31. return
  32. }
  33. func (Self *Queue) allowPop() (closed bool) {
  34. Self.mutex.Lock()
  35. Self.popWait = false
  36. Self.mutex.Unlock()
  37. select {
  38. case Self.readOp <- struct{}{}:
  39. return false
  40. case <-Self.cleanOp:
  41. return true
  42. }
  43. }
  44. func (Self *Queue) insert(packager *common.MuxPackager) {
  45. element := Self.list.Back()
  46. for {
  47. if element == nil { // Queue dose not have any of msg package with this close package id
  48. Self.list.PushFront(packager) // insert close package to first
  49. break
  50. }
  51. if element.Value.(*common.MuxPackager).Flag == common.MUX_NEW_MSG &&
  52. element.Value.(*common.MuxPackager).Id == packager.Id {
  53. Self.list.InsertAfter(packager, element) // Queue has some msg package
  54. // with this close package id, insert close package after last msg package
  55. break
  56. }
  57. element = element.Prev()
  58. }
  59. }
  60. func (Self *Queue) Pop() (packager *common.MuxPackager) {
  61. Self.mutex.Lock()
  62. element := Self.list.Front()
  63. if element != nil {
  64. packager = element.Value.(*common.MuxPackager)
  65. Self.list.Remove(element)
  66. Self.mutex.Unlock()
  67. return
  68. }
  69. Self.popWait = true // Queue is empty, notice Push method
  70. Self.mutex.Unlock()
  71. select {
  72. case <-Self.readOp:
  73. return Self.Pop()
  74. case <-Self.cleanOp:
  75. return nil
  76. }
  77. }
  78. func (Self *Queue) Len() (n int) {
  79. n = Self.list.Len()
  80. return
  81. }
  82. func (Self *Queue) Clean() {
  83. Self.cleanOp <- struct{}{}
  84. Self.cleanOp <- struct{}{}
  85. close(Self.cleanOp)
  86. }