MCPcopy
hub / github.com/tinode/chat / NewSession

Method NewSession

server/sessionstore.go:76–160  ·  view source on GitHub ↗

NewSession creates a new session and saves it to the session store.

(conn any, sid string)

Source from the content-addressed store, hash-verified

74
75// NewSession creates a new session and saves it to the session store.
76func (ss *SessionStore) NewSession(conn any, sid string) (*Session, int) {
77 var s Session
78
79 if sid == "" {
80 s.sid = store.Store.GetUidString()
81 } else {
82 s.sid = sid
83 }
84
85 ss.lock.Lock()
86 if _, found := ss.sessCache[s.sid]; found {
87 logs.Err.Fatalln("ERROR! duplicate session ID", s.sid)
88 }
89 ss.lock.Unlock()
90
91 switch c := conn.(type) {
92 case *websocket.Conn:
93 s.proto = WEBSOCK
94 s.ws = c
95 case http.ResponseWriter:
96 s.proto = LPOLL
97 // no need to store c for long polling, it changes with every request
98 case *ClusterNode:
99 s.proto = MULTIPLEX
100 s.clnode = c
101 case pbx.Node_MessageLoopServer:
102 s.proto = GRPC
103 s.grpcnode = c
104 default:
105 logs.Err.Panicln("session: unknown connection type", conn)
106 }
107
108 s.subs = make(map[string]*Subscription)
109 s.send = make(chan any, sendQueueLimit+32) // buffered
110 s.stop = make(chan any, 1) // Buffered by 1 just to make it non-blocking
111 s.detach = make(chan string, 64) // buffered
112
113 s.bkgTimer = time.NewTimer(time.Hour)
114 s.bkgTimer.Stop()
115
116 // Make sure at most 1 request is modifying session/topic state at any time.
117 // TODO: use Mutex & CondVar?
118 s.inflightReqs = newBoundedWaitGroup(1)
119
120 s.lastTouched = time.Now()
121
122 ss.lock.Lock()
123
124 if s.proto == LPOLL {
125 // Only LP sessions need to be sorted by last active
126 s.lpTracker = ss.lru.PushFront(&s)
127 }
128
129 ss.sessCache[s.sid] = &s
130
131 // Expire stale long polling sessions: ss.lru contains only long polling sessions.
132 // If ss.lru is empty this is a noop.
133 var expired []*Session

Callers 5

MessageLoopMethod · 0.80
serveWebSocketFunction · 0.80
serveLongPollFunction · 0.80
TopicMasterMethod · 0.80
InitMethod · 0.80

Calls 10

newBoundedWaitGroupFunction · 0.85
statsSetFunction · 0.85
statsIncFunction · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
cleanUpMethod · 0.80
GetUidStringMethod · 0.65
StopMethod · 0.65
RemoveMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected