bgreader.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Package bgreader provides a io.Reader that can optionally buffer reads in the background.
  2. package bgreader
  3. import (
  4. "io"
  5. "sync"
  6. "github.com/jackc/pgx/v5/internal/iobufpool"
  7. )
  8. const (
  9. StatusStopped = iota
  10. StatusRunning
  11. StatusStopping
  12. )
  13. // BGReader is an io.Reader that can optionally buffer reads in the background. It is safe for concurrent use.
  14. type BGReader struct {
  15. r io.Reader
  16. cond *sync.Cond
  17. status int32
  18. readResults []readResult
  19. }
  20. type readResult struct {
  21. buf *[]byte
  22. err error
  23. }
  24. // Start starts the backgrounder reader. If the background reader is already running this is a no-op. The background
  25. // reader will stop automatically when the underlying reader returns an error.
  26. func (r *BGReader) Start() {
  27. r.cond.L.Lock()
  28. defer r.cond.L.Unlock()
  29. switch r.status {
  30. case StatusStopped:
  31. r.status = StatusRunning
  32. go r.bgRead()
  33. case StatusRunning:
  34. // no-op
  35. case StatusStopping:
  36. r.status = StatusRunning
  37. }
  38. }
  39. // Stop tells the background reader to stop after the in progress Read returns. It is safe to call Stop when the
  40. // background reader is not running.
  41. func (r *BGReader) Stop() {
  42. r.cond.L.Lock()
  43. defer r.cond.L.Unlock()
  44. switch r.status {
  45. case StatusStopped:
  46. // no-op
  47. case StatusRunning:
  48. r.status = StatusStopping
  49. case StatusStopping:
  50. // no-op
  51. }
  52. }
  53. // Status returns the current status of the background reader.
  54. func (r *BGReader) Status() int32 {
  55. r.cond.L.Lock()
  56. defer r.cond.L.Unlock()
  57. return r.status
  58. }
  59. func (r *BGReader) bgRead() {
  60. keepReading := true
  61. for keepReading {
  62. buf := iobufpool.Get(8192)
  63. n, err := r.r.Read(*buf)
  64. *buf = (*buf)[:n]
  65. r.cond.L.Lock()
  66. r.readResults = append(r.readResults, readResult{buf: buf, err: err})
  67. if r.status == StatusStopping || err != nil {
  68. r.status = StatusStopped
  69. keepReading = false
  70. }
  71. r.cond.L.Unlock()
  72. r.cond.Broadcast()
  73. }
  74. }
  75. // Read implements the io.Reader interface.
  76. func (r *BGReader) Read(p []byte) (int, error) {
  77. r.cond.L.Lock()
  78. defer r.cond.L.Unlock()
  79. if len(r.readResults) > 0 {
  80. return r.readFromReadResults(p)
  81. }
  82. // There are no unread background read results and the background reader is stopped.
  83. if r.status == StatusStopped {
  84. return r.r.Read(p)
  85. }
  86. // Wait for results from the background reader
  87. for len(r.readResults) == 0 {
  88. r.cond.Wait()
  89. }
  90. return r.readFromReadResults(p)
  91. }
  92. // readBackgroundResults reads a result previously read by the background reader. r.cond.L must be held.
  93. func (r *BGReader) readFromReadResults(p []byte) (int, error) {
  94. buf := r.readResults[0].buf
  95. var err error
  96. n := copy(p, *buf)
  97. if n == len(*buf) {
  98. err = r.readResults[0].err
  99. iobufpool.Put(buf)
  100. if len(r.readResults) == 1 {
  101. r.readResults = nil
  102. } else {
  103. r.readResults = r.readResults[1:]
  104. }
  105. } else {
  106. *buf = (*buf)[n:]
  107. r.readResults[0].buf = buf
  108. }
  109. return n, err
  110. }
  111. func New(r io.Reader) *BGReader {
  112. return &BGReader{
  113. r: r,
  114. cond: &sync.Cond{
  115. L: &sync.Mutex{},
  116. },
  117. }
  118. }