GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Verified Commit 5a0e7d48 authored by Philipp Winter's avatar Philipp Winter
Browse files

Fix bug where test cache timeout was ignored.

When bridgestrap read its cache from disk, it wouldn't only read the
cache entries but also the cache timeout, overriding whatever we
provided over the command line.  In other words, if bridgestrap saved a
cache whose timeout was set to 24h, and we later restart bridgestrap
with a given cache timeout of 18h, it would use 24h because that's what
it read from disk.

This patch fixes this bug in two ways: 1) we no longer write the cache
timeout to disk and 2) we set the cache timeout after reading the cached
entries from disk.
parent c93fde23
...@@ -30,11 +30,16 @@ type CacheEntry struct { ...@@ -30,11 +30,16 @@ type CacheEntry struct {
type TestCache struct { type TestCache struct {
// Entries maps a bridge's addr:port tuple to a cache entry. // Entries maps a bridge's addr:port tuple to a cache entry.
Entries map[string]*CacheEntry Entries map[string]*CacheEntry
// EntryTimeout determines how long a cache entry is valid for. // entryTimeout determines how long a cache entry is valid for.
EntryTimeout time.Duration entryTimeout time.Duration
l sync.Mutex l sync.Mutex
} }
// NewTestCache returns a new test cache.
func NewTestCache() *TestCache {
return &TestCache{Entries: make(map[string]*CacheEntry)}
}
// bridgeLineToAddrPort takes a bridge line as input and returns a string // bridgeLineToAddrPort takes a bridge line as input and returns a string
// consisting of the bridge's addr:port (for both IPv4 and IPv6 addresses). // consisting of the bridge's addr:port (for both IPv4 and IPv6 addresses).
func bridgeLineToAddrPort(bridgeLine string) (string, error) { func bridgeLineToAddrPort(bridgeLine string) (string, error) {
...@@ -109,14 +114,14 @@ func (tc *TestCache) ReadFromDisk(cacheFile string) error { ...@@ -109,14 +114,14 @@ func (tc *TestCache) ReadFromDisk(cacheFile string) error {
} }
// IsCached returns a cache entry if the given bridge line has been tested // IsCached returns a cache entry if the given bridge line has been tested
// recently (as determined by EntryTimeout), and nil otherwise. // recently (as determined by entryTimeout), and nil otherwise.
func (tc *TestCache) IsCached(bridgeLine string) *CacheEntry { func (tc *TestCache) IsCached(bridgeLine string) *CacheEntry {
// First, prune expired cache entries. // First, prune expired cache entries.
now := time.Now().UTC() now := time.Now().UTC()
tc.l.Lock() tc.l.Lock()
for index, entry := range (*tc).Entries { for index, entry := range (*tc).Entries {
if entry.Time.Before(now.Add(-(*tc).EntryTimeout)) { if entry.Time.Before(now.Add(-(*tc).entryTimeout)) {
delete((*tc).Entries, index) delete((*tc).Entries, index)
} }
} }
......
...@@ -18,7 +18,7 @@ func init() { ...@@ -18,7 +18,7 @@ func init() {
func NewCache() *TestCache { func NewCache() *TestCache {
return &TestCache{ return &TestCache{
Entries: make(map[string]*CacheEntry), Entries: make(map[string]*CacheEntry),
EntryTimeout: 24 * time.Hour, entryTimeout: 18 * time.Hour,
} }
} }
......
...@@ -165,14 +165,12 @@ func main() { ...@@ -165,14 +165,12 @@ func main() {
}) })
} }
cache = &TestCache{ cache = NewTestCache()
Entries: make(map[string]*CacheEntry),
EntryTimeout: time.Duration(cacheTimeout) * time.Hour,
}
log.Printf("Setting cache timeout to %s.", cache.EntryTimeout)
if err = cache.ReadFromDisk(cacheFile); err != nil { if err = cache.ReadFromDisk(cacheFile); err != nil {
log.Printf("Could not read cache: %s", err) log.Printf("Could not read cache: %s", err)
} }
cache.entryTimeout = time.Duration(cacheTimeout) * time.Hour
log.Printf("Set cache timeout to %s.", cache.entryTimeout)
if printCache { if printCache {
printPrettyCache() printPrettyCache()
return return
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment