gateway_rpc/servlet/rpc-servlet.go

128 lines
2.8 KiB
Go
Raw Normal View History

2018-04-06 12:44:24 +00:00
package servlet
import (
2018-04-09 16:02:35 +00:00
"context"
logging "git.loafle.net/commons/logging-go"
crp "git.loafle.net/commons/rpc-go/protocol"
2018-04-06 12:44:24 +00:00
crpj "git.loafle.net/commons/rpc-go/protocol/json"
"git.loafle.net/commons/server-go"
2018-04-09 16:02:35 +00:00
cssw "git.loafle.net/commons/server-go/socket/web"
og "git.loafle.net/overflow/gateway"
2018-04-06 12:44:24 +00:00
ogeg "git.loafle.net/overflow/gateway/external/grpc"
2018-04-09 16:02:35 +00:00
"github.com/valyala/fasthttp"
2018-04-09 17:04:28 +00:00
"google.golang.org/grpc/metadata"
2018-04-06 12:44:24 +00:00
)
type RPCServlet interface {
cssw.Servlet
2018-04-06 12:57:41 +00:00
}
2018-04-06 12:44:24 +00:00
type RPCServlets struct {
cssw.Servlets
}
2018-04-09 16:02:35 +00:00
func init() {
2018-04-06 12:44:24 +00:00
// member RSA file read
}
func (s *RPCServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
// member auth token extraction
// auth token parse
// auth token valid
// servletCtx set member
return nil, nil
}
2018-04-09 16:02:35 +00:00
func (s *RPCServlets) Handle(servletCtx server.ServletCtx,
stopChan <-chan struct{}, doneChan chan<- struct{},
readChan <-chan []byte, writeChan chan<- []byte) {
2018-04-06 12:44:24 +00:00
defer func() {
doneChan <- struct{}{}
}()
2018-04-09 16:02:35 +00:00
var (
2018-04-09 17:04:28 +00:00
md metadata.MD
2018-04-09 16:02:35 +00:00
src crp.ServerRequestCodec
method string
params []string
grpcCtx context.Context
grpcReply string
replyBuff []byte
err error
)
2018-04-09 17:04:28 +00:00
sc := crpj.NewServerCodec()
_clientType := servletCtx.GetAttribute(og.SessionClientTypeKey)
_sessionID := servletCtx.GetAttribute(og.SessionIDKey)
_targetID := servletCtx.GetAttribute(og.SessionTargetIDKey)
if nil != _clientType && nil != _sessionID && nil != _targetID {
md = metadata.Pairs(
2018-04-10 03:53:46 +00:00
og.GRPCClientTypeKey.String(), _clientType.(og.ClientType).String(),
og.GRPCSessionIDKey.String(), _sessionID.(string),
og.GRPCTargetIDKey.String(), _targetID.(string),
2018-04-09 17:04:28 +00:00
)
}
servletCtx.SetAttribute(og.SessionWriteChanKey, writeChan)
2018-04-06 12:44:24 +00:00
for {
select {
case msg, ok := <-readChan:
if !ok {
return
}
// grpc exec method call
2018-04-09 16:02:35 +00:00
src, err = sc.NewRequest(msg)
if nil != err {
2018-04-10 03:53:46 +00:00
logging.Logger().Error(err)
2018-04-09 16:02:35 +00:00
break
}
2018-04-06 12:44:24 +00:00
2018-04-09 16:02:35 +00:00
method = src.Method()
params, err = src.Params()
if nil != err {
2018-04-10 03:53:46 +00:00
logging.Logger().Error(err)
s.writeError(src, writeChan, crp.E_BAD_PARAMS, "", err)
2018-04-09 16:02:35 +00:00
break
}
2018-04-06 12:44:24 +00:00
2018-04-09 17:04:28 +00:00
grpcCtx = metadata.NewOutgoingContext(context.Background(), md)
2018-04-09 16:46:48 +00:00
grpcReply, err = ogeg.Exec(grpcCtx, method, params...)
2018-04-09 16:02:35 +00:00
replyBuff, err = src.NewResponseWithString(grpcReply, err)
2018-04-09 16:46:48 +00:00
if nil != err {
2018-04-10 03:53:46 +00:00
logging.Logger().Error(err)
s.writeError(src, writeChan, crp.E_INTERNAL, "", err)
2018-04-09 16:46:48 +00:00
break
}
2018-04-06 12:44:24 +00:00
2018-04-09 16:02:35 +00:00
writeChan <- replyBuff
2018-04-06 12:44:24 +00:00
case <-stopChan:
return
}
}
2018-04-09 16:02:35 +00:00
}
2018-04-09 16:46:48 +00:00
2018-04-10 03:53:46 +00:00
func (s *RPCServlets) writeError(src crp.ServerRequestCodec, writeChan chan<- []byte, code crp.ErrorCode, message string, data interface{}) {
2018-04-09 16:46:48 +00:00
if !src.HasResponse() {
return
}
pErr := &crp.Error{
Code: code,
Message: message,
Data: data,
}
buf, err := src.NewResponse(nil, pErr)
if nil != err {
2018-04-10 03:53:46 +00:00
logging.Logger().Error(err)
2018-04-09 16:46:48 +00:00
return
}
writeChan <- buf
}