Close closes the Variable. The Variable is unusable after Close returns.
()
| 291 | |
| 292 | // Close closes the Variable. The Variable is unusable after Close returns. |
| 293 | func (c *Variable) Close() error { |
| 294 | // Record that we're closing. Subsequent calls to Watch/Latest will return ErrClosed. |
| 295 | c.mu.Lock() |
| 296 | if errors.Is(c.lastErr, ErrClosed) { |
| 297 | c.mu.Unlock() |
| 298 | return ErrClosed |
| 299 | } |
| 300 | c.last = Snapshot{} |
| 301 | c.lastErr = ErrClosed |
| 302 | |
| 303 | // Close any remaining channels to wake up any callers that are waiting on them. |
| 304 | close(c.changed) |
| 305 | // If it's the first good value, close haveGoodCh so that Latest doesn't block. |
| 306 | select { |
| 307 | case <-c.haveGoodCh: |
| 308 | default: |
| 309 | close(c.haveGoodCh) |
| 310 | } |
| 311 | c.mu.Unlock() |
| 312 | |
| 313 | // Shut down the background goroutine. |
| 314 | c.backgroundCancel() |
| 315 | <-c.backgroundDone |
| 316 | |
| 317 | // Close the driver. |
| 318 | err := c.dw.Close() |
| 319 | return wrapError(c.dw, err) |
| 320 | } |
| 321 | |
| 322 | func wrapError(w driver.Watcher, err error) error { |
| 323 | if err == nil { |