For the legacy
This commit is contained in:
parent
9ce9977d21
commit
2cd2258731
@ -38,7 +38,9 @@ type Message interface {
|
|||||||
type Request interface {
|
type Request interface {
|
||||||
// The service to call
|
// The service to call
|
||||||
Service() string
|
Service() string
|
||||||
// The endpoint to call
|
// The action to take
|
||||||
|
Method() string
|
||||||
|
// The endpoint to invoke
|
||||||
Endpoint() string
|
Endpoint() string
|
||||||
// The content type
|
// The content type
|
||||||
ContentType() string
|
ContentType() string
|
||||||
|
@ -104,6 +104,7 @@ func (c *rpcCodec) Write(m *codec.Message, body interface{}) error {
|
|||||||
// set the mucp headers
|
// set the mucp headers
|
||||||
m.Header["X-Micro-Id"] = m.Id
|
m.Header["X-Micro-Id"] = m.Id
|
||||||
m.Header["X-Micro-Service"] = m.Target
|
m.Header["X-Micro-Service"] = m.Target
|
||||||
|
m.Header["X-Micro-Method"] = m.Method
|
||||||
m.Header["X-Micro-Endpoint"] = m.Endpoint
|
m.Header["X-Micro-Endpoint"] = m.Endpoint
|
||||||
|
|
||||||
// if body is bytes Frame don't encode
|
// if body is bytes Frame don't encode
|
||||||
@ -154,6 +155,7 @@ func (c *rpcCodec) ReadHeader(wm *codec.Message, r codec.MessageType) error {
|
|||||||
// read header
|
// read header
|
||||||
err := c.codec.ReadHeader(&me, r)
|
err := c.codec.ReadHeader(&me, r)
|
||||||
wm.Endpoint = me.Endpoint
|
wm.Endpoint = me.Endpoint
|
||||||
|
wm.Method = me.Method
|
||||||
wm.Id = me.Id
|
wm.Id = me.Id
|
||||||
wm.Error = me.Error
|
wm.Error = me.Error
|
||||||
|
|
||||||
@ -162,11 +164,16 @@ func (c *rpcCodec) ReadHeader(wm *codec.Message, r codec.MessageType) error {
|
|||||||
wm.Error = me.Header["X-Micro-Error"]
|
wm.Error = me.Header["X-Micro-Error"]
|
||||||
}
|
}
|
||||||
|
|
||||||
// check method in header
|
// check endpoint in header
|
||||||
if len(me.Endpoint) == 0 {
|
if len(me.Endpoint) == 0 {
|
||||||
wm.Endpoint = me.Header["X-Micro-Endpoint"]
|
wm.Endpoint = me.Header["X-Micro-Endpoint"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check method in header
|
||||||
|
if len(me.Method) == 0 {
|
||||||
|
wm.Method = me.Header["X-Micro-Method"]
|
||||||
|
}
|
||||||
|
|
||||||
if len(me.Id) == 0 {
|
if len(me.Id) == 0 {
|
||||||
wm.Id = me.Header["X-Micro-Id"]
|
wm.Id = me.Header["X-Micro-Id"]
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
type rpcRequest struct {
|
type rpcRequest struct {
|
||||||
service string
|
service string
|
||||||
|
method string
|
||||||
endpoint string
|
endpoint string
|
||||||
contentType string
|
contentType string
|
||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
@ -27,6 +28,7 @@ func newRequest(service, endpoint string, request interface{}, contentType strin
|
|||||||
|
|
||||||
return &rpcRequest{
|
return &rpcRequest{
|
||||||
service: service,
|
service: service,
|
||||||
|
method: endpoint,
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
body: request,
|
body: request,
|
||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
@ -42,6 +44,10 @@ func (r *rpcRequest) Service() string {
|
|||||||
return r.service
|
return r.service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *rpcRequest) Method() string {
|
||||||
|
return r.method
|
||||||
|
}
|
||||||
|
|
||||||
func (r *rpcRequest) Endpoint() string {
|
func (r *rpcRequest) Endpoint() string {
|
||||||
return r.endpoint
|
return r.endpoint
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,7 @@ func (r *rpcStream) Send(msg interface{}) error {
|
|||||||
req := codec.Message{
|
req := codec.Message{
|
||||||
Id: r.id,
|
Id: r.id,
|
||||||
Target: r.request.Service(),
|
Target: r.request.Service(),
|
||||||
|
Method: r.request.Method(),
|
||||||
Endpoint: r.request.Endpoint(),
|
Endpoint: r.request.Endpoint(),
|
||||||
Type: codec.Request,
|
Type: codec.Request,
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,7 @@ type Message struct {
|
|||||||
Id string
|
Id string
|
||||||
Type MessageType
|
Type MessageType
|
||||||
Target string
|
Target string
|
||||||
|
Method string
|
||||||
Endpoint string
|
Endpoint string
|
||||||
Error string
|
Error string
|
||||||
|
|
||||||
|
@ -45,9 +45,9 @@ func newClientCodec(conn io.ReadWriteCloser) *clientCodec {
|
|||||||
|
|
||||||
func (c *clientCodec) Write(m *codec.Message, b interface{}) error {
|
func (c *clientCodec) Write(m *codec.Message, b interface{}) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
c.pending[m.Id] = m.Endpoint
|
c.pending[m.Id] = m.Method
|
||||||
c.Unlock()
|
c.Unlock()
|
||||||
c.req.Method = m.Endpoint
|
c.req.Method = m.Method
|
||||||
c.req.Params[0] = b
|
c.req.Params[0] = b
|
||||||
c.req.ID = m.Id
|
c.req.ID = m.Id
|
||||||
return c.enc.Encode(&c.req)
|
return c.enc.Encode(&c.req)
|
||||||
@ -66,7 +66,7 @@ func (c *clientCodec) ReadHeader(m *codec.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Lock()
|
c.Lock()
|
||||||
m.Endpoint = c.pending[c.resp.ID]
|
m.Method = c.pending[c.resp.ID]
|
||||||
delete(c.pending, c.resp.ID)
|
delete(c.pending, c.resp.ID)
|
||||||
c.Unlock()
|
c.Unlock()
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ func (c *serverCodec) ReadHeader(m *codec.Message) error {
|
|||||||
if err := c.dec.Decode(&c.req); err != nil {
|
if err := c.dec.Decode(&c.req); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.Endpoint = c.req.Method
|
m.Method = c.req.Method
|
||||||
m.Id = fmt.Sprintf("%v", c.req.ID)
|
m.Id = fmt.Sprintf("%v", c.req.ID)
|
||||||
c.req.ID = nil
|
c.req.ID = nil
|
||||||
return nil
|
return nil
|
||||||
|
@ -47,7 +47,7 @@ func (c *protoCodec) Write(m *codec.Message, b interface{}) error {
|
|||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
// This is protobuf, of course we copy it.
|
// This is protobuf, of course we copy it.
|
||||||
pbr := &Request{ServiceMethod: &m.Endpoint, Seq: id(m.Id)}
|
pbr := &Request{ServiceMethod: &m.Method, Seq: id(m.Id)}
|
||||||
data, err := proto.Marshal(pbr)
|
data, err := proto.Marshal(pbr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -73,7 +73,7 @@ func (c *protoCodec) Write(m *codec.Message, b interface{}) error {
|
|||||||
case codec.Response, codec.Error:
|
case codec.Response, codec.Error:
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
rtmp := &Response{ServiceMethod: &m.Endpoint, Seq: id(m.Id), Error: &m.Error}
|
rtmp := &Response{ServiceMethod: &m.Method, Seq: id(m.Id), Error: &m.Error}
|
||||||
data, err := proto.Marshal(rtmp)
|
data, err := proto.Marshal(rtmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -126,7 +126,7 @@ func (c *protoCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.Endpoint = rtmp.GetServiceMethod()
|
m.Method = rtmp.GetServiceMethod()
|
||||||
m.Id = fmt.Sprintf("%d", rtmp.GetSeq())
|
m.Id = fmt.Sprintf("%d", rtmp.GetSeq())
|
||||||
case codec.Response:
|
case codec.Response:
|
||||||
data, err := ReadNetString(c.rwc)
|
data, err := ReadNetString(c.rwc)
|
||||||
@ -138,7 +138,7 @@ func (c *protoCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
m.Endpoint = rtmp.GetServiceMethod()
|
m.Method = rtmp.GetServiceMethod()
|
||||||
m.Id = fmt.Sprintf("%d", rtmp.GetSeq())
|
m.Id = fmt.Sprintf("%d", rtmp.GetSeq())
|
||||||
m.Error = rtmp.GetError()
|
m.Error = rtmp.GetError()
|
||||||
case codec.Publication:
|
case codec.Publication:
|
||||||
|
@ -41,6 +41,15 @@ var (
|
|||||||
"application/proto-rpc": protorpc.NewCodec,
|
"application/proto-rpc": protorpc.NewCodec,
|
||||||
"application/octet-stream": raw.NewCodec,
|
"application/octet-stream": raw.NewCodec,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remove legacy codec list
|
||||||
|
defaultCodecs = map[string]codec.NewCodec{
|
||||||
|
"application/json": jsonrpc.NewCodec,
|
||||||
|
"application/json-rpc": jsonrpc.NewCodec,
|
||||||
|
"application/protobuf": protorpc.NewCodec,
|
||||||
|
"application/proto-rpc": protorpc.NewCodec,
|
||||||
|
"application/octet-stream": protorpc.NewCodec,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (rwc *readWriteCloser) Read(p []byte) (n int, err error) {
|
func (rwc *readWriteCloser) Read(p []byte) (n int, err error) {
|
||||||
@ -57,6 +66,33 @@ func (rwc *readWriteCloser) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupProtocol sets up the old protocol
|
||||||
|
func setupProtocol(msg *transport.Message) codec.NewCodec {
|
||||||
|
// if the protocol exists do nothing
|
||||||
|
if len(msg.Header["X-Micro-Protocol"]) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// if 0.17 - 0.21
|
||||||
|
if len(msg.Header["X-Micro-Service"]) > 0 {
|
||||||
|
// set method to endpoint
|
||||||
|
if len(msg.Header["X-Micro-Method"]) == 0 {
|
||||||
|
msg.Header["X-Micro-Method"] = msg.Header["X-Micro-Endpoint"]
|
||||||
|
}
|
||||||
|
|
||||||
|
// set endpoint to method
|
||||||
|
if len(msg.Header["X-Micro-Endpoint"]) == 0 {
|
||||||
|
msg.Header["X-Micro-Endpoint"] = msg.Header["X-Micro-Method"]
|
||||||
|
}
|
||||||
|
|
||||||
|
// done
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// old ways
|
||||||
|
return defaultCodecs[msg.Header["Content-Type"]]
|
||||||
|
}
|
||||||
|
|
||||||
func newRpcCodec(req *transport.Message, socket transport.Socket, c codec.NewCodec) codec.Codec {
|
func newRpcCodec(req *transport.Message, socket transport.Socket, c codec.NewCodec) codec.Codec {
|
||||||
rwc := &readWriteCloser{
|
rwc := &readWriteCloser{
|
||||||
rbuf: bytes.NewBuffer(req.Body),
|
rbuf: bytes.NewBuffer(req.Body),
|
||||||
@ -109,6 +145,7 @@ func (c *rpcCodec) ReadHeader(r *codec.Message, t codec.MessageType) error {
|
|||||||
|
|
||||||
// set some internal things
|
// set some internal things
|
||||||
m.Target = m.Header["X-Micro-Service"]
|
m.Target = m.Header["X-Micro-Service"]
|
||||||
|
m.Method = m.Header["X-Micro-Method"]
|
||||||
m.Endpoint = m.Header["X-Micro-Endpoint"]
|
m.Endpoint = m.Header["X-Micro-Endpoint"]
|
||||||
m.Id = m.Header["X-Micro-Id"]
|
m.Id = m.Header["X-Micro-Id"]
|
||||||
|
|
||||||
@ -141,6 +178,8 @@ func (c *rpcCodec) Write(r *codec.Message, b interface{}) error {
|
|||||||
|
|
||||||
// create a new message
|
// create a new message
|
||||||
m := &codec.Message{
|
m := &codec.Message{
|
||||||
|
Target: r.Target,
|
||||||
|
Method: r.Method,
|
||||||
Endpoint: r.Endpoint,
|
Endpoint: r.Endpoint,
|
||||||
Id: r.Id,
|
Id: r.Id,
|
||||||
Error: r.Error,
|
Error: r.Error,
|
||||||
@ -162,6 +201,11 @@ func (c *rpcCodec) Write(r *codec.Message, b interface{}) error {
|
|||||||
m.Header["X-Micro-Service"] = r.Target
|
m.Header["X-Micro-Service"] = r.Target
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set request method
|
||||||
|
if len(r.Method) > 0 {
|
||||||
|
m.Header["X-Micro-Method"] = r.Method
|
||||||
|
}
|
||||||
|
|
||||||
// set request endpoint
|
// set request endpoint
|
||||||
if len(r.Endpoint) > 0 {
|
if len(r.Endpoint) > 0 {
|
||||||
m.Header["X-Micro-Endpoint"] = r.Endpoint
|
m.Header["X-Micro-Endpoint"] = r.Endpoint
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
type rpcRequest struct {
|
type rpcRequest struct {
|
||||||
service string
|
service string
|
||||||
|
method string
|
||||||
endpoint string
|
endpoint string
|
||||||
contentType string
|
contentType string
|
||||||
socket transport.Socket
|
socket transport.Socket
|
||||||
@ -34,6 +35,10 @@ func (r *rpcRequest) Service() string {
|
|||||||
return r.service
|
return r.service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *rpcRequest) Method() string {
|
||||||
|
return r.method
|
||||||
|
}
|
||||||
|
|
||||||
func (r *rpcRequest) Endpoint() string {
|
func (r *rpcRequest) Endpoint() string {
|
||||||
return r.endpoint
|
return r.endpoint
|
||||||
}
|
}
|
||||||
|
@ -189,6 +189,7 @@ func (s *service) call(ctx context.Context, router *router, sending *sync.Mutex,
|
|||||||
r := &rpcRequest{
|
r := &rpcRequest{
|
||||||
service: req.msg.Target,
|
service: req.msg.Target,
|
||||||
contentType: req.msg.Header["Content-Type"],
|
contentType: req.msg.Header["Content-Type"],
|
||||||
|
method: req.msg.Method,
|
||||||
endpoint: req.msg.Endpoint,
|
endpoint: req.msg.Endpoint,
|
||||||
body: req.msg.Body,
|
body: req.msg.Body,
|
||||||
}
|
}
|
||||||
|
@ -97,9 +97,14 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
|
|||||||
ct = DefaultContentType
|
ct = DefaultContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setup old protocol
|
||||||
|
cf := setupProtocol(&msg)
|
||||||
|
|
||||||
|
// no old codec
|
||||||
|
if cf == nil {
|
||||||
// TODO: needs better error handling
|
// TODO: needs better error handling
|
||||||
cf, err := s.newCodec(ct)
|
var err error
|
||||||
if err != nil {
|
if cf, err = s.newCodec(ct); err != nil {
|
||||||
sock.Send(&transport.Message{
|
sock.Send(&transport.Message{
|
||||||
Header: map[string]string{
|
Header: map[string]string{
|
||||||
"Content-Type": "text/plain",
|
"Content-Type": "text/plain",
|
||||||
@ -109,12 +114,14 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
|
|||||||
s.wg.Done()
|
s.wg.Done()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rcodec := newRpcCodec(&msg, sock, cf)
|
rcodec := newRpcCodec(&msg, sock, cf)
|
||||||
|
|
||||||
// internal request
|
// internal request
|
||||||
request := &rpcRequest{
|
request := &rpcRequest{
|
||||||
service: msg.Header["X-Micro-Service"],
|
service: msg.Header["X-Micro-Service"],
|
||||||
|
method: msg.Header["X-Micro-Method"],
|
||||||
endpoint: msg.Header["X-Micro-Endpoint"],
|
endpoint: msg.Header["X-Micro-Endpoint"],
|
||||||
contentType: ct,
|
contentType: ct,
|
||||||
codec: rcodec,
|
codec: rcodec,
|
||||||
|
@ -31,6 +31,8 @@ func (r *rpcStream) Send(msg interface{}) error {
|
|||||||
defer r.Unlock()
|
defer r.Unlock()
|
||||||
|
|
||||||
resp := codec.Message{
|
resp := codec.Message{
|
||||||
|
Target: r.request.Service(),
|
||||||
|
Method: r.request.Method(),
|
||||||
Endpoint: r.request.Endpoint(),
|
Endpoint: r.request.Endpoint(),
|
||||||
Id: r.id,
|
Id: r.id,
|
||||||
Type: codec.Response,
|
Type: codec.Response,
|
||||||
|
@ -45,6 +45,8 @@ type Message interface {
|
|||||||
type Request interface {
|
type Request interface {
|
||||||
// Service name requested
|
// Service name requested
|
||||||
Service() string
|
Service() string
|
||||||
|
// The action requested
|
||||||
|
Method() string
|
||||||
// Endpoint name requested
|
// Endpoint name requested
|
||||||
Endpoint() string
|
Endpoint() string
|
||||||
// Content type provided
|
// Content type provided
|
||||||
|
Loading…
Reference in New Issue
Block a user