2015-01-14 01:31:27 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"sync"
|
|
|
|
|
2015-05-23 12:53:40 +02:00
|
|
|
c "github.com/myodc/go-micro/context"
|
2015-05-20 23:57:19 +02:00
|
|
|
"github.com/myodc/go-micro/transport"
|
2015-05-23 12:53:40 +02:00
|
|
|
|
|
|
|
log "github.com/golang/glog"
|
2015-01-14 01:31:27 +02:00
|
|
|
rpc "github.com/youtube/vitess/go/rpcplus"
|
|
|
|
js "github.com/youtube/vitess/go/rpcplus/jsonrpc"
|
|
|
|
pb "github.com/youtube/vitess/go/rpcplus/pbrpc"
|
2015-05-23 12:53:40 +02:00
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
"golang.org/x/net/context"
|
2015-01-14 01:31:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type RpcServer struct {
|
2015-05-21 20:24:57 +02:00
|
|
|
mtx sync.RWMutex
|
|
|
|
address string
|
|
|
|
opts options
|
|
|
|
rpc *rpc.Server
|
|
|
|
exit chan chan error
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
HealthPath = "/_status/health"
|
|
|
|
RpcPath = "/_rpc"
|
|
|
|
)
|
|
|
|
|
2015-05-21 22:08:19 +02:00
|
|
|
func (s *RpcServer) accept(sock transport.Socket) {
|
|
|
|
var msg transport.Message
|
|
|
|
if err := sock.Recv(&msg); err != nil {
|
2015-01-14 01:31:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
rbq := bytes.NewBuffer(msg.Body)
|
2015-01-14 01:31:27 +02:00
|
|
|
rsp := bytes.NewBuffer(nil)
|
|
|
|
defer rsp.Reset()
|
|
|
|
defer rbq.Reset()
|
|
|
|
|
|
|
|
buf := &buffer{
|
|
|
|
rbq,
|
|
|
|
rsp,
|
|
|
|
}
|
|
|
|
|
|
|
|
var cc rpc.ServerCodec
|
2015-05-20 23:57:19 +02:00
|
|
|
switch msg.Header["Content-Type"] {
|
2015-01-14 01:31:27 +02:00
|
|
|
case "application/octet-stream":
|
|
|
|
cc = pb.NewServerCodec(buf)
|
|
|
|
case "application/json":
|
|
|
|
cc = js.NewServerCodec(buf)
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-23 12:53:40 +02:00
|
|
|
// strip our headers
|
|
|
|
ct := msg.Header["Content-Type"]
|
|
|
|
delete(msg.Header, "Content-Type")
|
|
|
|
|
|
|
|
ctx := c.WithMetaData(context.Background(), msg.Header)
|
|
|
|
|
|
|
|
if err := s.rpc.ServeRequestWithContext(ctx, cc); err != nil {
|
2015-01-14 01:31:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-21 22:08:19 +02:00
|
|
|
sock.Send(&transport.Message{
|
|
|
|
Header: map[string]string{
|
2015-05-23 12:53:40 +02:00
|
|
|
"Content-Type": ct,
|
2015-05-21 22:08:19 +02:00
|
|
|
},
|
|
|
|
Body: rsp.Bytes(),
|
|
|
|
})
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) Address() string {
|
|
|
|
s.mtx.RLock()
|
|
|
|
address := s.address
|
|
|
|
s.mtx.RUnlock()
|
|
|
|
return address
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) NewReceiver(handler interface{}) Receiver {
|
|
|
|
return newRpcReceiver("", handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) NewNamedReceiver(name string, handler interface{}) Receiver {
|
|
|
|
return newRpcReceiver(name, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) Register(r Receiver) error {
|
|
|
|
if len(r.Name()) > 0 {
|
|
|
|
s.rpc.RegisterName(r.Name(), r.Handler())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rpc.Register(r.Handler())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) Start() error {
|
2015-05-22 00:06:01 +02:00
|
|
|
registerHealthChecker(s)
|
2015-01-14 01:31:27 +02:00
|
|
|
|
2015-05-21 22:08:19 +02:00
|
|
|
ts, err := s.opts.transport.Listen(s.address)
|
2015-01-14 01:31:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
log.Infof("Listening on %s", ts.Addr())
|
2015-01-14 01:31:27 +02:00
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
s.mtx.RLock()
|
|
|
|
s.address = ts.Addr()
|
|
|
|
s.mtx.RUnlock()
|
2015-05-16 01:34:02 +02:00
|
|
|
|
2015-05-21 22:08:19 +02:00
|
|
|
go ts.Accept(s.accept)
|
2015-01-14 01:31:27 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
ch := <-s.exit
|
2015-05-20 23:57:19 +02:00
|
|
|
ch <- ts.Close()
|
2015-01-14 01:31:27 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) Stop() error {
|
|
|
|
ch := make(chan error)
|
|
|
|
s.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
2015-05-21 20:28:57 +02:00
|
|
|
func NewRpcServer(address string, opt ...Option) *RpcServer {
|
2015-05-21 20:24:57 +02:00
|
|
|
var opts options
|
|
|
|
|
|
|
|
for _, o := range opt {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.transport == nil {
|
|
|
|
opts.transport = transport.DefaultTransport
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:31:27 +02:00
|
|
|
return &RpcServer{
|
2015-05-21 20:24:57 +02:00
|
|
|
opts: opts,
|
|
|
|
address: address,
|
|
|
|
rpc: rpc.NewServer(),
|
|
|
|
exit: make(chan chan error),
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
}
|