PurgeInactiveUsers removes users that were last active before given deadline, and returns removed users.
(deadline int64)
| 54 | |
| 55 | // PurgeInactiveUsers removes users that were last active before given deadline, and returns removed users. |
| 56 | func (m *ActiveUsers) PurgeInactiveUsers(deadline int64) []string { |
| 57 | // Find inactive users with read-lock. |
| 58 | m.mu.RLock() |
| 59 | inactive := make([]string, 0, len(m.timestamps)) |
| 60 | |
| 61 | for userID, ts := range m.timestamps { |
| 62 | if ts.Load() <= deadline { |
| 63 | inactive = append(inactive, userID) |
| 64 | } |
| 65 | } |
| 66 | m.mu.RUnlock() |
| 67 | |
| 68 | if len(inactive) == 0 { |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | // Cleanup inactive users. |
| 73 | for ix := 0; ix < len(inactive); { |
| 74 | userID := inactive[ix] |
| 75 | deleted := false |
| 76 | |
| 77 | m.mu.Lock() |
| 78 | u := m.timestamps[userID] |
| 79 | if u != nil && u.Load() <= deadline { |
| 80 | delete(m.timestamps, userID) |
| 81 | deleted = true |
| 82 | } |
| 83 | m.mu.Unlock() |
| 84 | |
| 85 | if deleted { |
| 86 | // keep it in the output |
| 87 | ix++ |
| 88 | } else { |
| 89 | // not really inactive, remove it from output |
| 90 | inactive = append(inactive[:ix], inactive[ix+1:]...) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return inactive |
| 95 | } |
| 96 | |
| 97 | func (m *ActiveUsers) ActiveUsers(deadline int64) []string { |
| 98 | m.mu.RLock() |