| 162 | } |
| 163 | |
| 164 | func (s *Storage) gc() { |
| 165 | // Close gcExited first so a panic in NewTicker (e.g. non-positive |
| 166 | // gcInterval) still unblocks Close. |
| 167 | defer close(s.gcExited) |
| 168 | ticker := time.NewTicker(s.gcInterval) |
| 169 | defer ticker.Stop() |
| 170 | var expired []string |
| 171 | |
| 172 | for { |
| 173 | select { |
| 174 | case <-s.done: |
| 175 | return |
| 176 | case <-ticker.C: |
| 177 | ts := utils.Timestamp() |
| 178 | expired = expired[:0] |
| 179 | s.mux.RLock() |
| 180 | for id, v := range s.db { |
| 181 | if v.expiry != 0 && v.expiry < ts { |
| 182 | expired = append(expired, id) |
| 183 | } |
| 184 | } |
| 185 | s.mux.RUnlock() |
| 186 | |
| 187 | if len(expired) == 0 { |
| 188 | // avoid locking if nothing to delete |
| 189 | continue |
| 190 | } |
| 191 | |
| 192 | s.mux.Lock() |
| 193 | // Double-checked locking. |
| 194 | // We might have replaced the item in the meantime. |
| 195 | for i := range expired { |
| 196 | v := s.db[expired[i]] |
| 197 | if v.expiry != 0 && v.expiry <= ts { |
| 198 | delete(s.db, expired[i]) |
| 199 | } |
| 200 | } |
| 201 | s.mux.Unlock() |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Conn returns the underlying storage map. The returned map remains shared with |
| 207 | // the storage, so callers must not modify it and must synchronize any access |