2015-01-14 02:31:27 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-05-23 13:53:40 +03:00
|
|
|
c "github.com/myodc/go-micro/context"
|
2015-05-21 00:57:19 +03:00
|
|
|
"github.com/myodc/go-micro/transport"
|
2015-05-23 13:53:40 +03:00
|
|
|
|
|
|
|
log "github.com/golang/glog"
|
2015-01-14 02:31:27 +03:00
|
|
|
rpc "github.com/youtube/vitess/go/rpcplus"
|
2015-05-23 13:53:40 +03:00
|
|
|
|
2015-05-21 00:57:19 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
type rpcServer struct {
|
2015-05-27 00:39:48 +03:00
|
|
|
opts options
|
|
|
|
rpc *rpc.Server
|
|
|
|
exit chan chan error
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func newRpcServer(opts ...Option) Server {
|
2015-05-23 19:40:53 +03:00
|
|
|
return &rpcServer{
|
2015-05-27 00:39:48 +03:00
|
|
|
opts: newOptions(opts...),
|
|
|
|
rpc: rpc.NewServer(),
|
|
|
|
exit: make(chan chan error),
|
2015-05-23 19:40:53 +03:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
func (s *rpcServer) accept(sock transport.Socket) {
|
2015-05-21 23:08:19 +03:00
|
|
|
var msg transport.Message
|
|
|
|
if err := sock.Recv(&msg); err != nil {
|
2015-01-14 02:31:27 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
codec := newRpcPlusCodec(&msg, sock)
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-23 13:53:40 +03:00
|
|
|
// strip our headers
|
2015-06-01 20:55:27 +03:00
|
|
|
hdr := make(map[string]string)
|
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr[k] = v
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
delete(hdr, "Content-Type")
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
ctx := c.WithMetadata(context.Background(), hdr)
|
|
|
|
s.rpc.ServeRequestWithContext(ctx, codec)
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func (s *rpcServer) Config() options {
|
|
|
|
return s.opts
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func (s *rpcServer) Init(opts ...Option) {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&s.opts)
|
|
|
|
}
|
|
|
|
if len(s.opts.id) == 0 {
|
|
|
|
s.opts.id = s.opts.name + "-" + DefaultId
|
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
func (s *rpcServer) NewReceiver(handler interface{}) Receiver {
|
2015-01-14 02:31:27 +03:00
|
|
|
return newRpcReceiver("", handler)
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
func (s *rpcServer) NewNamedReceiver(name string, handler interface{}) Receiver {
|
2015-01-14 02:31:27 +03:00
|
|
|
return newRpcReceiver(name, handler)
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
func (s *rpcServer) Register(r Receiver) error {
|
2015-01-14 02:31:27 +03:00
|
|
|
if len(r.Name()) > 0 {
|
|
|
|
s.rpc.RegisterName(r.Name(), r.Handler())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rpc.Register(r.Handler())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
func (s *rpcServer) Start() error {
|
2015-05-22 01:06:01 +03:00
|
|
|
registerHealthChecker(s)
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
ts, err := s.opts.transport.Listen(s.opts.address)
|
2015-01-14 02:31:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-21 00:57:19 +03:00
|
|
|
log.Infof("Listening on %s", ts.Addr())
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
s.opts.address = ts.Addr()
|
2015-05-16 02:34:02 +03:00
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
go ts.Accept(s.accept)
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
ch := <-s.exit
|
2015-05-21 00:57:19 +03:00
|
|
|
ch <- ts.Close()
|
2015-01-14 02:31:27 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:40:53 +03:00
|
|
|
func (s *rpcServer) Stop() error {
|
2015-01-14 02:31:27 +03:00
|
|
|
ch := make(chan error)
|
|
|
|
s.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|