| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 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 | |
| 18 | // Package buffer provides an implementation of an unbounded buffer. |
| 19 | package buffer |
| 20 | |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 21 | import ( |
| 22 | "errors" |
| 23 | "sync" |
| 24 | ) |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 25 | |
| 26 | // Unbounded is an implementation of an unbounded buffer which does not use |
| 27 | // extra goroutines. This is typically used for passing updates from one entity |
| 28 | // to another within gRPC. |
| 29 | // |
| 30 | // All methods on this type are thread-safe and don't block on anything except |
| 31 | // the underlying mutex used for synchronization. |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 32 | // |
| 33 | // Unbounded supports values of any type to be stored in it by using a channel |
| 34 | // of `any`. This means that a call to Put() incurs an extra memory allocation, |
| 35 | // and also that users need a type assertion while reading. For performance |
| 36 | // critical code paths, using Unbounded is strongly discouraged and defining a |
| 37 | // new type specific implementation of this buffer is preferred. See |
| 38 | // internal/transport/transport.go for an example of this. |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 39 | type Unbounded struct { |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 40 | c chan any |
| 41 | closed bool |
| 42 | closing bool |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 43 | mu sync.Mutex |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 44 | backlog []any |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // NewUnbounded returns a new instance of Unbounded. |
| 48 | func NewUnbounded() *Unbounded { |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 49 | return &Unbounded{c: make(chan any, 1)} |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 52 | var errBufferClosed = errors.New("Put called on closed buffer.Unbounded") |
| 53 | |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 54 | // Put adds t to the unbounded buffer. |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 55 | func (b *Unbounded) Put(t any) error { |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 56 | b.mu.Lock() |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 57 | defer b.mu.Unlock() |
| 58 | if b.closing { |
| 59 | return errBufferClosed |
| 60 | } |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 61 | if len(b.backlog) == 0 { |
| 62 | select { |
| 63 | case b.c <- t: |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 64 | return nil |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 65 | default: |
| 66 | } |
| 67 | } |
| 68 | b.backlog = append(b.backlog, t) |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 69 | return nil |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 72 | // Load sends the earliest buffered data, if any, onto the read channel returned |
| 73 | // by Get(). Users are expected to call this every time they successfully read a |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 74 | // value from the read channel. |
| 75 | func (b *Unbounded) Load() { |
| 76 | b.mu.Lock() |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 77 | defer b.mu.Unlock() |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 78 | if len(b.backlog) > 0 { |
| 79 | select { |
| 80 | case b.c <- b.backlog[0]: |
| 81 | b.backlog[0] = nil |
| 82 | b.backlog = b.backlog[1:] |
| 83 | default: |
| 84 | } |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 85 | } else if b.closing && !b.closed { |
| 86 | b.closed = true |
| 87 | close(b.c) |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 88 | } |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Get returns a read channel on which values added to the buffer, via Put(), |
| 92 | // are sent on. |
| 93 | // |
| 94 | // Upon reading a value from this channel, users are expected to call Load() to |
| 95 | // send the next buffered value onto the channel if there is any. |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 96 | // |
| 97 | // If the unbounded buffer is closed, the read channel returned by this method |
| 98 | // is closed after all data is drained. |
| 99 | func (b *Unbounded) Get() <-chan any { |
| Devmalya Paul | dd23a99 | 2019-11-14 07:06:31 +0000 | [diff] [blame] | 100 | return b.c |
| 101 | } |
| Abhay Kumar | a61c522 | 2025-11-10 07:32:50 +0000 | [diff] [blame^] | 102 | |
| 103 | // Close closes the unbounded buffer. No subsequent data may be Put(), and the |
| 104 | // channel returned from Get() will be closed after all the data is read and |
| 105 | // Load() is called for the final time. |
| 106 | func (b *Unbounded) Close() { |
| 107 | b.mu.Lock() |
| 108 | defer b.mu.Unlock() |
| 109 | if b.closing { |
| 110 | return |
| 111 | } |
| 112 | b.closing = true |
| 113 | if len(b.backlog) == 0 { |
| 114 | b.closed = true |
| 115 | close(b.c) |
| 116 | } |
| 117 | } |