-
Notifications
You must be signed in to change notification settings - Fork 6
/
sessions.go
52 lines (44 loc) · 956 Bytes
/
sessions.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
package ike
import "sync"
type Sessions interface {
Add(spi uint64, session *Session)
Remove(spi uint64)
Get(spi uint64) (*Session, bool)
ForEach(action func(*Session))
}
func NewSessions() Sessions {
return &sessions{
_sessions: make(map[uint64]*Session),
}
}
type sessions struct {
_sessions map[uint64]*Session
mtx sync.Mutex
}
func (s *sessions) Add(spi uint64, sess *Session) {
s.mtx.Lock()
defer s.mtx.Unlock()
s._sessions[spi] = sess
}
func (s *sessions) Remove(spi uint64) {
s.mtx.Lock()
defer s.mtx.Unlock()
delete(s._sessions, spi)
}
func (s *sessions) Get(spi uint64) (*Session, bool) {
s.mtx.Lock()
defer s.mtx.Unlock()
session, found := s._sessions[spi]
return session, found
}
func (s *sessions) ForEach(action func(*Session)) {
s.mtx.Lock()
var temp []*Session
for _, session := range s._sessions {
temp = append(temp, session)
}
s.mtx.Unlock()
for _, session := range temp {
action(session)
}
}