requestAndWait marks the cache as recently requested and returns the current cached result. If the background goroutine is not running it starts it and waits for the first populate before returning.
(ctx context.Context)
| 69 | // result. If the background goroutine is not running it starts it and waits for the |
| 70 | // first populate before returning. |
| 71 | func (s *procCacheState) requestAndWait(ctx context.Context) (*wshrpc.ProcessListResponse, error) { |
| 72 | s.lock.Lock() |
| 73 | s.lastRequest = time.Now() |
| 74 | if !s.running { |
| 75 | s.running = true |
| 76 | readyCh := make(chan struct{}) |
| 77 | s.ready = readyCh |
| 78 | go s.runLoop(readyCh) |
| 79 | } |
| 80 | readyCh := s.ready |
| 81 | s.lock.Unlock() |
| 82 | |
| 83 | if readyCh != nil { |
| 84 | select { |
| 85 | case <-readyCh: |
| 86 | case <-ctx.Done(): |
| 87 | return nil, ctx.Err() |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | s.lock.Lock() |
| 92 | result := s.cached |
| 93 | s.lock.Unlock() |
| 94 | |
| 95 | if result == nil { |
| 96 | return nil, fmt.Errorf("process list unavailable") |
| 97 | } |
| 98 | return result, nil |
| 99 | } |
| 100 | |
| 101 | func (s *procCacheState) touchLastRequest() { |
| 102 | s.lock.Lock() |
no test coverage detected