Use timers to determine when it's time to prune cache entries
Each time a new cache entry is added, bridgestrap iterates over existing cache entries to figure out what entries have expired:
// First, prune expired cache entries.
now := time.Now()
cacheMutex.Lock()
for index, entry := range *tc {
if entry.Time.Before(now.Add(-CacheValidity)) {
delete(*tc, index)
}
}
This scales linearly, which is bad news. On my core i7 2.9 GHz laptop, it takes 0.3 ms to prune a cache holding 10,000 elements. @cohosh suggested that we could use Timers instead: each time a cache entry is added, we set up a new timer. In parallel, we have a goroutine running that waits for the timers to fire, to then delete the expired cache entries.