| 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 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 25 | "source.monogon.dev/osbase/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 | } |
| 53 | |
| 54 | // Set updates the Value to the given data. It is safe to call this from |
| 55 | // multiple goroutines, including concurrently. |
| 56 | // |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 57 | // For more information about guarantees, see event.Value.Set. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 58 | func (m *Value[T]) Set(val T) { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 59 | m.mu.Lock() |
| 60 | defer m.mu.Unlock() |
| 61 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 62 | // Update the data that is provided on first Get() to watchers. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 63 | m.inner = val |
| 64 | m.innerSet = true |
| 65 | |
| 66 | // Go through all watchers, updating them on the new value and filtering out |
| 67 | // all closed watchers. |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 68 | newWatchers := m.watchers[:0] |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 69 | for _, w := range m.watchers { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 70 | if w.closed() { |
| 71 | continue |
| 72 | } |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 73 | w.update(val) |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 74 | newWatchers = append(newWatchers, w) |
| 75 | } |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 76 | if cap(newWatchers) > len(newWatchers)*3 { |
| 77 | reallocated := make([]*watcher[T], 0, len(newWatchers)*2) |
| 78 | newWatchers = append(reallocated, newWatchers...) |
| 79 | } |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 80 | m.watchers = newWatchers |
| 81 | } |
| 82 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 83 | // watcher implements the event.Watcher interface for watchers returned by |
| 84 | // Value. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 85 | type watcher[T any] struct { |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 86 | // valueC is a buffered channel of size 1 for submitting values to the |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 87 | // watcher. |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 88 | valueC chan T |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 89 | |
| 90 | // getSem is a channel-based semaphore (which is of size 1, and thus in |
| 91 | // fact a mutex) that is used to ensure that only a single .Get() call is |
| 92 | // active. It is implemented as a channel to permit concurrent .Get() calls |
| 93 | // to error out instead of blocking. |
| 94 | getSem chan struct{} |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 95 | // 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] | 96 | close chan struct{} |
| 97 | } |
| 98 | |
| 99 | // Watch retrieves a Watcher that keeps track on the version of the data |
| 100 | // contained within the Value that was last seen by a consumer. |
| 101 | // |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 102 | // For more information about guarantees, see event.Value.Watch. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 103 | func (m *Value[T]) Watch() event.Watcher[T] { |
| 104 | waiter := &watcher[T]{ |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 105 | valueC: make(chan T, 1), |
| 106 | close: make(chan struct{}), |
| 107 | getSem: make(chan struct{}, 1), |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 108 | } |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 109 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 110 | m.mu.Lock() |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 111 | // If the watchers slice is at capacity, drop closed watchers, and |
| 112 | // reallocate the slice at 2x length if it is not between 1.5x and 3x. |
| 113 | if len(m.watchers) == cap(m.watchers) { |
| 114 | newWatchers := m.watchers[:0] |
| 115 | for _, w := range m.watchers { |
| 116 | if !w.closed() { |
| 117 | newWatchers = append(newWatchers, w) |
| 118 | } |
| 119 | } |
| 120 | if cap(newWatchers)*2 < len(newWatchers)*3 || cap(newWatchers) > len(newWatchers)*3 { |
| 121 | reallocated := make([]*watcher[T], 0, len(newWatchers)*2) |
| 122 | newWatchers = append(reallocated, newWatchers...) |
| 123 | } |
| 124 | m.watchers = newWatchers |
| 125 | } |
| 126 | // Append this watcher to the Value. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 127 | m.watchers = append(m.watchers, waiter) |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 128 | // If the Value already has some value set, put it in the channel. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 129 | if m.innerSet { |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 130 | waiter.valueC <- m.inner |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 131 | } |
| 132 | m.mu.Unlock() |
| 133 | |
| 134 | return waiter |
| 135 | } |
| 136 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 137 | // closed returns whether this watcher has been closed. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 138 | func (m *watcher[T]) closed() bool { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 139 | select { |
| 140 | case _, ok := <-m.close: |
| 141 | if !ok { |
| 142 | return true |
| 143 | } |
| 144 | default: |
| 145 | } |
| 146 | return false |
| 147 | } |
| 148 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 149 | // update is the high level update-this-watcher function called by Value. |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 150 | func (m *watcher[T]) update(val T) { |
| 151 | // If there is already a value in the channel that was not retrieved, drop it. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 152 | select { |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 153 | case <-m.valueC: |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 154 | default: |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 155 | } |
| Jan Schär | 36bde9c | 2024-03-19 15:05:33 +0100 | [diff] [blame] | 156 | // The channel is now empty, so sending to it cannot block. |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 157 | m.valueC <- val |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 160 | func (m *watcher[T]) Close() error { |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 161 | close(m.close) |
| 162 | return nil |
| 163 | } |
| 164 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 165 | // Get blocks until a Value's data is available. See event.Watcher.Get for |
| 166 | // guarantees and more information. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 167 | 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] | 168 | // Make sure we're the only active .Get call. |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 169 | var empty T |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 170 | select { |
| 171 | case m.getSem <- struct{}{}: |
| 172 | default: |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 173 | return empty, fmt.Errorf("cannot Get() concurrently on a single waiter") |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 174 | } |
| 175 | defer func() { |
| 176 | <-m.getSem |
| 177 | }() |
| 178 | |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 179 | var predicate func(t T) bool |
| 180 | for _, opt := range opts { |
| 181 | if opt.Predicate != nil { |
| 182 | predicate = opt.Predicate |
| 183 | } |
| Tim Windelschmidt | 885668a | 2024-04-19 00:01:06 +0200 | [diff] [blame] | 184 | if opt.BacklogOnly { |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 185 | return empty, errors.New("BacklogOnly is not implemented for memory watchers") |
| Serge Bazanski | 8d45a05 | 2021-10-18 17:24:24 +0200 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 189 | for { |
| 190 | select { |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 191 | case <-ctx.Done(): |
| 192 | return empty, ctx.Err() |
| 193 | case val := <-m.valueC: |
| 194 | if predicate != nil && !predicate(val) { |
| 195 | continue |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 196 | } |
| Jan Schär | 5b997a1 | 2024-12-19 15:10:07 +0100 | [diff] [blame] | 197 | return val, nil |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |