unbounded.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. // Package buffer provides an implementation of an unbounded buffer.
  18. package buffer
  19. import (
  20. "errors"
  21. "sync"
  22. )
  23. // Unbounded is an implementation of an unbounded buffer which does not use
  24. // extra goroutines. This is typically used for passing updates from one entity
  25. // to another within gRPC.
  26. //
  27. // All methods on this type are thread-safe and don't block on anything except
  28. // the underlying mutex used for synchronization.
  29. //
  30. // Unbounded supports values of any type to be stored in it by using a channel
  31. // of `any`. This means that a call to Put() incurs an extra memory allocation,
  32. // and also that users need a type assertion while reading. For performance
  33. // critical code paths, using Unbounded is strongly discouraged and defining a
  34. // new type specific implementation of this buffer is preferred. See
  35. // internal/transport/transport.go for an example of this.
  36. type Unbounded struct {
  37. c chan any
  38. closed bool
  39. closing bool
  40. mu sync.Mutex
  41. backlog []any
  42. }
  43. // NewUnbounded returns a new instance of Unbounded.
  44. func NewUnbounded() *Unbounded {
  45. return &Unbounded{c: make(chan any, 1)}
  46. }
  47. var errBufferClosed = errors.New("Put called on closed buffer.Unbounded")
  48. // Put adds t to the unbounded buffer.
  49. func (b *Unbounded) Put(t any) error {
  50. b.mu.Lock()
  51. defer b.mu.Unlock()
  52. if b.closing {
  53. return errBufferClosed
  54. }
  55. if len(b.backlog) == 0 {
  56. select {
  57. case b.c <- t:
  58. return nil
  59. default:
  60. }
  61. }
  62. b.backlog = append(b.backlog, t)
  63. return nil
  64. }
  65. // Load sends the earliest buffered data, if any, onto the read channel returned
  66. // by Get(). Users are expected to call this every time they successfully read a
  67. // value from the read channel.
  68. func (b *Unbounded) Load() {
  69. b.mu.Lock()
  70. defer b.mu.Unlock()
  71. if len(b.backlog) > 0 {
  72. select {
  73. case b.c <- b.backlog[0]:
  74. b.backlog[0] = nil
  75. b.backlog = b.backlog[1:]
  76. default:
  77. }
  78. } else if b.closing && !b.closed {
  79. close(b.c)
  80. }
  81. }
  82. // Get returns a read channel on which values added to the buffer, via Put(),
  83. // are sent on.
  84. //
  85. // Upon reading a value from this channel, users are expected to call Load() to
  86. // send the next buffered value onto the channel if there is any.
  87. //
  88. // If the unbounded buffer is closed, the read channel returned by this method
  89. // is closed after all data is drained.
  90. func (b *Unbounded) Get() <-chan any {
  91. return b.c
  92. }
  93. // Close closes the unbounded buffer. No subsequent data may be Put(), and the
  94. // channel returned from Get() will be closed after all the data is read and
  95. // Load() is called for the final time.
  96. func (b *Unbounded) Close() {
  97. b.mu.Lock()
  98. defer b.mu.Unlock()
  99. if b.closing {
  100. return
  101. }
  102. b.closing = true
  103. if len(b.backlog) == 0 {
  104. b.closed = true
  105. close(b.c)
  106. }
  107. }