You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. package p2p
  2. import (
  3. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  4. )
  5. // queue does QoS scheduling for Envelopes, enqueueing and dequeueing according
  6. // to some policy. Queues are used at contention points, i.e.:
  7. //
  8. // - Receiving inbound messages to a single channel from all peers.
  9. // - Sending outbound messages to a single peer from all channels.
  10. type queue interface {
  11. // enqueue returns a channel for submitting envelopes.
  12. enqueue() chan<- Envelope
  13. // dequeue returns a channel ordered according to some queueing policy.
  14. dequeue() <-chan Envelope
  15. // close closes the queue. After this call enqueue() will block, so the
  16. // caller must select on closed() as well to avoid blocking forever. The
  17. // enqueue() and dequeue() channels will not be closed.
  18. close()
  19. // closed returns a channel that's closed when the scheduler is closed.
  20. closed() <-chan struct{}
  21. }
  22. // fifoQueue is a simple unbuffered lossless queue that passes messages through
  23. // in the order they were received, and blocks until message is received.
  24. type fifoQueue struct {
  25. queueCh chan Envelope
  26. closer *tmsync.Closer
  27. }
  28. func newFIFOQueue(size int) queue {
  29. return &fifoQueue{
  30. queueCh: make(chan Envelope, size),
  31. closer: tmsync.NewCloser(),
  32. }
  33. }
  34. func (q *fifoQueue) enqueue() chan<- Envelope {
  35. return q.queueCh
  36. }
  37. func (q *fifoQueue) dequeue() <-chan Envelope {
  38. return q.queueCh
  39. }
  40. func (q *fifoQueue) close() {
  41. q.closer.Close()
  42. }
  43. func (q *fifoQueue) closed() <-chan struct{} {
  44. return q.closer.Done()
  45. }