-
Notifications
You must be signed in to change notification settings - Fork 47
/
mem_session.go
57 lines (51 loc) · 1.04 KB
/
mem_session.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
56
57
package guac
import (
"net/http"
"sync"
)
// MemorySessionStore is a simple in-memory store of connected sessions that is used by
// the WebsocketServer to store active sessions.
type MemorySessionStore struct {
sync.RWMutex
ConnIds map[string]int
}
// NewMemorySessionStore creates a new store
func NewMemorySessionStore() *MemorySessionStore {
return &MemorySessionStore{
ConnIds: map[string]int{},
}
}
// Get returns a connection by uuid
func (s *MemorySessionStore) Get(id string) int {
s.RLock()
defer s.RUnlock()
return s.ConnIds[id]
}
// Add inserts a new connection by uuid
func (s *MemorySessionStore) Add(id string, req *http.Request) {
s.Lock()
defer s.Unlock()
n, ok := s.ConnIds[id]
if !ok {
s.ConnIds[id] = 1
return
}
n++
s.ConnIds[id] = n
return
}
// Delete removes a connection by uuid
func (s *MemorySessionStore) Delete(id string, req *http.Request, tunnel Tunnel) {
s.Lock()
defer s.Unlock()
n, ok := s.ConnIds[id]
if !ok {
return
}
if n == 1 {
delete(s.ConnIds, id)
return
}
s.ConnIds[id]--
return
}