| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package subscriber | 5 package subscriber |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "sync" | 8 "sync" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // sleep. This is a simple method to obtain the desired effect of avoiding | 157 // sleep. This is a simple method to obtain the desired effect of avoiding |
| 158 // pointless burst Pub/Sub spam when the service has nothing useful to offer. | 158 // pointless burst Pub/Sub spam when the service has nothing useful to offer. |
| 159 func (s *Subscriber) noDataSleep(c context.Context) { | 159 func (s *Subscriber) noDataSleep(c context.Context) { |
| 160 s.noDataMu.Lock() | 160 s.noDataMu.Lock() |
| 161 defer s.noDataMu.Unlock() | 161 defer s.noDataMu.Unlock() |
| 162 | 162 |
| 163 d := s.NoDataDelay | 163 d := s.NoDataDelay |
| 164 if d <= 0 { | 164 if d <= 0 { |
| 165 d = DefaultNoDataDelay | 165 d = DefaultNoDataDelay |
| 166 } | 166 } |
| 167 » cancellableSleep(c, d) | 167 » clock.CancelSleep(c, d) |
| 168 } | 168 } |
| 169 | |
| 170 // cancellableSleep sleeps, returning either when the sleep duration has expired | |
| 171 // or the supplied context has been canceled. | |
| 172 func cancellableSleep(c context.Context, delay time.Duration) { | |
| 173 // Sleep for "delay", stopping early if our Context is canceled. | |
| 174 select { | |
| 175 case <-clock.After(c, delay): | |
| 176 break | |
| 177 | |
| 178 case <-c.Done(): | |
| 179 break | |
| 180 } | |
| 181 } | |
| OLD | NEW |