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(
og.GRPCClientTypeKey, _clientType.(og.ClientType).String(),
og.GRPCSessionIDKey, _sessionID.(string),
og.GRPCTargetIDKey, _targetID.(string),
)
}
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 {
logging.Logger().Errore(err)
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 {
logging.Logger().Errore(err)
2018-04-09 16:46:48 +00:00
s.writeError(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 {
logging.Logger().Errore(err)
s.writeError(crp.E_INTERNAL, "", err)
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
func (s *RPCServlets) writeError(src crp.ServerRequestCodec, writeChan chan<- []byte, code int, message string, data interface{}) {
if !src.HasResponse() {
return
}
pErr := &crp.Error{
Code: code,
Message: message,
Data: data,
}
buf, err := src.NewResponse(nil, pErr)
if nil != err {
logging.Logger().Errore(err)
return
}
writeChan <- buf
}