37 Star 404 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
session_manager.go 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-02-02 20:11 . Remote dialer
package remotedialer
import (
"fmt"
"math/rand"
"sync"
"github.com/gorilla/websocket"
)
type sessionManager struct {
sync.Mutex
clients map[string][]*session
}
func newSessionManager() *sessionManager {
return &sessionManager{
clients: map[string][]*session{},
}
}
func (sm *sessionManager) getByClient(clientKey string) (*session, error) {
sm.Lock()
defer sm.Unlock()
sessions := sm.clients[clientKey]
if len(sessions) > 0 {
return sessions[0], nil
}
return nil, fmt.Errorf("failed to find session for client %s", clientKey)
}
func (sm *sessionManager) add(clientKey string, conn *websocket.Conn) *session {
sessionKey := rand.Int63()
session := newSession(sessionKey, clientKey, conn)
session.sessionKey = sessionKey
sm.Lock()
defer sm.Unlock()
sm.clients[clientKey] = append(sm.clients[clientKey], session)
return session
}
func (sm *sessionManager) remove(s *session) {
sm.Lock()
defer sm.Unlock()
var newSessions []*session
for _, v := range sm.clients[s.clientKey] {
if v.sessionKey == s.sessionKey {
continue
}
newSessions = append(newSessions, v)
}
if len(newSessions) == 0 {
delete(sm.clients, s.clientKey)
} else {
sm.clients[s.clientKey] = newSessions
}
s.Close()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.1-rc3

搜索帮助