/
session.go
48 lines (38 loc) · 925 Bytes
/
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
package core
import (
"sync"
"github.com/hashicorp/yamux"
)
var sessionMgr = &SessionManager{}
// SessionManager defines the session info add/delete/get actions
type SessionManager struct {
sessions sync.Map
}
// GetSessionManager returs the singleton of session manager
func GetSessionManager() *SessionManager {
return sessionMgr
}
// Session defines each opennotr_client to opennotr_server connection
type Session struct {
conn *yamux.Session
rxbytes uint64
txbytes uint64
}
func newSession(conn *yamux.Session, vip string) *Session {
return &Session{
conn: conn,
}
}
func (mgr *SessionManager) AddSession(vip string, sess *Session) {
mgr.sessions.Store(vip, sess)
}
func (mgr *SessionManager) GetSession(vip string) *Session {
val, ok := mgr.sessions.Load(vip)
if !ok {
return nil
}
return val.(*Session)
}
func (mgr *SessionManager) DeleteSession(vip string) {
mgr.sessions.Delete(vip)
}