unbounded.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "sync"
  20. // Unbounded is an implementation of an unbounded buffer which does not use
  21. // extra goroutines. This is typically used for passing updates from one entity
  22. // to another within gRPC.
  23. //
  24. // All methods on this type are thread-safe and don't block on anything except
  25. // the underlying mutex used for synchronization.
  26. //
  27. // Unbounded supports values of any type to be stored in it by using a channel
  28. // of `interface{}`. This means that a call to Put() incurs an extra memory
  29. // allocation, and also that users need a type assertion while reading. For
  30. // performance critical code paths, using Unbounded is strongly discouraged and
  31. // defining a new type specific implementation of this buffer is preferred. See
  32. // internal/transport/transport.go for an example of this.
  33. type Unbounded struct {
  34. c chan interface{}
  35. closed bool
  36. mu sync.Mutex
  37. backlog []interface{}
  38. }
  39. // NewUnbounded returns a new instance of Unbounded.
  40. func NewUnbounded() *Unbounded {
  41. return &Unbounded{c: make(chan interface{}, 1)}
  42. }
  43. // Put adds t to the unbounded buffer.
  44. func (b *Unbounded) Put(t interface{}) {
  45. b.mu.Lock()
  46. defer b.mu.Unlock()
  47. if b.closed {
  48. return
  49. }
  50. if len(b.backlog) == 0 {
  51. select {
  52. case b.c <- t:
  53. return
  54. default:
  55. }
  56. }
  57. b.backlog = append(b.backlog, t)
  58. }
  59. // Load sends the earliest buffered data, if any, onto the read channel
  60. // returned by Get(). Users are expected to call this every time they read a
  61. // value from the read channel.
  62. func (b *Unbounded) Load() {
  63. b.mu.Lock()
  64. defer b.mu.Unlock()
  65. if b.closed {
  66. return
  67. }
  68. if len(b.backlog) > 0 {
  69. select {
  70. case b.c <- b.backlog[0]:
  71. b.backlog[0] = nil
  72. b.backlog = b.backlog[1:]
  73. default:
  74. }
  75. }
  76. }
  77. // Get returns a read channel on which values added to the buffer, via Put(),
  78. // are sent on.
  79. //
  80. // Upon reading a value from this channel, users are expected to call Load() to
  81. // send the next buffered value onto the channel if there is any.
  82. //
  83. // If the unbounded buffer is closed, the read channel returned by this method
  84. // is closed.
  85. func (b *Unbounded) Get() <-chan interface{} {
  86. return b.c
  87. }
  88. // Close closes the unbounded buffer.
  89. func (b *Unbounded) Close() {
  90. b.mu.Lock()
  91. defer b.mu.Unlock()
  92. if b.closed {
  93. return
  94. }
  95. b.closed = true
  96. close(b.c)
  97. }