add Get method to traceIdTtlMap

This commit is contained in:
2025-05-19 21:19:24 +02:00
parent ce7d18e880
commit 00944d01ad

View File

@@ -90,19 +90,7 @@ func (m *TTLMap) Add(key string) {
* Removes the trace id from the map if it has expired.
*/
func (m *TTLMap) Exists(key string) bool {
m.mu.RLock()
value, ok := m.m[key]
m.mu.RUnlock()
if ok {
if value < time.Now().Unix() {
m.mu.Lock()
delete(m.m, key)
m.mu.Unlock()
return false
}
}
_, ok := m.Get(key)
return ok
}
@@ -116,6 +104,26 @@ func (m *TTLMap) insertEntry(key string, value int64) {
m.mu.Unlock()
}
/**
* Gets an entry from the map.
*/
func (m *TTLMap) Get(key string) (int64, bool) {
m.mu.RLock()
value, ok := m.m[key]
m.mu.RUnlock()
if ok {
if value < time.Now().Unix() {
m.mu.Lock()
delete(m.m, key)
m.mu.Unlock()
return 0, false
}
}
return value, ok
}
/**
* Gets an entry from the map.
* Only used for testing.
@@ -124,7 +132,6 @@ func (m *TTLMap) getEntry(key string) (int64, bool) {
m.mu.RLock()
value, ok := m.m[key]
m.mu.RUnlock()
return value, ok
}