|
| 1 | +// Copyright 2024 The Outline Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "sync" |
| 19 | + "sync/atomic" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +// ExpiringMap is a thread-safe, generic map that automatically removes |
| 24 | +// key-value pairs after a specified duration of inactivity. |
| 25 | +// It employs reference counting to ensure safe concurrent access and prevent |
| 26 | +// premature deletion during cleanup. |
| 27 | +type ExpiringMap[K comparable, V any] struct { |
| 28 | + data map[K]*item[V] |
| 29 | + mu sync.RWMutex |
| 30 | + expiryTime time.Duration |
| 31 | + done chan struct{} |
| 32 | +} |
| 33 | + |
| 34 | +type item[V any] struct { |
| 35 | + value V |
| 36 | + lastAccess time.Time |
| 37 | + refCount int32 |
| 38 | +} |
| 39 | + |
| 40 | +func NewExpiringMap[K comparable, V any](expiryTime time.Duration) *ExpiringMap[K, V] { |
| 41 | + em := &ExpiringMap[K, V]{ |
| 42 | + data: make(map[K]*item[V]), |
| 43 | + expiryTime: expiryTime, |
| 44 | + done: make(chan struct{}), |
| 45 | + } |
| 46 | + go em.cleanupLoop() |
| 47 | + return em |
| 48 | +} |
| 49 | + |
| 50 | +func (em *ExpiringMap[K, V]) Set(key K, value V) { |
| 51 | + em.mu.Lock() |
| 52 | + defer em.mu.Unlock() |
| 53 | + |
| 54 | + em.data[key] = &item[V]{ |
| 55 | + value: value, |
| 56 | + lastAccess: time.Now(), |
| 57 | + refCount: 0, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (em *ExpiringMap[K, V]) Get(key K) (V, bool) { |
| 62 | + em.mu.RLock() |
| 63 | + item, ok := em.data[key] |
| 64 | + if !ok { |
| 65 | + em.mu.RUnlock() |
| 66 | + var zeroValue V |
| 67 | + return zeroValue, false |
| 68 | + } |
| 69 | + |
| 70 | + atomic.AddInt32(&item.refCount, 1) |
| 71 | + em.mu.RUnlock() |
| 72 | + |
| 73 | + em.mu.Lock() |
| 74 | + defer em.mu.Unlock() |
| 75 | + |
| 76 | + atomic.AddInt32(&item.refCount, -1) |
| 77 | + |
| 78 | + item.lastAccess = time.Now() |
| 79 | + return item.value, true |
| 80 | +} |
| 81 | + |
| 82 | +func (em *ExpiringMap[K, V]) cleanup() { |
| 83 | + em.mu.Lock() |
| 84 | + defer em.mu.Unlock() |
| 85 | + |
| 86 | + for key, item := range em.data { |
| 87 | + if time.Since(item.lastAccess) > em.expiryTime && atomic.LoadInt32(&item.refCount) == 0 { |
| 88 | + delete(em.data, key) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func (em *ExpiringMap[K, V]) cleanupLoop() { |
| 94 | + ticker := time.NewTicker(em.expiryTime / 2) |
| 95 | + defer ticker.Stop() |
| 96 | + |
| 97 | + for { |
| 98 | + select { |
| 99 | + case <-ticker.C: |
| 100 | + em.cleanup() |
| 101 | + case <-em.done: |
| 102 | + return |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func (em *ExpiringMap[K, V]) StopCleanup() { |
| 108 | + close(em.done) |
| 109 | +} |
0 commit comments