| 24 | } |
| 25 | |
| 26 | func (m *ActiveUsers) UpdateUserTimestamp(userID string, ts int64) { |
| 27 | m.mu.RLock() |
| 28 | u := m.timestamps[userID] |
| 29 | m.mu.RUnlock() |
| 30 | |
| 31 | if u != nil { |
| 32 | u.Store(ts) |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | // Pre-allocate new atomic to avoid doing allocation with lock held. |
| 37 | newAtomic := atomic.NewInt64(ts) |
| 38 | |
| 39 | // We need RW lock to create new entry. |
| 40 | m.mu.Lock() |
| 41 | u = m.timestamps[userID] |
| 42 | |
| 43 | if u != nil { |
| 44 | // Unlock first to reduce contention. |
| 45 | m.mu.Unlock() |
| 46 | |
| 47 | u.Store(ts) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | m.timestamps[userID] = newAtomic |
| 52 | m.mu.Unlock() |
| 53 | } |
| 54 | |
| 55 | // PurgeInactiveUsers removes users that were last active before given deadline, and returns removed users. |
| 56 | func (m *ActiveUsers) PurgeInactiveUsers(deadline int64) []string { |