-
Notifications
You must be signed in to change notification settings - Fork 45
/
stream_map.go
53 lines (43 loc) · 1.01 KB
/
stream_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
package muxado
import (
"sync"
"github.com/inconshreveable/muxado/frame"
)
const (
initMapCapacity = 128 // not too much extra memory wasted to avoid allocations
)
// streamMap is a map of stream ids -> streams guarded by a read/write lock
type streamMap struct {
sync.RWMutex
table map[frame.StreamId]streamPrivate
}
func (m *streamMap) Get(id frame.StreamId) (s streamPrivate, ok bool) {
m.RLock()
s, ok = m.table[id]
m.RUnlock()
return
}
func (m *streamMap) Set(id frame.StreamId, str streamPrivate) {
m.Lock()
m.table[id] = str
m.Unlock()
}
func (m *streamMap) Delete(id frame.StreamId) {
m.Lock()
delete(m.table, id)
m.Unlock()
}
func (m *streamMap) Each(fn func(frame.StreamId, streamPrivate)) {
m.RLock()
streams := make(map[frame.StreamId]streamPrivate, len(m.table))
for k, v := range m.table {
streams[k] = v
}
m.RUnlock()
for id, str := range streams {
fn(id, str)
}
}
func newStreamMap() *streamMap {
return &streamMap{table: make(map[frame.StreamId]streamPrivate, initMapCapacity)}
}