reformat
This commit is contained in:
1 parent
48a991e11b
commit
d3e39f66a2
4 files changed
+222
-5
No files matched your search
Generated
+2
-1
@@ -590,6 +590,7 @@
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "e58e780e1378aa4df5dc705364ff5ae9a4dd1e04"
|
||||
source = "https://github.com/mellium/sasl.git"
|
||||
version = "v0.2.1"
|
||||
|
||||
[solve-meta]
|
||||
@@ -602,6 +603,7 @@
|
||||
"github.com/go-pg/pg/orm",
|
||||
"github.com/golang/protobuf/proto",
|
||||
"github.com/golang/protobuf/ptypes/struct",
|
||||
"github.com/googollee/go-engine.io",
|
||||
"github.com/googollee/go-socket.io",
|
||||
"github.com/grpc-ecosystem/grpc-gateway/runtime",
|
||||
"github.com/grpc-ecosystem/grpc-gateway/utilities",
|
||||
@@ -613,7 +615,6 @@
|
||||
"github.com/integration-system/isp-lib/database",
|
||||
"github.com/integration-system/isp-lib/logger",
|
||||
"github.com/integration-system/isp-lib/metric",
|
||||
"github.com/integration-system/isp-lib/socket",
|
||||
"github.com/integration-system/isp-lib/structure",
|
||||
"github.com/integration-system/isp-lib/utils",
|
||||
"github.com/valyala/fasthttp",
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/googollee/go-socket.io"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SocketConn struct {
|
||||
socketio.Socket
|
||||
EstablishedAt time.Time
|
||||
}
|
||||
|
||||
type roomStats struct {
|
||||
m map[string]map[string]*SocketConn
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func NewRoomStats() *roomStats {
|
||||
return &roomStats{
|
||||
m: make(map[string]map[string]*SocketConn),
|
||||
}
|
||||
}
|
||||
|
||||
func (rs *roomStats) Join(room string, socket socketio.Socket) error {
|
||||
rs.Lock()
|
||||
sockets, ok := rs.m[room]
|
||||
if !ok {
|
||||
sockets = make(map[string]*SocketConn)
|
||||
}
|
||||
sockets[socket.Id()] = &SocketConn{socket, time.Now()}
|
||||
rs.m[room] = sockets
|
||||
rs.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *roomStats) GetConnection(instanceId, sockId string) (*SocketConn, bool) {
|
||||
rs.RLock()
|
||||
defer rs.RUnlock()
|
||||
|
||||
if m, ok := rs.m[":"+instanceId]; !ok {
|
||||
return nil, false
|
||||
} else if s, ok := m[sockId]; !ok {
|
||||
return nil, false
|
||||
} else {
|
||||
return s, true
|
||||
}
|
||||
}
|
||||
|
||||
func (rs *roomStats) Leave(room string, socket socketio.Socket) error {
|
||||
rs.Lock()
|
||||
defer rs.Unlock()
|
||||
sockets, ok := rs.m[room]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
delete(sockets, socket.Id())
|
||||
if len(sockets) == 0 {
|
||||
delete(rs.m, room)
|
||||
return nil
|
||||
}
|
||||
rs.m[room] = sockets
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *roomStats) RoomCount(room string) map[string]*SocketConn {
|
||||
rs.RLock()
|
||||
defer rs.RUnlock()
|
||||
return rs.m[room]
|
||||
}
|
||||
|
||||
func (rs *roomStats) RoomsCount() map[string]map[string]int {
|
||||
rs.RLock()
|
||||
defer rs.RUnlock()
|
||||
|
||||
result := map[string]map[string]int{}
|
||||
for room, value := range rs.m {
|
||||
instanceId := room[1:37]
|
||||
module := room[37:]
|
||||
if module == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := result[instanceId]; !ok {
|
||||
result[instanceId] = map[string]int{module: len(value)}
|
||||
} else {
|
||||
result[instanceId][module] = len(value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (rs *roomStats) GetModuleConnectionMap() map[string]map[string][]string {
|
||||
rs.RLock()
|
||||
defer rs.RUnlock()
|
||||
|
||||
result := make(map[string]map[string][]string)
|
||||
for room, value := range rs.m {
|
||||
instanceId := room[1:37]
|
||||
module := room[37:]
|
||||
if module == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := result[instanceId]; !ok {
|
||||
result[instanceId] = make(map[string][]string)
|
||||
}
|
||||
for sockId := range value {
|
||||
if arr, ok := result[instanceId][module]; !ok {
|
||||
result[instanceId][module] = []string{sockId}
|
||||
} else {
|
||||
result[instanceId][module] = append(arr, sockId)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (rs *roomStats) Send(ignore socketio.Socket, room, event string, args ...interface{}) error {
|
||||
rs.RLock()
|
||||
sockets := rs.m[room]
|
||||
for id, s := range sockets {
|
||||
if ignore != nil && ignore.Id() == id {
|
||||
continue
|
||||
}
|
||||
s.Emit(event, args...)
|
||||
}
|
||||
rs.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *roomStats) Len(room string) int {
|
||||
return len(rs.m[room])
|
||||
}
|
||||
|
||||
func (rs *roomStats) RemoveSocketConn(sockId string) {
|
||||
rs.Lock()
|
||||
|
||||
for room, store := range rs.m {
|
||||
delete(store, sockId)
|
||||
if len(store) == 0 {
|
||||
delete(rs.m, room)
|
||||
}
|
||||
}
|
||||
|
||||
rs.Unlock()
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/googollee/go-engine.io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type StoreOption func(ss *sessionsStore)
|
||||
|
||||
type sessionsStore struct {
|
||||
sessions map[string]engineio.Conn
|
||||
locker sync.RWMutex
|
||||
|
||||
onRemove func(id string)
|
||||
onSet func(id string, conn engineio.Conn)
|
||||
}
|
||||
|
||||
func (s *sessionsStore) Get(id string) engineio.Conn {
|
||||
s.locker.RLock()
|
||||
defer s.locker.RUnlock()
|
||||
|
||||
ret, ok := s.sessions[id]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (s *sessionsStore) Set(id string, conn engineio.Conn) {
|
||||
s.locker.Lock()
|
||||
defer s.locker.Unlock()
|
||||
|
||||
s.sessions[id] = conn
|
||||
|
||||
if s.onSet != nil {
|
||||
s.onSet(id, conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sessionsStore) Remove(id string) {
|
||||
s.locker.Lock()
|
||||
defer s.locker.Unlock()
|
||||
|
||||
delete(s.sessions, id)
|
||||
|
||||
if s.onRemove != nil {
|
||||
s.onRemove(id)
|
||||
}
|
||||
}
|
||||
|
||||
func NewSessionStore(opts ...StoreOption) engineio.Sessions {
|
||||
ss := &sessionsStore{sessions: make(map[string]engineio.Conn)}
|
||||
|
||||
for _, o := range opts {
|
||||
o(ss)
|
||||
}
|
||||
|
||||
return ss
|
||||
}
|
||||
|
||||
func WithOnRemoveCallback(f func(id string)) StoreOption {
|
||||
return func(ss *sessionsStore) {
|
||||
ss.onRemove = f
|
||||
}
|
||||
}
|
||||
|
||||
func WithOnSetCallback(f func(id string, conn engineio.Conn)) StoreOption {
|
||||
return func(ss *sessionsStore) {
|
||||
ss.onSet = f
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -5,7 +5,6 @@ import (
|
||||
"github.com/googollee/go-socket.io"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
"github.com/integration-system/isp-lib/logger"
|
||||
sck "github.com/integration-system/isp-lib/socket"
|
||||
str "github.com/integration-system/isp-lib/structure"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
"isp-config-service/conf"
|
||||
@@ -17,7 +16,7 @@ import (
|
||||
var (
|
||||
server *socketio.Server
|
||||
br *broadcaster
|
||||
socketRoomStats = sck.NewRoomStats()
|
||||
socketRoomStats = service.NewRoomStats()
|
||||
backendRoutes = &service.RouterTables{}
|
||||
ConfigServiceBackendDeclaration = &str.BackendDeclaration{}
|
||||
)
|
||||
@@ -27,7 +26,7 @@ func init() {
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
sessionStore := sck.NewSessionStore(sck.WithOnRemoveCallback(func(id string) {
|
||||
sessionStore := service.NewSessionStore(service.WithOnRemoveCallback(func(id string) {
|
||||
socketRoomStats.RemoveSocketConn(id)
|
||||
}))
|
||||
newServer.SetSessionManager(sessionStore)
|
||||
@@ -76,7 +75,7 @@ func GetModuleConnectionIdMapByInstanceId(instanceId string) map[string][]string
|
||||
return socketRoomStats.GetModuleConnectionMap()[instanceId]
|
||||
}
|
||||
|
||||
func GetConnectionById(instanceId, sockId string) (*sck.SocketConn, bool) {
|
||||
func GetConnectionById(instanceId, sockId string) (*service.SocketConn, bool) {
|
||||
return socketRoomStats.GetConnection(instanceId, sockId)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user