forked from openshift/geard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_identifier_map.go
55 lines (45 loc) · 1016 Bytes
/
request_identifier_map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package dispatcher
import (
"container/list"
"github.com/openshift/geard/jobs"
"sync"
)
type RequestIdentifierMap struct {
keys map[string]interface{}
order *list.List
max int
lock sync.RWMutex
}
func NewRequestIdentifierMap(size int) *RequestIdentifierMap {
return &RequestIdentifierMap{
keys: make(map[string]interface{}, size),
order: list.New(),
max: size,
}
}
func (m RequestIdentifierMap) Get(id jobs.RequestIdentifier) interface{} {
key := string(id)
m.lock.RLock()
defer m.lock.RUnlock()
return m.keys[key]
}
func (m RequestIdentifierMap) Put(id jobs.RequestIdentifier, v interface{}) (interface{}, bool) {
key := string(id)
m.lock.Lock()
defer m.lock.Unlock()
if existing, contains := m.keys[key]; contains {
if v == nil {
m.keys[key] = nil
}
return existing, true
}
if m.order.Len() > m.max {
last := m.order.Back()
m.order.Remove(last)
id := last.Value.(string)
delete(m.keys, id)
}
m.order.PushFront(key)
m.keys[key] = v
return nil, false
}