agent/cache: don't every block on NotifyCh

This commit is contained in:
Mitchell Hashimoto 2018-04-20 10:43:50 -07:00
parent 3f80a9f330
commit ad3928b6bd
No known key found for this signature in database
GPG key ID: 744E147AA52F5B0A

19
agent/cache/entry.go vendored
View file

@ -76,7 +76,7 @@ func (h *expiryHeap) Fix(entry *cacheEntryExpiry) {
// is zero, it means the head-of-line didn't change while the value
// changed. Notify to reset our expiry worker.
if idx == 0 && entry.HeapIndex == 0 {
h.NotifyCh <- struct{}{}
h.notify()
}
}
@ -91,7 +91,7 @@ func (h *expiryHeap) Swap(i, j int) {
// to re-update the timer we're waiting on for the soonest expiring
// value.
if i == 0 || j == 0 {
h.NotifyCh <- struct{}{}
h.notify()
}
}
@ -113,7 +113,7 @@ func (h *expiryHeap) Push(x interface{}) {
// Swap won't be called; nothing to swap! We can call it right away
// because all heap operations are within a lock.
if len(h.Entries) == 0 {
h.NotifyCh <- struct{}{}
h.notify()
}
h.Entries = append(h.Entries, entry)
@ -126,3 +126,16 @@ func (h *expiryHeap) Pop() interface{} {
h.Entries = old[0 : n-1]
return x
}
func (h *expiryHeap) notify() {
select {
case h.NotifyCh <- struct{}{}:
// Good
default:
// If the send would've blocked, we just ignore it. The reason this
// is safe is because NotifyCh should always be a buffered channel.
// If this blocks, it means that there is a pending message anyways
// so the receiver will restart regardless.
}
}