pipe.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package http2
  5. import (
  6. "errors"
  7. "io"
  8. "sync"
  9. )
  10. // pipe is a goroutine-safe io.Reader/io.Writer pair. It's like
  11. // io.Pipe except there are no PipeReader/PipeWriter halves, and the
  12. // underlying buffer is an interface. (io.Pipe is always unbuffered)
  13. type pipe struct {
  14. mu sync.Mutex
  15. c sync.Cond // c.L lazily initialized to &p.mu
  16. b pipeBuffer // nil when done reading
  17. unread int // bytes unread when done
  18. err error // read error once empty. non-nil means closed.
  19. breakErr error // immediate read error (caller doesn't see rest of b)
  20. donec chan struct{} // closed on error
  21. readFn func() // optional code to run in Read before error
  22. }
  23. type pipeBuffer interface {
  24. Len() int
  25. io.Writer
  26. io.Reader
  27. }
  28. func (p *pipe) Len() int {
  29. p.mu.Lock()
  30. defer p.mu.Unlock()
  31. if p.b == nil {
  32. return p.unread
  33. }
  34. return p.b.Len()
  35. }
  36. // Read waits until data is available and copies bytes
  37. // from the buffer into p.
  38. func (p *pipe) Read(d []byte) (n int, err error) {
  39. p.mu.Lock()
  40. defer p.mu.Unlock()
  41. if p.c.L == nil {
  42. p.c.L = &p.mu
  43. }
  44. for {
  45. if p.breakErr != nil {
  46. return 0, p.breakErr
  47. }
  48. if p.b != nil && p.b.Len() > 0 {
  49. return p.b.Read(d)
  50. }
  51. if p.err != nil {
  52. if p.readFn != nil {
  53. p.readFn() // e.g. copy trailers
  54. p.readFn = nil // not sticky like p.err
  55. }
  56. p.b = nil
  57. return 0, p.err
  58. }
  59. p.c.Wait()
  60. }
  61. }
  62. var errClosedPipeWrite = errors.New("write on closed buffer")
  63. // Write copies bytes from p into the buffer and wakes a reader.
  64. // It is an error to write more data than the buffer can hold.
  65. func (p *pipe) Write(d []byte) (n int, err error) {
  66. p.mu.Lock()
  67. defer p.mu.Unlock()
  68. if p.c.L == nil {
  69. p.c.L = &p.mu
  70. }
  71. defer p.c.Signal()
  72. if p.err != nil {
  73. return 0, errClosedPipeWrite
  74. }
  75. if p.breakErr != nil {
  76. p.unread += len(d)
  77. return len(d), nil // discard when there is no reader
  78. }
  79. return p.b.Write(d)
  80. }
  81. // CloseWithError causes the next Read (waking up a current blocked
  82. // Read if needed) to return the provided err after all data has been
  83. // read.
  84. //
  85. // The error must be non-nil.
  86. func (p *pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) }
  87. // BreakWithError causes the next Read (waking up a current blocked
  88. // Read if needed) to return the provided err immediately, without
  89. // waiting for unread data.
  90. func (p *pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) }
  91. // closeWithErrorAndCode is like CloseWithError but also sets some code to run
  92. // in the caller's goroutine before returning the error.
  93. func (p *pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) }
  94. func (p *pipe) closeWithError(dst *error, err error, fn func()) {
  95. if err == nil {
  96. panic("err must be non-nil")
  97. }
  98. p.mu.Lock()
  99. defer p.mu.Unlock()
  100. if p.c.L == nil {
  101. p.c.L = &p.mu
  102. }
  103. defer p.c.Signal()
  104. if *dst != nil {
  105. // Already been done.
  106. return
  107. }
  108. p.readFn = fn
  109. if dst == &p.breakErr {
  110. if p.b != nil {
  111. p.unread += p.b.Len()
  112. }
  113. p.b = nil
  114. }
  115. *dst = err
  116. p.closeDoneLocked()
  117. }
  118. // requires p.mu be held.
  119. func (p *pipe) closeDoneLocked() {
  120. if p.donec == nil {
  121. return
  122. }
  123. // Close if unclosed. This isn't racy since we always
  124. // hold p.mu while closing.
  125. select {
  126. case <-p.donec:
  127. default:
  128. close(p.donec)
  129. }
  130. }
  131. // Err returns the error (if any) first set by BreakWithError or CloseWithError.
  132. func (p *pipe) Err() error {
  133. p.mu.Lock()
  134. defer p.mu.Unlock()
  135. if p.breakErr != nil {
  136. return p.breakErr
  137. }
  138. return p.err
  139. }
  140. // Done returns a channel which is closed if and when this pipe is closed
  141. // with CloseWithError.
  142. func (p *pipe) Done() <-chan struct{} {
  143. p.mu.Lock()
  144. defer p.mu.Unlock()
  145. if p.donec == nil {
  146. p.donec = make(chan struct{})
  147. if p.err != nil || p.breakErr != nil {
  148. // Already hit an error.
  149. p.closeDoneLocked()
  150. }
  151. }
  152. return p.donec
  153. }