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