probe_gateway_rpc/servlet/data-servlet.go

146 lines
3.2 KiB
Go
Raw Normal View History

2018-04-12 12:07:02 +00:00
package servlet
import (
2018-04-13 07:18:50 +00:00
"context"
2018-04-13 07:21:17 +00:00
"fmt"
2018-04-13 07:18:50 +00:00
"github.com/valyala/fasthttp"
2018-05-08 11:12:08 +00:00
logging "git.loafle.net/commons/logging-go"
crp "git.loafle.net/commons/rpc-go/protocol"
2018-04-13 07:18:50 +00:00
"git.loafle.net/commons/server-go"
"git.loafle.net/commons/server-go/socket"
2018-05-08 11:12:08 +00:00
cssw "git.loafle.net/commons/server-go/socket/web"
2018-04-26 08:17:12 +00:00
occp "git.loafle.net/overflow/commons-go/config/probe"
2018-04-13 07:21:17 +00:00
og "git.loafle.net/overflow/gateway"
"git.loafle.net/overflow/gateway/external/grpc"
2018-05-08 11:12:08 +00:00
"git.loafle.net/overflow/gateway/external/kafka"
2018-04-12 12:07:02 +00:00
)
2018-04-13 07:18:50 +00:00
type DataServlet interface {
2018-05-08 11:12:08 +00:00
cssw.Servlet
2018-04-13 07:18:50 +00:00
}
type DataServlets struct {
2018-05-08 11:12:08 +00:00
cssw.Servlets
2018-04-13 07:18:50 +00:00
2018-05-08 11:12:08 +00:00
RPCServerCodec crp.ServerCodec
2018-04-13 07:18:50 +00:00
}
func (s *DataServlets) Init(serverCtx server.ServerCtx) error {
2018-05-08 11:12:08 +00:00
if err := s.Servlets.Init(serverCtx); nil != err {
2018-04-13 07:18:50 +00:00
return err
}
return nil
}
func (s *DataServlets) OnStart(serverCtx server.ServerCtx) error {
2018-05-08 11:12:08 +00:00
if err := s.Servlets.OnStart(serverCtx); nil != err {
2018-04-13 07:18:50 +00:00
return err
}
return nil
}
func (s *DataServlets) OnStop(serverCtx server.ServerCtx) {
2018-05-08 11:12:08 +00:00
s.Servlets.OnStop(serverCtx)
2018-04-13 07:18:50 +00:00
}
func (s *DataServlets) Destroy(serverCtx server.ServerCtx) {
2018-05-08 11:12:08 +00:00
s.Servlets.Destroy(serverCtx)
2018-04-13 07:18:50 +00:00
}
func (s *DataServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
// probe key extraction
2018-04-26 08:17:12 +00:00
bMethod := ctx.Request.Header.Peek(occp.HTTPRequestHeaderKey_Probe_Method)
2018-04-13 07:18:50 +00:00
if nil == bMethod {
return nil, fmt.Errorf("Unexpected probe method: %v", bMethod)
}
method := string(bMethod)
switch method {
2018-04-26 08:17:12 +00:00
case occp.HTTPRequestHeaderValue_Probe_Method_Connect:
2018-04-13 07:18:50 +00:00
default:
2018-04-13 07:21:17 +00:00
return nil, fmt.Errorf("Unexpected probe method: %s", method)
2018-04-13 07:18:50 +00:00
}
2018-04-26 08:17:12 +00:00
bProbeKey := ctx.Request.Header.Peek(occp.HTTPRequestHeaderKey_Probe_ProbeKey)
2018-04-13 07:18:50 +00:00
if nil == bProbeKey {
return nil, fmt.Errorf("Unexpected probe key : %v", bProbeKey)
}
probeKey := string(bProbeKey)
grpcCTX := context.Background()
2018-04-17 14:10:13 +00:00
_, err := grpc.Exec(grpcCTX, "ProbeService.readByProbeKey", probeKey)
2018-04-13 07:18:50 +00:00
if nil != err {
return nil, fmt.Errorf("grpc call Error: %s", err.Error())
}
2018-04-17 14:10:13 +00:00
servletCtx.SetAttribute(og.SessionIDKey, probeKey)
2018-04-13 07:18:50 +00:00
servletCtx.SetAttribute(og.SessionClientTypeKey, og.PROBE)
2018-04-17 14:10:13 +00:00
servletCtx.SetAttribute(og.SessionTargetIDKey, probeKey)
2018-04-13 07:18:50 +00:00
2018-04-17 14:10:13 +00:00
return nil, nil
2018-04-13 07:18:50 +00:00
}
func (s *DataServlets) OnConnect(servletCtx server.ServletCtx, conn socket.Conn) {
2018-05-08 11:12:08 +00:00
s.Servlets.OnConnect(servletCtx, conn)
2018-04-13 07:18:50 +00:00
}
func (s *DataServlets) OnDisconnect(servletCtx server.ServletCtx) {
2018-05-08 11:12:08 +00:00
s.Servlets.OnDisconnect(servletCtx)
}
func (s *DataServlets) Handle(servletCtx server.ServletCtx,
stopChan <-chan struct{}, doneChan chan<- struct{},
readChan <-chan []byte, writeChan chan<- []byte) {
defer func() {
doneChan <- struct{}{}
}()
var (
src crp.ServerRequestCodec
params []string
err error
)
for {
select {
case msg, ok := <-readChan:
if !ok {
return
}
src, err = s.RPCServerCodec.NewRequest(msg)
if nil != err {
logging.Logger().Error(err)
break
}
params, err = src.Params()
if nil != err {
logging.Logger().Error(err)
break
}
if nil == params || 2 != len(params) {
logging.Logger().Errorf("metric is not valid %v", params)
break
}
err = kafka.Write("metric", []byte(params[0]), []byte(params[1]))
if nil != err {
logging.Logger().Error(err)
break
}
case <-stopChan:
return
}
}
2018-04-13 07:21:17 +00:00
}