| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 17 | package memory |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 21 | "errors" |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 22 | "fmt" |
| 23 | "sync" |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 24 | |
| 25 | "source.monogon.dev/metropolis/pkg/event" |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var ( |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 29 | // Type assert that *Value implements Value. We do this artificially, as |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 30 | // there currently is no code path that needs this to be strictly true. However, |
| 31 | // users of this library might want to rely on the Value type instead of |
| 32 | // particular Value implementations. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 33 | _ event.Value[int] = &Value[int]{} |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 34 | ) |
| 35 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 36 | // Value is a 'memory value', which implements a event.Value stored in memory. |
| 37 | // It is safe to construct an empty object of this type. However, this must not |
| 38 | // be copied. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 39 | type Value[T any] struct { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 40 | // mu guards the inner, innerSet and watchers fields. |
| 41 | mu sync.RWMutex |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 42 | // inner is the latest data Set on the Value. It is used to provide the |
| 43 | // newest version of the Set data to new watchers. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 44 | inner T |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 45 | // innerSet is true when inner has been Set at least once. It is used to |
| 46 | // differentiate between a nil and unset value. |
| 47 | innerSet bool |
| 48 | // watchers is the list of watchers that should be updated when new data is |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 49 | // Set. It will grow on every .Watch() and shrink any time a watcher is |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 50 | // determined to have been closed. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 51 | watchers []*watcher[T] |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 52 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 53 | // Sync, if set to true, blocks all .Set() calls on the Value until all |
| 54 | // Watchers derived from it actively .Get() the new value. This can be used |
| 55 | // to ensure Watchers always receive a full log of all Set() calls. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 56 | // |
| 57 | // This must not be changed after the first .Set/.Watch call. |
| 58 | // |
| 59 | // This is an experimental API and subject to change. It might be migrated |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 60 | // to per-Watcher settings defined within the main event.Value/Watcher |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 61 | // interfaces. |
| 62 | Sync bool |
| 63 | } |
| 64 | |
| 65 | // Set updates the Value to the given data. It is safe to call this from |
| 66 | // multiple goroutines, including concurrently. |
| 67 | // |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 68 | // For more information about guarantees, see event.Value.Set. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 69 | func (m *Value[T]) Set(val T) { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 70 | m.mu.Lock() |
| 71 | defer m.mu.Unlock() |
| 72 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 73 | // Update the data that is provided on first Get() to watchers. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 74 | m.inner = val |
| 75 | m.innerSet = true |
| 76 | |
| 77 | // Go through all watchers, updating them on the new value and filtering out |
| 78 | // all closed watchers. |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 79 | newWatchers := m.watchers[:0] |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 80 | for _, w := range m.watchers { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 81 | if w.closed() { |
| 82 | continue |
| 83 | } |
| 84 | w.update(m.Sync, val) |
| 85 | newWatchers = append(newWatchers, w) |
| 86 | } |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 87 | if cap(newWatchers) > len(newWatchers)*3 { |
| 88 | reallocated := make([]*watcher[T], 0, len(newWatchers)*2) |
| 89 | newWatchers = append(reallocated, newWatchers...) |
| 90 | } |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 91 | m.watchers = newWatchers |
| 92 | } |
| 93 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 94 | // watcher implements the event.Watcher interface for watchers returned by |
| 95 | // Value. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 96 | type watcher[T any] struct { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 97 | // bufferedC is a buffered channel of size 1 for submitting values to the |
| 98 | // watcher. |
| 99 | bufferedC chan T |
| 100 | // unbufferedC is an unbuffered channel, which is used when Sync is enabled. |
| 101 | unbufferedC chan T |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 102 | |
| 103 | // getSem is a channel-based semaphore (which is of size 1, and thus in |
| 104 | // fact a mutex) that is used to ensure that only a single .Get() call is |
| 105 | // active. It is implemented as a channel to permit concurrent .Get() calls |
| 106 | // to error out instead of blocking. |
| 107 | getSem chan struct{} |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 108 | // close is a channel that is closed when this watcher is itself Closed. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 109 | close chan struct{} |
| 110 | } |
| 111 | |
| 112 | // Watch retrieves a Watcher that keeps track on the version of the data |
| 113 | // contained within the Value that was last seen by a consumer. |
| 114 | // |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 115 | // For more information about guarantees, see event.Value.Watch. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 116 | func (m *Value[T]) Watch() event.Watcher[T] { |
| 117 | waiter := &watcher[T]{ |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 118 | bufferedC: make(chan T, 1), |
| 119 | unbufferedC: make(chan T), |
| 120 | close: make(chan struct{}), |
| 121 | getSem: make(chan struct{}, 1), |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 122 | } |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 123 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 124 | m.mu.Lock() |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 125 | // If the watchers slice is at capacity, drop closed watchers, and |
| 126 | // reallocate the slice at 2x length if it is not between 1.5x and 3x. |
| 127 | if len(m.watchers) == cap(m.watchers) { |
| 128 | newWatchers := m.watchers[:0] |
| 129 | for _, w := range m.watchers { |
| 130 | if !w.closed() { |
| 131 | newWatchers = append(newWatchers, w) |
| 132 | } |
| 133 | } |
| 134 | if cap(newWatchers)*2 < len(newWatchers)*3 || cap(newWatchers) > len(newWatchers)*3 { |
| 135 | reallocated := make([]*watcher[T], 0, len(newWatchers)*2) |
| 136 | newWatchers = append(reallocated, newWatchers...) |
| 137 | } |
| 138 | m.watchers = newWatchers |
| 139 | } |
| 140 | // Append this watcher to the Value. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 141 | m.watchers = append(m.watchers, waiter) |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 142 | // If the Value already has some value set, put it in the buffered channel. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 143 | if m.innerSet { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 144 | waiter.bufferedC <- m.inner |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 145 | } |
| 146 | m.mu.Unlock() |
| 147 | |
| 148 | return waiter |
| 149 | } |
| 150 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 151 | // closed returns whether this watcher has been closed. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 152 | func (m *watcher[T]) closed() bool { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 153 | select { |
| 154 | case _, ok := <-m.close: |
| 155 | if !ok { |
| 156 | return true |
| 157 | } |
| 158 | default: |
| 159 | } |
| 160 | return false |
| 161 | } |
| 162 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 163 | // update is the high level update-this-watcher function called by Value. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 164 | func (m *watcher[T]) update(sync bool, val T) { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 165 | // If synchronous delivery was requested, block until a watcher .Gets it, |
| 166 | // or is closed. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 167 | if sync { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 168 | select { |
| 169 | case m.unbufferedC <- val: |
| 170 | case <-m.close: |
| 171 | } |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 172 | return |
| 173 | } |
| 174 | |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 175 | // Otherwise, deliver asynchronously. If there is already a value in the |
| 176 | // buffered channel that was not retrieved, drop it. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 177 | select { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 178 | case <-m.bufferedC: |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 179 | default: |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 180 | } |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 181 | // The channel is now empty, so sending to it cannot block. |
| 182 | m.bufferedC <- val |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 185 | func (m *watcher[T]) Close() error { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 186 | close(m.close) |
| 187 | return nil |
| 188 | } |
| 189 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 190 | // Get blocks until a Value's data is available. See event.Watcher.Get for |
| 191 | // guarantees and more information. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 192 | func (m *watcher[T]) Get(ctx context.Context, opts ...event.GetOption[T]) (T, error) { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 193 | // Make sure we're the only active .Get call. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 194 | var empty T |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 195 | select { |
| 196 | case m.getSem <- struct{}{}: |
| 197 | default: |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 198 | return empty, fmt.Errorf("cannot Get() concurrently on a single waiter") |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 199 | } |
| 200 | defer func() { |
| 201 | <-m.getSem |
| 202 | }() |
| 203 | |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 204 | var predicate func(t T) bool |
| 205 | for _, opt := range opts { |
| 206 | if opt.Predicate != nil { |
| 207 | predicate = opt.Predicate |
| 208 | } |
| 209 | if opt.BacklogOnly != false { |
| 210 | return empty, errors.New("BacklogOnly is not implemented for memory watchers") |
| Serge Bazanski | 8d45a05 | 2021-10-18 17:24:24 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 214 | for { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 215 | var val T |
| 216 | // For Sync values, ensure the initial value in the buffered |
| 217 | // channel is delivered first. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 218 | select { |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 219 | case val = <-m.bufferedC: |
| 220 | default: |
| 221 | select { |
| 222 | case <-ctx.Done(): |
| 223 | return empty, ctx.Err() |
| 224 | case val = <-m.bufferedC: |
| 225 | case val = <-m.unbufferedC: |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 226 | } |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 227 | } |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 228 | if predicate != nil && !predicate(val) { |
| 229 | continue |
| 230 | } |
| 231 | return val, nil |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 232 | } |
| 233 | } |