2015-01-14 01:31:27 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-06-12 20:52:27 +02:00
|
|
|
"fmt"
|
2015-06-03 02:25:37 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2015-11-20 18:17:33 +02:00
|
|
|
"github.com/micro/go-micro/broker"
|
2015-11-28 13:22:29 +02:00
|
|
|
"github.com/micro/go-micro/codec"
|
2015-11-20 18:17:33 +02:00
|
|
|
c "github.com/micro/go-micro/context"
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
"github.com/micro/go-micro/transport"
|
2015-05-23 12:53:40 +02:00
|
|
|
|
|
|
|
log "github.com/golang/glog"
|
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
"golang.org/x/net/context"
|
2015-01-14 01:31:27 +02:00
|
|
|
)
|
|
|
|
|
2015-05-23 18:40:53 +02:00
|
|
|
type rpcServer struct {
|
2015-12-02 01:32:23 +02:00
|
|
|
rpc *server
|
2015-05-26 23:39:48 +02:00
|
|
|
exit chan chan error
|
2015-06-03 02:25:37 +02:00
|
|
|
|
|
|
|
sync.RWMutex
|
2015-06-12 20:52:27 +02:00
|
|
|
opts options
|
|
|
|
handlers map[string]Handler
|
|
|
|
subscribers map[*subscriber][]broker.Subscriber
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 23:39:48 +02:00
|
|
|
func newRpcServer(opts ...Option) Server {
|
2015-12-02 02:47:52 +02:00
|
|
|
options := newOptions(opts...)
|
2015-05-23 18:40:53 +02:00
|
|
|
return &rpcServer{
|
2015-12-02 02:47:52 +02:00
|
|
|
opts: options,
|
|
|
|
rpc: &server{
|
|
|
|
serviceMap: make(map[string]*service),
|
|
|
|
wrappers: options.wrappers,
|
|
|
|
},
|
2015-06-12 20:52:27 +02:00
|
|
|
handlers: make(map[string]Handler),
|
|
|
|
subscribers: make(map[*subscriber][]broker.Subscriber),
|
|
|
|
exit: make(chan chan error),
|
2015-05-23 18:40:53 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 01:31:27 +02:00
|
|
|
|
2015-05-23 18:40:53 +02:00
|
|
|
func (s *rpcServer) accept(sock transport.Socket) {
|
2015-05-21 22:08:19 +02:00
|
|
|
var msg transport.Message
|
|
|
|
if err := sock.Recv(&msg); err != nil {
|
2015-01-14 01:31:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-28 13:22:29 +02:00
|
|
|
cf, err := s.newCodec(msg.Header["Content-Type"])
|
2015-11-25 21:50:05 +02:00
|
|
|
// TODO: needs better error handling
|
|
|
|
if err != nil {
|
|
|
|
sock.Send(&transport.Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
},
|
|
|
|
Body: []byte(err.Error()),
|
|
|
|
})
|
|
|
|
sock.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
codec := newRpcPlusCodec(&msg, sock, cf)
|
2015-01-14 01:31:27 +02:00
|
|
|
|
2015-05-23 12:53:40 +02:00
|
|
|
// strip our headers
|
2015-06-01 19:55:27 +02:00
|
|
|
hdr := make(map[string]string)
|
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr[k] = v
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
2015-06-01 19:55:27 +02:00
|
|
|
delete(hdr, "Content-Type")
|
2015-01-14 01:31:27 +02:00
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
ctx := c.WithMetadata(context.Background(), hdr)
|
2015-11-24 02:52:59 +02:00
|
|
|
// TODO: needs better error handling
|
|
|
|
if err := s.rpc.ServeRequestWithContext(ctx, codec); err != nil {
|
2015-11-25 21:50:05 +02:00
|
|
|
log.Errorf("Unexpected error serving request, closing socket: %v", err)
|
2015-11-24 02:52:59 +02:00
|
|
|
sock.Close()
|
|
|
|
}
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|
|
|
|
|
2015-11-28 13:22:29 +02:00
|
|
|
func (s *rpcServer) newCodec(contentType string) (codec.NewCodec, error) {
|
2015-11-25 21:50:05 +02:00
|
|
|
if cf, ok := s.opts.codecs[contentType]; ok {
|
2015-11-28 13:22:29 +02:00
|
|
|
return cf, nil
|
2015-11-25 21:50:05 +02:00
|
|
|
}
|
|
|
|
if cf, ok := defaultCodecs[contentType]; ok {
|
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:39:48 +02:00
|
|
|
func (s *rpcServer) Config() options {
|
2015-06-03 02:25:37 +02:00
|
|
|
s.RLock()
|
|
|
|
opts := s.opts
|
|
|
|
s.RUnlock()
|
|
|
|
return opts
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 23:39:48 +02:00
|
|
|
func (s *rpcServer) Init(opts ...Option) {
|
2015-06-03 02:25:37 +02:00
|
|
|
s.Lock()
|
2015-05-26 23:39:48 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&s.opts)
|
|
|
|
}
|
|
|
|
if len(s.opts.id) == 0 {
|
|
|
|
s.opts.id = s.opts.name + "-" + DefaultId
|
|
|
|
}
|
2015-06-03 02:25:37 +02:00
|
|
|
s.Unlock()
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
func (s *rpcServer) NewHandler(h interface{}) Handler {
|
|
|
|
return newRpcHandler(h)
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
func (s *rpcServer) Handle(h Handler) error {
|
|
|
|
if err := s.rpc.Register(h.Handler()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Lock()
|
|
|
|
s.handlers[h.Name()] = h
|
|
|
|
s.Unlock()
|
|
|
|
return nil
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 20:52:27 +02:00
|
|
|
func (s *rpcServer) NewSubscriber(topic string, sb interface{}) Subscriber {
|
|
|
|
return newSubscriber(topic, sb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) Subscribe(sb Subscriber) error {
|
|
|
|
sub, ok := sb.(*subscriber)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid subscriber: expected *subscriber")
|
|
|
|
}
|
|
|
|
if len(sub.handlers) == 0 {
|
|
|
|
return fmt.Errorf("invalid subscriber: no handler functions")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
_, ok = s.subscribers[sub]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("subscriber %v already exists", s)
|
|
|
|
}
|
|
|
|
s.subscribers[sub] = nil
|
|
|
|
s.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
func (s *rpcServer) Register() error {
|
|
|
|
// parse address for host, port
|
|
|
|
config := s.Config()
|
2015-11-11 20:22:04 +02:00
|
|
|
var advt, host string
|
2015-06-03 02:25:37 +02:00
|
|
|
var port int
|
2015-11-04 23:53:39 +02:00
|
|
|
|
2015-11-11 20:22:04 +02:00
|
|
|
// check the advertise address first
|
|
|
|
// if it exists then use it, otherwise
|
|
|
|
// use the address
|
|
|
|
if len(config.Advertise()) > 0 {
|
|
|
|
advt = config.Advertise()
|
|
|
|
} else {
|
|
|
|
advt = config.Address()
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(advt, ":")
|
2015-06-03 02:25:37 +02:00
|
|
|
if len(parts) > 1 {
|
|
|
|
host = strings.Join(parts[:len(parts)-1], ":")
|
|
|
|
port, _ = strconv.Atoi(parts[len(parts)-1])
|
|
|
|
} else {
|
|
|
|
host = parts[0]
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 23:53:39 +02:00
|
|
|
addr, err := extractAddress(host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
// register service
|
|
|
|
node := ®istry.Node{
|
|
|
|
Id: config.Id(),
|
2015-11-04 23:53:39 +02:00
|
|
|
Address: addr,
|
2015-06-03 02:25:37 +02:00
|
|
|
Port: port,
|
|
|
|
Metadata: config.Metadata(),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.RLock()
|
|
|
|
var endpoints []*registry.Endpoint
|
|
|
|
for _, e := range s.handlers {
|
|
|
|
endpoints = append(endpoints, e.Endpoints()...)
|
|
|
|
}
|
2015-06-12 20:52:27 +02:00
|
|
|
for e, _ := range s.subscribers {
|
|
|
|
endpoints = append(endpoints, e.Endpoints()...)
|
|
|
|
}
|
2015-06-03 02:25:37 +02:00
|
|
|
s.RUnlock()
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
|
|
|
Name: config.Name(),
|
|
|
|
Version: config.Version(),
|
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
Endpoints: endpoints,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Registering node: %s", node.Id)
|
2015-06-12 20:52:27 +02:00
|
|
|
if err := config.registry.Register(service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
for sb, _ := range s.subscribers {
|
2015-11-28 13:22:29 +02:00
|
|
|
handler := s.createSubHandler(sb)
|
2015-06-12 20:52:27 +02:00
|
|
|
sub, err := config.broker.Subscribe(sb.Topic(), handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.subscribers[sb] = []broker.Subscriber{sub}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) Deregister() error {
|
|
|
|
config := s.Config()
|
2015-11-11 20:22:04 +02:00
|
|
|
var advt, host string
|
2015-06-03 02:25:37 +02:00
|
|
|
var port int
|
2015-11-11 20:22:04 +02:00
|
|
|
|
|
|
|
// check the advertise address first
|
|
|
|
// if it exists then use it, otherwise
|
|
|
|
// use the address
|
|
|
|
if len(config.Advertise()) > 0 {
|
|
|
|
advt = config.Advertise()
|
|
|
|
} else {
|
|
|
|
advt = config.Address()
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(advt, ":")
|
2015-06-03 02:25:37 +02:00
|
|
|
if len(parts) > 1 {
|
|
|
|
host = strings.Join(parts[:len(parts)-1], ":")
|
|
|
|
port, _ = strconv.Atoi(parts[len(parts)-1])
|
|
|
|
} else {
|
|
|
|
host = parts[0]
|
|
|
|
}
|
|
|
|
|
2015-11-04 23:53:39 +02:00
|
|
|
addr, err := extractAddress(host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
node := ®istry.Node{
|
|
|
|
Id: config.Id(),
|
2015-11-04 23:53:39 +02:00
|
|
|
Address: addr,
|
2015-06-03 02:25:37 +02:00
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
|
|
|
Name: config.Name(),
|
|
|
|
Version: config.Version(),
|
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
|
|
|
|
2015-06-12 20:52:27 +02:00
|
|
|
log.Infof("Deregistering node: %s", node.Id)
|
|
|
|
if err := config.registry.Deregister(service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
for sb, subs := range s.subscribers {
|
|
|
|
for _, sub := range subs {
|
|
|
|
log.Infof("Unsubscribing from topic: %s", sub.Topic())
|
|
|
|
sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
s.subscribers[sb] = nil
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
return nil
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 18:40:53 +02:00
|
|
|
func (s *rpcServer) Start() error {
|
2015-05-22 00:06:01 +02:00
|
|
|
registerHealthChecker(s)
|
2015-06-03 02:25:37 +02:00
|
|
|
config := s.Config()
|
2015-01-14 01:31:27 +02:00
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
ts, err := config.transport.Listen(s.opts.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-06-03 02:25:37 +02:00
|
|
|
s.Lock()
|
2015-05-26 23:39:48 +02:00
|
|
|
s.opts.address = ts.Addr()
|
2015-06-03 02:25:37 +02:00
|
|
|
s.Unlock()
|
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-06-12 20:52:27 +02:00
|
|
|
config.broker.Disconnect()
|
2015-01-14 01:31:27 +02:00
|
|
|
}()
|
|
|
|
|
2015-06-12 20:52:27 +02:00
|
|
|
// TODO: subscribe to cruft
|
|
|
|
return config.broker.Connect()
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 18:40:53 +02:00
|
|
|
func (s *rpcServer) Stop() error {
|
2015-01-14 01:31:27 +02:00
|
|
|
ch := make(chan error)
|
|
|
|
s.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|