unbounded.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. mu sync.Mutex
  36. backlog []interface{}
  37. }
  38. // NewUnbounded returns a new instance of Unbounded.
  39. func NewUnbounded() *Unbounded {
  40. return &Unbounded{c: make(chan interface{}, 1)}
  41. }
  42. // Put adds t to the unbounded buffer.
  43. func (b *Unbounded) Put(t interface{}) {
  44. b.mu.Lock()
  45. if len(b.backlog) == 0 {
  46. select {
  47. case b.c <- t:
  48. b.mu.Unlock()
  49. return
  50. default:
  51. }
  52. }
  53. b.backlog = append(b.backlog, t)
  54. b.mu.Unlock()
  55. }
  56. // Load sends the earliest buffered data, if any, onto the read channel
  57. // returned by Get(). Users are expected to call this every time they read a
  58. // value from the read channel.
  59. func (b *Unbounded) Load() {
  60. b.mu.Lock()
  61. if len(b.backlog) > 0 {
  62. select {
  63. case b.c <- b.backlog[0]:
  64. b.backlog[0] = nil
  65. b.backlog = b.backlog[1:]
  66. default:
  67. }
  68. }
  69. b.mu.Unlock()
  70. }
  71. // Get returns a read channel on which values added to the buffer, via Put(),
  72. // are sent on.
  73. //
  74. // Upon reading a value from this channel, users are expected to call Load() to
  75. // send the next buffered value onto the channel if there is any.
  76. func (b *Unbounded) Get() <-chan interface{} {
  77. return b.c
  78. }