This commit is contained in:
geek 2018-04-13 18:07:14 +09:00
parent 9a50b95275
commit eaa0c12de7
2 changed files with 20 additions and 47 deletions

View File

@ -1,7 +1,7 @@
#!/bin/bash
rm ./dist
CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o ./_docker/bin/member_gateway_rpc
CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o ./dist/member_gateway_rpc
docker build -t docker.loafle.net/overflow/member_gateway_rpc:1.0.0 .

View File

@ -12,6 +12,8 @@ import (
ogs "git.loafle.net/overflow/gateway/subscribe"
ogrs "git.loafle.net/overflow/gateway_rpc/servlet"
"git.loafle.net/overflow/member_gateway_rpc/subscribe"
"github.com/dgrijalva/jwt-go"
"github.com/satori/go.uuid"
"github.com/valyala/fasthttp"
@ -27,7 +29,7 @@ type WebappServlets struct {
VerifyKey *rsa.PublicKey
SignKey *rsa.PrivateKey
connections sync.Map
sessions sync.Map
}
func (s *WebappServlets) Init(serverCtx server.ServerCtx) error {
@ -103,7 +105,7 @@ func (s *WebappServlets) OnConnect(servletCtx server.ServletCtx, conn socket.Con
sessionID := servletCtx.GetAttribute(og.SessionIDKey)
targetID := servletCtx.GetAttribute(og.SessionTargetIDKey)
if nil != sessionID && nil != targetID {
s.connections.Store(sessionID.(string), retainConnection(targetID.(string), servletCtx))
s.sessions.Store(sessionID.(string), ogrs.RetainSession(targetID.(string), servletCtx))
}
}
@ -112,7 +114,7 @@ func (s *WebappServlets) OnDisconnect(servletCtx server.ServletCtx) {
sessionID := servletCtx.GetAttribute(og.SessionIDKey)
if nil != sessionID {
s.connections.Delete(sessionID.(string))
s.sessions.Delete(sessionID.(string))
}
}
@ -126,13 +128,13 @@ func (s *WebappServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeCh
switch msg.TargetType {
case ogs.MEMBER:
for _, targetID := range msg.Targets {
_connections := s.getMemberConnections(targetID)
if nil == _connections || 0 == len(_connections) {
_sessions := s.getMemberSessions(targetID)
if nil == _sessions || 0 == len(_sessions) {
break
}
for _, _connection := range _connections {
_writeChan := _connection.servletCtx.GetAttribute(og.SessionWriteChanKey)
for _, _session := range _sessions {
_writeChan := _session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
if nil != _writeChan {
writeChan := _writeChan.(chan<- []byte)
writeChan <- msg.Message
@ -142,13 +144,13 @@ func (s *WebappServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeCh
case ogs.MEMBER_SESSION:
for _, sessionID := range msg.Targets {
__connection, ok := s.connections.Load(sessionID)
_session, ok := s.sessions.Load(sessionID)
if !ok {
logging.Logger().Debugf("Client[%s] is not exist", sessionID)
break
}
_connection := __connection.(*connection)
_writeChan := _connection.servletCtx.GetAttribute(og.SessionWriteChanKey)
session := _session.(*ogrs.Session)
_writeChan := session.ServletCtx.GetAttribute(og.SessionWriteChanKey)
if nil != _writeChan {
writeChan := _writeChan.(chan<- []byte)
writeChan <- msg.Message
@ -161,45 +163,16 @@ func (s *WebappServlets) handleSubscribe(serverCtx server.ServerCtx, subscribeCh
}
}
func (s *WebappServlets) getMemberConnections(targetID string) []*connection {
var connections []*connection
func (s *WebappServlets) getMemberSessions(targetID string) []*ogrs.Session {
var sessions []*ogrs.Session
s.connections.Range(func(k, v interface{}) bool {
_connection := v.(*connection)
if _connection.targetID == targetID {
connections = append(connections, _connection)
s.sessions.Range(func(k, v interface{}) bool {
session := v.(*ogrs.Session)
if session.TargetID == targetID {
sessions = append(sessions, session)
}
return true
})
return connections
}
type connection struct {
targetID string
servletCtx server.ServletCtx
}
var connectionPool sync.Pool
func retainConnection(targetID string, servletCtx server.ServletCtx) *connection {
v := connectionPool.Get()
var _connection *connection
if v == nil {
_connection = &connection{}
} else {
_connection = v.(*connection)
}
_connection.targetID = targetID
_connection.servletCtx = servletCtx
return _connection
}
func releaseConnection(_connection *connection) {
_connection.targetID = ""
_connection.servletCtx = nil
connectionPool.Put(_connection)
return sessions
}