2020-07-27 13:22:00 +01:00
|
|
|
package mucp
|
2015-01-13 23:31:27 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2015-06-12 19:52:27 +01:00
|
|
|
"fmt"
|
2019-09-27 16:01:16 +02:00
|
|
|
"io"
|
2019-07-17 10:38:50 +03:00
|
|
|
"net"
|
2015-12-21 17:18:04 +00:00
|
|
|
"runtime/debug"
|
2017-08-24 18:25:05 +10:00
|
|
|
"sort"
|
2015-06-03 01:25:37 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-05-12 23:32:58 +01:00
|
|
|
"time"
|
2015-06-03 01:25:37 +01:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
raw "github.com/unistack-org/micro/v3/codec/bytes"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
"github.com/unistack-org/micro/v3/transport"
|
|
|
|
"github.com/unistack-org/micro/v3/util/addr"
|
|
|
|
"github.com/unistack-org/micro/v3/util/backoff"
|
|
|
|
mnet "github.com/unistack-org/micro/v3/util/net"
|
|
|
|
"github.com/unistack-org/micro/v3/util/socket"
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
2015-05-23 17:40:53 +01:00
|
|
|
type rpcServer struct {
|
2019-01-07 14:44:40 +00:00
|
|
|
router *router
|
|
|
|
exit chan chan error
|
2015-06-03 01:25:37 +01:00
|
|
|
|
|
|
|
sync.RWMutex
|
2020-07-27 13:22:00 +01:00
|
|
|
opts server.Options
|
|
|
|
handlers map[string]server.Handler
|
|
|
|
subscribers map[server.Subscriber][]broker.Subscriber
|
2019-08-23 15:00:29 +01:00
|
|
|
// marks the serve as started
|
|
|
|
started bool
|
2016-02-15 22:20:54 +00:00
|
|
|
// used for first registration
|
|
|
|
registered bool
|
2019-11-25 16:31:43 +00:00
|
|
|
// subscribe to service name
|
|
|
|
subscriber broker.Subscriber
|
2017-05-31 19:21:41 +01:00
|
|
|
// graceful exit
|
2019-05-27 21:17:57 +08:00
|
|
|
wg *sync.WaitGroup
|
2020-04-08 12:50:19 +03:00
|
|
|
|
|
|
|
rsvc *registry.Service
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
var (
|
|
|
|
log = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "server"})
|
|
|
|
)
|
|
|
|
|
|
|
|
func wait(ctx context.Context) *sync.WaitGroup {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
wg, ok := ctx.Value("wait").(*sync.WaitGroup)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return wg
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServer(opts ...server.Option) server.Server {
|
2015-12-02 00:47:52 +00:00
|
|
|
options := newOptions(opts...)
|
2019-06-07 15:02:19 +01:00
|
|
|
router := newRpcRouter()
|
|
|
|
router.hdlrWrappers = options.HdlrWrappers
|
2019-11-25 16:31:43 +00:00
|
|
|
router.subWrappers = options.SubWrappers
|
2019-06-07 15:02:19 +01:00
|
|
|
|
2015-05-23 17:40:53 +01:00
|
|
|
return &rpcServer{
|
2019-01-07 14:44:40 +00:00
|
|
|
opts: options,
|
2019-06-12 12:05:34 +01:00
|
|
|
router: router,
|
2020-07-27 13:22:00 +01:00
|
|
|
handlers: make(map[string]server.Handler),
|
|
|
|
subscribers: make(map[server.Subscriber][]broker.Subscriber),
|
2015-06-12 19:52:27 +01:00
|
|
|
exit: make(chan chan error),
|
2019-05-27 21:17:57 +08:00
|
|
|
wg: wait(options.Context),
|
2015-05-23 17:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
// HandleEvent handles inbound messages to the service directly
|
|
|
|
// TODO: handle requests from an event. We won't send a response.
|
2020-08-18 14:00:51 +01:00
|
|
|
func (s *rpcServer) HandleEvent(msg *broker.Message) error {
|
2019-11-25 16:31:43 +00:00
|
|
|
if msg.Header == nil {
|
|
|
|
// create empty map in case of headers empty to avoid panic later
|
|
|
|
msg.Header = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get codec
|
|
|
|
ct := msg.Header["Content-Type"]
|
|
|
|
|
|
|
|
// default content type
|
|
|
|
if len(ct) == 0 {
|
|
|
|
msg.Header["Content-Type"] = DefaultContentType
|
|
|
|
ct = DefaultContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
// get codec
|
|
|
|
cf, err := s.newCodec(ct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy headers
|
2020-02-24 17:15:20 +03:00
|
|
|
hdr := make(map[string]string, len(msg.Header))
|
2019-11-25 16:31:43 +00:00
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// create context
|
|
|
|
ctx := metadata.NewContext(context.Background(), hdr)
|
|
|
|
|
|
|
|
// TODO: inspect message header
|
|
|
|
// Micro-Service means a request
|
|
|
|
// Micro-Topic means a message
|
|
|
|
|
|
|
|
rpcMsg := &rpcMessage{
|
|
|
|
topic: msg.Header["Micro-Topic"],
|
|
|
|
contentType: ct,
|
2019-12-05 16:10:49 +00:00
|
|
|
payload: &raw.Frame{Data: msg.Body},
|
2019-11-25 16:31:43 +00:00
|
|
|
codec: cf,
|
|
|
|
header: msg.Header,
|
|
|
|
body: msg.Body,
|
|
|
|
}
|
|
|
|
|
|
|
|
// existing router
|
2020-07-27 13:22:00 +01:00
|
|
|
r := server.Router(s.router)
|
2019-11-25 16:31:43 +00:00
|
|
|
|
|
|
|
// if the router is present then execute it
|
|
|
|
if s.opts.Router != nil {
|
|
|
|
// create a wrapped function
|
|
|
|
handler := s.opts.Router.ProcessMessage
|
|
|
|
|
|
|
|
// execute the wrapper for it
|
|
|
|
for i := len(s.opts.SubWrappers); i > 0; i-- {
|
|
|
|
handler = s.opts.SubWrappers[i-1](handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the router
|
|
|
|
r = rpcRouter{m: handler}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.ProcessMessage(ctx, rpcMsg)
|
|
|
|
}
|
|
|
|
|
2019-01-08 20:32:47 +00:00
|
|
|
// ServeConn serves a single connection
|
|
|
|
func (s *rpcServer) ServeConn(sock transport.Socket) {
|
2019-08-24 20:12:04 +01:00
|
|
|
// streams are multiplexed on Micro-Stream or Micro-Id header
|
2019-11-27 17:12:07 +00:00
|
|
|
pool := socket.NewPool()
|
|
|
|
|
|
|
|
// get global waitgroup
|
|
|
|
s.Lock()
|
|
|
|
gg := s.wg
|
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
// waitgroup to wait for processing to finish
|
|
|
|
wg := &waitGroup{
|
|
|
|
gg: gg,
|
|
|
|
}
|
2019-08-24 20:12:04 +01:00
|
|
|
|
2015-12-21 17:18:04 +00:00
|
|
|
defer func() {
|
2020-07-07 03:14:22 +08:00
|
|
|
// wait till done
|
|
|
|
wg.Wait()
|
2019-11-27 17:12:07 +00:00
|
|
|
|
|
|
|
// close all the sockets for this connection
|
|
|
|
pool.Close()
|
2019-08-24 20:12:04 +01:00
|
|
|
|
|
|
|
// close underlying socket
|
2016-05-10 10:55:18 +01:00
|
|
|
sock.Close()
|
|
|
|
|
2019-08-24 20:12:04 +01:00
|
|
|
// recover any panics
|
2015-12-21 17:18:04 +00:00
|
|
|
if r := recover(); r != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, log) {
|
|
|
|
log.Error("panic recovered: ", r)
|
|
|
|
log.Error(string(debug.Stack()))
|
|
|
|
}
|
2015-12-21 17:18:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-05-13 15:58:53 +01:00
|
|
|
for {
|
|
|
|
var msg transport.Message
|
2019-11-27 17:12:07 +00:00
|
|
|
// process inbound messages one at a time
|
2016-05-13 15:58:53 +01:00
|
|
|
if err := sock.Recv(&msg); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
// check the message header for
|
|
|
|
// Micro-Service is a request
|
|
|
|
// Micro-Topic is a message
|
|
|
|
if t := msg.Header["Micro-Topic"]; len(t) > 0 {
|
|
|
|
// TODO: handle the error event
|
2020-08-18 14:00:51 +01:00
|
|
|
if err := s.HandleEvent(newMessage(msg)); err != nil {
|
2019-11-25 16:31:43 +00:00
|
|
|
msg.Header["Micro-Error"] = err.Error()
|
|
|
|
}
|
|
|
|
// write back some 200
|
2019-11-27 17:12:07 +00:00
|
|
|
if err := sock.Send(&transport.Message{
|
2019-11-25 16:31:43 +00:00
|
|
|
Header: msg.Header,
|
2019-11-27 17:12:07 +00:00
|
|
|
}); err != nil {
|
|
|
|
break
|
|
|
|
}
|
2019-11-25 16:31:43 +00:00
|
|
|
// we're done
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// business as usual
|
|
|
|
|
2019-08-16 14:42:45 +01:00
|
|
|
// use Micro-Stream as the stream identifier
|
2019-08-15 20:08:49 +01:00
|
|
|
// in the event its blank we'll always process
|
|
|
|
// on the same socket
|
|
|
|
id := msg.Header["Micro-Stream"]
|
|
|
|
|
|
|
|
// if there's no stream id then its a standard request
|
2019-08-16 14:42:45 +01:00
|
|
|
// use the Micro-Id
|
2019-08-15 20:08:49 +01:00
|
|
|
if len(id) == 0 {
|
|
|
|
id = msg.Header["Micro-Id"]
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// check stream id
|
|
|
|
var stream bool
|
2019-11-10 00:52:41 +03:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
if v := getHeader("Micro-Stream", msg.Header); len(v) > 0 {
|
|
|
|
stream = true
|
2019-05-27 21:17:57 +08:00
|
|
|
}
|
2018-05-26 09:41:41 +01:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// check if we have an existing socket
|
|
|
|
psock, ok := pool.Get(id)
|
2019-08-15 20:08:49 +01:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// if we don't have a socket and its a stream
|
|
|
|
if !ok && stream {
|
|
|
|
// check if its a last stream EOS error
|
|
|
|
err := msg.Header["Micro-Error"]
|
|
|
|
if err == lastStreamResponseError.Error() {
|
|
|
|
pool.Release(psock)
|
|
|
|
continue
|
2019-08-15 20:08:49 +01:00
|
|
|
}
|
2019-11-27 17:12:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// got an existing socket already
|
|
|
|
if ok {
|
|
|
|
// we're starting processing
|
|
|
|
wg.Add(1)
|
2019-08-15 20:08:49 +01:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// pass the message to that existing socket
|
|
|
|
if err := psock.Accept(&msg); err != nil {
|
|
|
|
// release the socket if there's an error
|
|
|
|
pool.Release(psock)
|
2019-08-15 20:08:49 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// done waiting
|
2019-08-24 20:12:04 +01:00
|
|
|
wg.Done()
|
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// continue to the next message
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// no socket was found so its new
|
|
|
|
// set the local and remote values
|
2019-08-15 20:08:49 +01:00
|
|
|
psock.SetLocal(sock.Local())
|
|
|
|
psock.SetRemote(sock.Remote())
|
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// load the socket with the current message
|
2019-08-15 20:08:49 +01:00
|
|
|
psock.Accept(&msg)
|
|
|
|
|
|
|
|
// now walk the usual path
|
|
|
|
|
2016-05-13 15:58:53 +01:00
|
|
|
// we use this Timeout header to set a server deadline
|
|
|
|
to := msg.Header["Timeout"]
|
|
|
|
// we use this Content-Type header to identify the codec needed
|
|
|
|
ct := msg.Header["Content-Type"]
|
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// copy the message headers
|
2020-02-24 17:15:20 +03:00
|
|
|
hdr := make(map[string]string, len(msg.Header))
|
2019-01-07 18:20:47 +00:00
|
|
|
for k, v := range msg.Header {
|
|
|
|
hdr[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// set local/remote ips
|
|
|
|
hdr["Local"] = sock.Local()
|
|
|
|
hdr["Remote"] = sock.Remote()
|
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// create new context with the metadata
|
2019-01-07 18:20:47 +00:00
|
|
|
ctx := metadata.NewContext(context.Background(), hdr)
|
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// set the timeout from the header if we have it
|
2019-01-07 18:20:47 +00:00
|
|
|
if len(to) > 0 {
|
|
|
|
if n, err := strconv.ParseUint(to, 10, 64); err == nil {
|
2019-12-03 15:25:58 +08:00
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(n))
|
|
|
|
defer cancel()
|
2019-01-07 18:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// if there's no content type default it
|
2019-01-07 13:48:38 +00:00
|
|
|
if len(ct) == 0 {
|
2019-01-08 20:32:47 +00:00
|
|
|
msg.Header["Content-Type"] = DefaultContentType
|
2019-01-07 13:48:38 +00:00
|
|
|
ct = DefaultContentType
|
|
|
|
}
|
|
|
|
|
2019-01-18 10:12:57 +00:00
|
|
|
// setup old protocol
|
|
|
|
cf := setupProtocol(&msg)
|
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// no legacy codec needed
|
2019-01-18 10:12:57 +00:00
|
|
|
if cf == nil {
|
|
|
|
var err error
|
2019-11-27 17:12:07 +00:00
|
|
|
// try get a new codec
|
2019-01-18 10:12:57 +00:00
|
|
|
if cf, err = s.newCodec(ct); err != nil {
|
2019-11-27 17:12:07 +00:00
|
|
|
// no codec found so send back an error
|
2020-07-07 03:14:22 +08:00
|
|
|
sock.Send(&transport.Message{
|
2019-01-18 10:12:57 +00:00
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
},
|
|
|
|
Body: []byte(err.Error()),
|
2020-07-07 03:14:22 +08:00
|
|
|
})
|
2019-08-24 20:12:04 +01:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// release the socket we just created
|
|
|
|
pool.Release(psock)
|
|
|
|
// now continue
|
|
|
|
continue
|
2019-01-18 10:12:57 +00:00
|
|
|
}
|
2016-05-13 15:58:53 +01:00
|
|
|
}
|
2015-11-25 19:50:05 +00:00
|
|
|
|
2019-11-27 17:12:07 +00:00
|
|
|
// create a new rpc codec based on the pseudo socket and codec
|
2019-08-15 20:08:49 +01:00
|
|
|
rcodec := newRpcCodec(&msg, psock, cf)
|
2019-11-27 17:12:07 +00:00
|
|
|
// check the protocol as well
|
2019-08-26 12:33:59 +01:00
|
|
|
protocol := rcodec.String()
|
2019-08-15 20:08:49 +01:00
|
|
|
|
2019-01-09 16:20:57 +00:00
|
|
|
// internal request
|
|
|
|
request := &rpcRequest{
|
2019-01-24 10:11:02 +00:00
|
|
|
service: getHeader("Micro-Service", msg.Header),
|
|
|
|
method: getHeader("Micro-Method", msg.Header),
|
|
|
|
endpoint: getHeader("Micro-Endpoint", msg.Header),
|
2019-01-09 16:20:57 +00:00
|
|
|
contentType: ct,
|
2019-01-13 12:15:35 +00:00
|
|
|
codec: rcodec,
|
2019-01-09 19:11:47 +00:00
|
|
|
header: msg.Header,
|
2019-01-09 16:20:57 +00:00
|
|
|
body: msg.Body,
|
2019-08-15 20:08:49 +01:00
|
|
|
socket: psock,
|
|
|
|
stream: stream,
|
2019-01-09 16:20:57 +00:00
|
|
|
}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2019-01-09 19:11:47 +00:00
|
|
|
// internal response
|
|
|
|
response := &rpcResponse{
|
|
|
|
header: make(map[string]string),
|
2019-08-15 20:08:49 +01:00
|
|
|
socket: psock,
|
2019-01-13 12:15:35 +00:00
|
|
|
codec: rcodec,
|
2019-01-09 19:11:47 +00:00
|
|
|
}
|
2019-01-08 20:32:47 +00:00
|
|
|
|
2019-01-09 19:11:47 +00:00
|
|
|
// set router
|
2020-07-27 13:22:00 +01:00
|
|
|
r := server.Router(s.router)
|
2019-01-11 15:49:54 +00:00
|
|
|
|
2019-06-07 15:02:19 +01:00
|
|
|
// if not nil use the router specified
|
|
|
|
if s.opts.Router != nil {
|
|
|
|
// create a wrapped function
|
2020-07-27 13:22:00 +01:00
|
|
|
handler := func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
return s.opts.Router.ServeRequest(ctx, req, rsp.(server.Response))
|
2019-06-07 15:02:19 +01:00
|
|
|
}
|
2019-01-08 20:32:47 +00:00
|
|
|
|
2019-06-07 15:02:19 +01:00
|
|
|
// execute the wrapper for it
|
|
|
|
for i := len(s.opts.HdlrWrappers); i > 0; i-- {
|
|
|
|
handler = s.opts.HdlrWrappers[i-1](handler)
|
|
|
|
}
|
2019-01-11 15:49:54 +00:00
|
|
|
|
2019-06-07 15:02:19 +01:00
|
|
|
// set the router
|
2019-11-25 16:31:43 +00:00
|
|
|
r = rpcRouter{h: handler}
|
2019-01-11 15:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 03:07:04 +08:00
|
|
|
// wait for two coroutines to exit
|
|
|
|
// serve the request and process the outbound messages
|
|
|
|
wg.Add(2)
|
|
|
|
|
2019-08-25 19:30:22 +01:00
|
|
|
// process the outbound messages from the socket
|
|
|
|
go func(id string, psock *socket.Socket) {
|
|
|
|
defer func() {
|
2019-08-26 12:33:59 +01:00
|
|
|
// TODO: don't hack this but if its grpc just break out of the stream
|
|
|
|
// We do this because the underlying connection is h2 and its a stream
|
|
|
|
switch protocol {
|
|
|
|
case "grpc":
|
|
|
|
sock.Close()
|
|
|
|
}
|
2019-11-27 17:12:07 +00:00
|
|
|
// release the socket
|
|
|
|
pool.Release(psock)
|
|
|
|
// signal we're done
|
2019-08-25 19:30:22 +01:00
|
|
|
wg.Done()
|
2019-12-27 18:53:11 +08:00
|
|
|
|
|
|
|
// recover any panics for outbound process
|
|
|
|
if r := recover(); r != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.ErrorLevel, log) {
|
|
|
|
log.Error("panic recovered: ", r)
|
|
|
|
log.Error(string(debug.Stack()))
|
|
|
|
}
|
2019-12-27 18:53:11 +08:00
|
|
|
}
|
2019-08-25 19:30:22 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// get the message from our internal handler/stream
|
|
|
|
m := new(transport.Message)
|
|
|
|
if err := psock.Process(m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the message back over the socket
|
|
|
|
if err := sock.Send(m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(id, psock)
|
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// serve the request in a go routine as this may be a stream
|
|
|
|
go func(id string, psock *socket.Socket) {
|
2019-11-27 17:12:07 +00:00
|
|
|
defer func() {
|
|
|
|
// release the socket
|
|
|
|
pool.Release(psock)
|
|
|
|
// signal we're done
|
|
|
|
wg.Done()
|
2019-12-27 18:53:11 +08:00
|
|
|
|
|
|
|
// recover any panics for call handler
|
|
|
|
if r := recover(); r != nil {
|
2020-02-23 16:45:20 +03:00
|
|
|
log.Error("panic recovered: ", r)
|
|
|
|
log.Error(string(debug.Stack()))
|
2019-12-27 18:53:11 +08:00
|
|
|
}
|
2019-11-27 17:12:07 +00:00
|
|
|
}()
|
2019-08-24 20:12:04 +01:00
|
|
|
|
2019-08-15 20:08:49 +01:00
|
|
|
// serve the actual request using the request router
|
2019-09-27 15:02:21 +02:00
|
|
|
if serveRequestError := r.ServeRequest(ctx, request, response); serveRequestError != nil {
|
2019-08-15 20:08:49 +01:00
|
|
|
// write an error response
|
2019-09-27 15:02:21 +02:00
|
|
|
writeError := rcodec.Write(&codec.Message{
|
2019-08-15 20:08:49 +01:00
|
|
|
Header: msg.Header,
|
2019-09-27 15:02:21 +02:00
|
|
|
Error: serveRequestError.Error(),
|
2019-08-15 20:08:49 +01:00
|
|
|
Type: codec.Error,
|
|
|
|
}, nil)
|
|
|
|
|
2019-09-27 15:02:21 +02:00
|
|
|
// if the server request is an EOS error we let the socket know
|
|
|
|
// sometimes the socket is already closed on the other side, so we can ignore that error
|
|
|
|
alreadyClosed := serveRequestError == lastStreamResponseError && writeError == io.EOF
|
|
|
|
|
|
|
|
// could not write error response
|
|
|
|
if writeError != nil && !alreadyClosed {
|
2019-11-27 17:12:07 +00:00
|
|
|
log.Debugf("rpc: unable to write error response: %v", writeError)
|
2019-08-15 20:08:49 +01:00
|
|
|
}
|
2019-01-22 13:55:04 +00:00
|
|
|
}
|
2019-08-15 20:08:49 +01:00
|
|
|
}(id, psock)
|
2015-11-24 00:52:59 +00:00
|
|
|
}
|
2015-05-20 22:57:19 +01:00
|
|
|
}
|
|
|
|
|
2015-11-28 11:22:29 +00:00
|
|
|
func (s *rpcServer) newCodec(contentType string) (codec.NewCodec, error) {
|
2015-12-31 18:11:46 +00:00
|
|
|
if cf, ok := s.opts.Codecs[contentType]; ok {
|
2015-11-28 11:22:29 +00:00
|
|
|
return cf, nil
|
2015-11-25 19:50:05 +00:00
|
|
|
}
|
2019-01-07 13:48:38 +00:00
|
|
|
if cf, ok := DefaultCodecs[contentType]; ok {
|
2015-11-25 19:50:05 +00:00
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
|
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (s *rpcServer) Options() server.Options {
|
2015-06-03 01:25:37 +01:00
|
|
|
s.RLock()
|
|
|
|
opts := s.opts
|
|
|
|
s.RUnlock()
|
|
|
|
return opts
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (s *rpcServer) Init(opts ...server.Option) error {
|
2015-06-03 01:25:37 +01:00
|
|
|
s.Lock()
|
2020-04-08 12:50:19 +03:00
|
|
|
defer s.Unlock()
|
|
|
|
|
2015-05-26 22:39:48 +01:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&s.opts)
|
|
|
|
}
|
2019-06-07 15:02:19 +01:00
|
|
|
// update router if its the default
|
|
|
|
if s.opts.Router == nil {
|
|
|
|
r := newRpcRouter()
|
|
|
|
r.hdlrWrappers = s.opts.HdlrWrappers
|
|
|
|
r.serviceMap = s.router.serviceMap
|
2019-11-25 16:31:43 +00:00
|
|
|
r.subWrappers = s.opts.SubWrappers
|
2019-06-07 15:02:19 +01:00
|
|
|
s.router = r
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:50:19 +03:00
|
|
|
s.rsvc = nil
|
|
|
|
|
2016-01-02 19:12:17 +00:00
|
|
|
return nil
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (s *rpcServer) NewHandler(h interface{}, opts ...server.HandlerOption) server.Handler {
|
2019-01-07 17:17:06 +00:00
|
|
|
return s.router.NewHandler(h, opts...)
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (s *rpcServer) Handle(h server.Handler) error {
|
2016-02-18 18:08:43 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2019-01-07 17:17:06 +00:00
|
|
|
if err := s.router.Handle(h); err != nil {
|
2015-06-03 01:25:37 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-02-18 18:08:43 +00:00
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
s.handlers[h.Name()] = h
|
2016-02-18 18:08:43 +00:00
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
return nil
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (s *rpcServer) NewSubscriber(topic string, sb interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
2019-11-25 16:31:43 +00:00
|
|
|
return s.router.NewSubscriber(topic, sb, opts...)
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
func (s *rpcServer) Subscribe(sb server.Subscriber) error {
|
2019-11-25 16:31:43 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2015-06-12 19:52:27 +01:00
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
if err := s.router.Subscribe(sb); err != nil {
|
2015-12-02 17:42:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
s.subscribers[sb] = nil
|
2015-06-12 19:52:27 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 12:23:18 +00:00
|
|
|
func (s *rpcServer) Register() error {
|
2020-04-08 12:50:19 +03:00
|
|
|
s.RLock()
|
|
|
|
rsvc := s.rsvc
|
2015-12-31 18:11:46 +00:00
|
|
|
config := s.Options()
|
2020-04-08 12:50:19 +03:00
|
|
|
s.RUnlock()
|
|
|
|
|
2020-08-09 16:17:52 +01:00
|
|
|
// only register if it exists or is not noop
|
|
|
|
if config.Registry == nil || config.Registry.String() == "noop" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:49:09 +01:00
|
|
|
regFunc := func(service *registry.Service) error {
|
|
|
|
// create registry options
|
2020-06-19 09:24:32 +01:00
|
|
|
rOpts := []registry.RegisterOption{
|
|
|
|
registry.RegisterTTL(config.RegisterTTL),
|
|
|
|
registry.RegisterDomain(s.opts.Namespace),
|
|
|
|
}
|
2020-05-20 11:49:09 +01:00
|
|
|
|
|
|
|
var regErr error
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// attempt to register
|
|
|
|
if err := config.Registry.Register(service, rOpts...); err != nil {
|
|
|
|
// set the error
|
|
|
|
regErr = err
|
|
|
|
// backoff then retry
|
|
|
|
time.Sleep(backoff.Do(i + 1))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// success so nil error
|
|
|
|
regErr = nil
|
|
|
|
break
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:49:09 +01:00
|
|
|
return regErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// have we registered before?
|
|
|
|
if rsvc != nil {
|
|
|
|
if err := regFunc(rsvc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 12:50:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var advt, host, port string
|
|
|
|
var cacheService bool
|
2015-11-04 21:53:39 +00:00
|
|
|
|
2015-11-11 18:22:04 +00:00
|
|
|
// check the advertise address first
|
|
|
|
// if it exists then use it, otherwise
|
|
|
|
// use the address
|
2015-12-31 18:11:46 +00:00
|
|
|
if len(config.Advertise) > 0 {
|
|
|
|
advt = config.Advertise
|
2015-11-11 18:22:04 +00:00
|
|
|
} else {
|
2015-12-31 18:11:46 +00:00
|
|
|
advt = config.Address
|
2015-11-11 18:22:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 00:18:40 +03:00
|
|
|
if cnt := strings.Count(advt, ":"); cnt >= 1 {
|
2019-07-17 10:38:50 +03:00
|
|
|
// ipv6 address in format [host]:port or ipv4 host:port
|
|
|
|
host, port, err = net.SplitHostPort(advt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
} else {
|
2019-07-17 10:38:50 +03:00
|
|
|
host = advt
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 12:50:19 +03:00
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
cacheService = true
|
|
|
|
}
|
|
|
|
|
2017-01-12 13:20:34 +00:00
|
|
|
addr, err := addr.Extract(host)
|
2015-11-04 21:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-04 10:29:10 +00:00
|
|
|
// make copy of metadata
|
2020-04-08 12:50:19 +03:00
|
|
|
md := metadata.Copy(config.Metadata)
|
2019-02-04 10:29:10 +00:00
|
|
|
|
2019-08-14 21:32:28 +08:00
|
|
|
// mq-rpc(eg. nats) doesn't need the port. its addr is queue name.
|
|
|
|
if port != "" {
|
|
|
|
addr = mnet.HostPort(addr, port)
|
|
|
|
}
|
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
// register service
|
|
|
|
node := ®istry.Node{
|
2016-01-02 00:38:57 +00:00
|
|
|
Id: config.Name + "-" + config.Id,
|
2019-08-14 21:32:28 +08:00
|
|
|
Address: addr,
|
2019-02-04 10:29:10 +00:00
|
|
|
Metadata: md,
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
node.Metadata["transport"] = config.Transport.String()
|
2016-01-04 01:27:05 +00:00
|
|
|
node.Metadata["broker"] = config.Broker.String()
|
|
|
|
node.Metadata["server"] = s.String()
|
2016-01-04 23:27:44 +00:00
|
|
|
node.Metadata["registry"] = config.Registry.String()
|
2019-01-18 12:30:39 +00:00
|
|
|
node.Metadata["protocol"] = "mucp"
|
2015-12-19 21:56:14 +00:00
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
s.RLock()
|
2019-12-07 19:54:29 +00:00
|
|
|
|
2017-08-24 18:25:05 +10:00
|
|
|
// Maps are ordered randomly, sort the keys for consistency
|
|
|
|
var handlerList []string
|
|
|
|
for n, e := range s.handlers {
|
2016-01-08 14:02:32 +00:00
|
|
|
// Only advertise non internal handlers
|
|
|
|
if !e.Options().Internal {
|
2017-08-24 18:25:05 +10:00
|
|
|
handlerList = append(handlerList, n)
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
2019-12-07 19:54:29 +00:00
|
|
|
|
2017-08-24 18:25:05 +10:00
|
|
|
sort.Strings(handlerList)
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
var subscriberList []server.Subscriber
|
2017-08-24 18:25:05 +10:00
|
|
|
for e := range s.subscribers {
|
2016-01-08 14:02:32 +00:00
|
|
|
// Only advertise non internal subscribers
|
|
|
|
if !e.Options().Internal {
|
2017-08-24 18:25:05 +10:00
|
|
|
subscriberList = append(subscriberList, e)
|
2016-01-08 14:02:32 +00:00
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
}
|
2019-12-07 19:54:29 +00:00
|
|
|
|
2017-08-24 18:25:05 +10:00
|
|
|
sort.Slice(subscriberList, func(i, j int) bool {
|
2019-11-25 16:31:43 +00:00
|
|
|
return subscriberList[i].Topic() > subscriberList[j].Topic()
|
2017-08-24 18:25:05 +10:00
|
|
|
})
|
|
|
|
|
2019-11-05 17:43:12 +00:00
|
|
|
endpoints := make([]*registry.Endpoint, 0, len(handlerList)+len(subscriberList))
|
2019-12-07 19:54:29 +00:00
|
|
|
|
2017-08-24 18:25:05 +10:00
|
|
|
for _, n := range handlerList {
|
|
|
|
endpoints = append(endpoints, s.handlers[n].Endpoints()...)
|
|
|
|
}
|
2019-12-07 19:54:29 +00:00
|
|
|
|
2017-08-24 18:25:05 +10:00
|
|
|
for _, e := range subscriberList {
|
|
|
|
endpoints = append(endpoints, e.Endpoints()...)
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
|
|
|
|
service := ®istry.Service{
|
2015-12-31 18:11:46 +00:00
|
|
|
Name: config.Name,
|
|
|
|
Version: config.Version,
|
2015-06-03 01:25:37 +01:00
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
Endpoints: endpoints,
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:54:29 +00:00
|
|
|
// get registered value
|
2016-02-27 22:14:25 +00:00
|
|
|
registered := s.registered
|
2019-12-07 19:54:29 +00:00
|
|
|
|
|
|
|
s.RUnlock()
|
2016-02-27 22:14:25 +00:00
|
|
|
|
|
|
|
if !registered {
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Registry [%s] Registering node: %s", config.Registry.String(), node.Id)
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2016-02-27 22:14:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:49:09 +01:00
|
|
|
// register the service
|
|
|
|
if err := regFunc(service); err != nil {
|
2015-06-12 19:52:27 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-27 22:14:25 +00:00
|
|
|
// already registered? don't need to register subscribers
|
|
|
|
if registered {
|
2016-02-15 22:20:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 22:14:25 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2019-12-07 19:54:29 +00:00
|
|
|
// set what we're advertising
|
|
|
|
s.opts.Advertise = addr
|
2016-02-15 22:20:54 +00:00
|
|
|
|
2020-03-05 10:29:50 +03:00
|
|
|
// router can exchange messages
|
|
|
|
if s.opts.Router != nil {
|
|
|
|
// subscribe to the topic with own name
|
|
|
|
sub, err := s.opts.Broker.Subscribe(config.Name, s.HandleEvent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-25 16:31:43 +00:00
|
|
|
|
2020-03-05 10:29:50 +03:00
|
|
|
// save the subscriber
|
|
|
|
s.subscriber = sub
|
|
|
|
}
|
2019-11-25 16:31:43 +00:00
|
|
|
|
|
|
|
// subscribe for all of the subscribers
|
2019-11-01 15:07:53 +00:00
|
|
|
for sb := range s.subscribers {
|
2016-01-22 21:48:43 +00:00
|
|
|
var opts []broker.SubscribeOption
|
|
|
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
2016-05-10 10:55:18 +01:00
|
|
|
opts = append(opts, broker.Queue(queue))
|
2016-01-22 21:48:43 +00:00
|
|
|
}
|
2019-05-24 20:06:27 +08:00
|
|
|
|
2019-01-24 00:46:26 +03:00
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
opts = append(opts, broker.SubscribeContext(cx))
|
|
|
|
}
|
2019-05-24 20:06:27 +08:00
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
sub, err := config.Broker.Subscribe(sb.Topic(), s.HandleEvent, opts...)
|
2015-06-12 19:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Subscribing to topic: %s", sub.Topic())
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
s.subscribers[sb] = []broker.Subscriber{sub}
|
|
|
|
}
|
2020-04-21 23:01:59 +03:00
|
|
|
if cacheService {
|
|
|
|
s.rsvc = service
|
|
|
|
}
|
|
|
|
s.registered = true
|
2015-06-12 19:52:27 +01:00
|
|
|
|
|
|
|
return nil
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rpcServer) Deregister() error {
|
2019-07-17 10:38:50 +03:00
|
|
|
var err error
|
|
|
|
var advt, host, port string
|
|
|
|
|
2020-04-08 12:50:19 +03:00
|
|
|
s.RLock()
|
2015-12-31 18:11:46 +00:00
|
|
|
config := s.Options()
|
2020-04-08 12:50:19 +03:00
|
|
|
s.RUnlock()
|
2015-11-11 18:22:04 +00:00
|
|
|
|
2020-08-09 16:17:52 +01:00
|
|
|
// only register if it exists or is not noop
|
|
|
|
if config.Registry == nil || config.Registry.String() == "noop" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-11 18:22:04 +00:00
|
|
|
// check the advertise address first
|
|
|
|
// if it exists then use it, otherwise
|
|
|
|
// use the address
|
2015-12-31 18:11:46 +00:00
|
|
|
if len(config.Advertise) > 0 {
|
|
|
|
advt = config.Advertise
|
2015-11-11 18:22:04 +00:00
|
|
|
} else {
|
2015-12-31 18:11:46 +00:00
|
|
|
advt = config.Address
|
2015-11-11 18:22:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 00:18:40 +03:00
|
|
|
if cnt := strings.Count(advt, ":"); cnt >= 1 {
|
2019-07-17 10:38:50 +03:00
|
|
|
// ipv6 address in format [host]:port or ipv4 host:port
|
|
|
|
host, port, err = net.SplitHostPort(advt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-03 01:25:37 +01:00
|
|
|
} else {
|
2019-07-17 10:38:50 +03:00
|
|
|
host = advt
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 13:20:34 +00:00
|
|
|
addr, err := addr.Extract(host)
|
2015-11-04 21:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-15 08:59:50 +01:00
|
|
|
// mq-rpc(eg. nats) doesn't need the port. its addr is queue name.
|
|
|
|
if port != "" {
|
|
|
|
addr = mnet.HostPort(addr, port)
|
|
|
|
}
|
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
node := ®istry.Node{
|
2016-01-02 00:38:57 +00:00
|
|
|
Id: config.Name + "-" + config.Id,
|
2019-08-15 08:59:50 +01:00
|
|
|
Address: addr,
|
2015-06-03 01:25:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
2015-12-31 18:11:46 +00:00
|
|
|
Name: config.Name,
|
|
|
|
Version: config.Version,
|
2015-06-03 01:25:37 +01:00
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Registry [%s] Deregistering node: %s", config.Registry.String(), node.Id)
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2020-06-25 11:25:43 +01:00
|
|
|
if err := config.Registry.Deregister(service, registry.DeregisterDomain(s.opts.Namespace)); err != nil {
|
2015-06-12 19:52:27 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
2020-04-08 12:50:19 +03:00
|
|
|
s.rsvc = nil
|
2016-02-15 22:20:54 +00:00
|
|
|
|
|
|
|
if !s.registered {
|
|
|
|
s.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.registered = false
|
|
|
|
|
2019-11-25 16:31:43 +00:00
|
|
|
// close the subscriber
|
|
|
|
if s.subscriber != nil {
|
|
|
|
s.subscriber.Unsubscribe()
|
|
|
|
s.subscriber = nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
for sb, subs := range s.subscribers {
|
|
|
|
for _, sub := range subs {
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Unsubscribing %s from topic: %s", node.Id, sub.Topic())
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2015-06-12 19:52:27 +01:00
|
|
|
sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
s.subscribers[sb] = nil
|
|
|
|
}
|
2016-02-15 22:20:54 +00:00
|
|
|
|
2015-06-12 19:52:27 +01:00
|
|
|
s.Unlock()
|
|
|
|
return nil
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 17:40:53 +01:00
|
|
|
func (s *rpcServer) Start() error {
|
2019-08-23 15:00:29 +01:00
|
|
|
s.RLock()
|
|
|
|
if s.started {
|
|
|
|
s.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.RUnlock()
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
config := s.Options()
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2019-01-24 13:22:17 +00:00
|
|
|
// start listening on the transport
|
2015-12-31 18:11:46 +00:00
|
|
|
ts, err := config.Transport.Listen(config.Address)
|
2015-01-13 23:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Transport [%s] Listening on %s", config.Transport.String(), ts.Addr())
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2019-01-22 12:18:33 +00:00
|
|
|
|
2018-05-25 15:19:25 +01:00
|
|
|
// swap address
|
2019-01-24 13:22:17 +00:00
|
|
|
s.Lock()
|
2018-05-25 15:19:25 +01:00
|
|
|
addr := s.opts.Address
|
2015-12-31 18:11:46 +00:00
|
|
|
s.opts.Address = ts.Addr()
|
2015-06-03 01:25:37 +01:00
|
|
|
s.Unlock()
|
2015-05-16 00:34:02 +01:00
|
|
|
|
2020-04-08 12:50:19 +03:00
|
|
|
bname := config.Broker.String()
|
|
|
|
|
2019-01-24 13:22:17 +00:00
|
|
|
// connect to the broker
|
|
|
|
if err := config.Broker.Connect(); err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Broker [%s] connect error: %v", bname, err)
|
|
|
|
}
|
2019-01-24 13:22:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Broker [%s] Connected to %s", bname, config.Broker.Address())
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2019-01-24 13:22:17 +00:00
|
|
|
|
2019-05-13 01:39:42 +03:00
|
|
|
// use RegisterCheck func before register
|
|
|
|
if err = s.opts.RegisterCheck(s.opts.Context); err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Server %s-%s register check error: %s", config.Name, config.Id, err)
|
|
|
|
}
|
2019-05-13 01:39:42 +03:00
|
|
|
} else {
|
|
|
|
// announce self to the world
|
|
|
|
if err = s.Register(); err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Server %s-%s register error: %s", config.Name, config.Id, err)
|
|
|
|
}
|
2019-05-13 01:39:42 +03:00
|
|
|
}
|
2019-01-24 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit := make(chan bool)
|
2018-11-15 19:55:13 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-01-24 13:22:17 +00:00
|
|
|
// listen for connections
|
2019-01-08 20:32:47 +00:00
|
|
|
err := ts.Accept(s.ServeConn)
|
2018-11-15 19:55:13 +00:00
|
|
|
|
2019-01-24 13:22:17 +00:00
|
|
|
// TODO: listen for messages
|
|
|
|
// msg := broker.Exchange(service).Consume()
|
|
|
|
|
2018-11-15 19:55:13 +00:00
|
|
|
select {
|
2019-01-24 13:22:17 +00:00
|
|
|
// check if we're supposed to exit
|
2018-11-15 19:55:13 +00:00
|
|
|
case <-exit:
|
|
|
|
return
|
|
|
|
// check the error and backoff
|
2019-01-24 13:22:17 +00:00
|
|
|
default:
|
|
|
|
if err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Accept error: %v", err)
|
|
|
|
}
|
2019-01-24 13:22:17 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-15 19:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// no error just exit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2015-01-13 23:31:27 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-01-24 16:08:04 +00:00
|
|
|
t := new(time.Ticker)
|
2019-01-24 13:22:17 +00:00
|
|
|
|
|
|
|
// only process if it exists
|
2019-01-24 16:08:04 +00:00
|
|
|
if s.opts.RegisterInterval > time.Duration(0) {
|
|
|
|
// new ticker
|
|
|
|
t = time.NewTicker(s.opts.RegisterInterval)
|
2019-01-24 13:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return error chan
|
|
|
|
var ch chan error
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// register self on interval
|
|
|
|
case <-t.C:
|
2019-05-13 01:39:42 +03:00
|
|
|
s.RLock()
|
|
|
|
registered := s.registered
|
|
|
|
s.RUnlock()
|
2019-12-28 14:11:46 +03:00
|
|
|
rerr := s.opts.RegisterCheck(s.opts.Context)
|
|
|
|
if rerr != nil && registered {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-07-07 21:14:23 +08:00
|
|
|
log.Errorf("Server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr)
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2019-05-13 01:39:42 +03:00
|
|
|
// deregister self in case of error
|
|
|
|
if err := s.Deregister(); err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Server %s-%s deregister error: %s", config.Name, config.Id, err)
|
|
|
|
}
|
2019-05-13 01:39:42 +03:00
|
|
|
}
|
2019-12-28 14:11:46 +03:00
|
|
|
} else if rerr != nil && !registered {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
2020-07-07 21:14:23 +08:00
|
|
|
log.Errorf("Server %s-%s register check error: %s", config.Name, config.Id, rerr)
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2019-12-28 14:11:46 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := s.Register(); err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Server %s-%s register error: %s", config.Name, config.Id, err)
|
|
|
|
}
|
2019-01-24 13:22:17 +00:00
|
|
|
}
|
|
|
|
// wait for exit
|
|
|
|
case ch = <-s.exit:
|
|
|
|
t.Stop()
|
|
|
|
close(exit)
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 14:11:46 +03:00
|
|
|
s.RLock()
|
|
|
|
registered := s.registered
|
|
|
|
s.RUnlock()
|
|
|
|
if registered {
|
|
|
|
// deregister self
|
|
|
|
if err := s.Deregister(); err != nil {
|
2020-04-08 12:50:19 +03:00
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Server %s-%s deregister error: %s", config.Name, config.Id, err)
|
|
|
|
}
|
2019-12-28 14:11:46 +03:00
|
|
|
}
|
2019-01-24 13:22:17 +00:00
|
|
|
}
|
2017-05-31 19:21:41 +01:00
|
|
|
|
2019-11-10 00:52:41 +03:00
|
|
|
s.Lock()
|
|
|
|
swg := s.wg
|
|
|
|
s.Unlock()
|
|
|
|
|
2017-05-31 19:21:41 +01:00
|
|
|
// wait for requests to finish
|
2019-11-10 00:52:41 +03:00
|
|
|
if swg != nil {
|
|
|
|
swg.Wait()
|
2017-05-31 19:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// close transport listener
|
2015-05-20 22:57:19 +01:00
|
|
|
ch <- ts.Close()
|
2017-05-31 19:21:41 +01:00
|
|
|
|
2020-04-23 11:24:39 +01:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
log.Infof("Broker [%s] Disconnected from %s", bname, config.Broker.Address())
|
2020-04-08 12:50:19 +03:00
|
|
|
}
|
2017-05-31 19:21:41 +01:00
|
|
|
// disconnect the broker
|
2020-04-08 12:50:19 +03:00
|
|
|
if err := config.Broker.Disconnect(); err != nil {
|
|
|
|
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
|
|
|
|
log.Errorf("Broker [%s] Disconnect error: %v", bname, err)
|
|
|
|
}
|
|
|
|
}
|
2018-05-25 15:19:25 +01:00
|
|
|
|
|
|
|
// swap back address
|
2019-01-24 13:22:17 +00:00
|
|
|
s.Lock()
|
2018-05-25 15:19:25 +01:00
|
|
|
s.opts.Address = addr
|
|
|
|
s.Unlock()
|
2015-01-13 23:31:27 +00:00
|
|
|
}()
|
|
|
|
|
2019-08-23 15:00:29 +01:00
|
|
|
// mark the server as started
|
|
|
|
s.Lock()
|
|
|
|
s.started = true
|
|
|
|
s.Unlock()
|
|
|
|
|
2019-01-22 12:18:33 +00:00
|
|
|
return nil
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 17:40:53 +01:00
|
|
|
func (s *rpcServer) Stop() error {
|
2019-08-23 15:00:29 +01:00
|
|
|
s.RLock()
|
|
|
|
if !s.started {
|
|
|
|
s.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.RUnlock()
|
|
|
|
|
2015-01-13 23:31:27 +00:00
|
|
|
ch := make(chan error)
|
|
|
|
s.exit <- ch
|
2019-08-23 15:00:29 +01:00
|
|
|
|
2019-12-03 22:59:44 +03:00
|
|
|
err := <-ch
|
|
|
|
s.Lock()
|
|
|
|
s.started = false
|
|
|
|
s.Unlock()
|
2019-08-23 15:00:29 +01:00
|
|
|
|
|
|
|
return err
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
2015-12-19 21:56:14 +00:00
|
|
|
|
|
|
|
func (s *rpcServer) String() string {
|
2019-06-08 19:40:44 +01:00
|
|
|
return "mucp"
|
2015-12-19 21:56:14 +00:00
|
|
|
}
|