various fixes for broker and messaging in server (#1187)

* provide broker disconnect messages in server

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>

* broker/eats: another fix

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-02-11 21:41:23 +03:00 committed by GitHub
parent 2764de9a1a
commit 79ad1e6fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 13 deletions

View File

@ -240,6 +240,10 @@ func (n *natsBroker) serve(exit chan bool) error {
// register the cluster address // register the cluster address
for { for {
select { select {
case err := <-n.closeCh:
if err != nil {
log.Log(err)
}
case <-exit: case <-exit:
// deregister on exit // deregister on exit
n.opts.Registry.Deregister(&registry.Service{ n.opts.Registry.Deregister(&registry.Service{
@ -282,7 +286,6 @@ func (n *natsBroker) Connect() error {
} }
// set to connected // set to connected
n.connected = true
} }
status := nats.CLOSED status := nats.CLOSED
@ -313,6 +316,9 @@ func (n *natsBroker) Connect() error {
return err return err
} }
n.conn = c n.conn = c
n.connected = true
return nil return nil
} }
} }
@ -328,7 +334,6 @@ func (n *natsBroker) Disconnect() error {
// drain the connection if specified // drain the connection if specified
if n.drain { if n.drain {
n.conn.Drain() n.conn.Drain()
n.closeCh <- nil
} }
// close the client connection // close the client connection
@ -336,10 +341,12 @@ func (n *natsBroker) Disconnect() error {
// shutdown the local server // shutdown the local server
// and deregister // and deregister
select { if n.server != nil {
case <-n.exit: select {
default: case <-n.exit:
close(n.exit) default:
close(n.exit)
}
} }
// set not connected // set not connected
@ -439,7 +446,7 @@ func (n *natsBroker) onClose(conn *nats.Conn) {
} }
func (n *natsBroker) onDisconnectedError(conn *nats.Conn, err error) { func (n *natsBroker) onDisconnectedError(conn *nats.Conn, err error) {
n.closeCh <- nil n.closeCh <- err
} }
func (n *natsBroker) onAsyncError(conn *nats.Conn, sub *nats.Subscription, err error) { func (n *natsBroker) onAsyncError(conn *nats.Conn, sub *nats.Subscription, err error) {
@ -459,7 +466,8 @@ func NewBroker(opts ...Option) Broker {
} }
n := &natsBroker{ n := &natsBroker{
opts: options, opts: options,
closeCh: make(chan error),
} }
n.setOption(opts...) n.setOption(opts...)

View File

@ -457,7 +457,7 @@ func (n *natsBroker) onAsyncError(conn *nats.Conn, sub *nats.Subscription, err e
} }
func (n *natsBroker) onDisconnectedError(conn *nats.Conn, err error) { func (n *natsBroker) onDisconnectedError(conn *nats.Conn, err error) {
n.closeCh <- nil n.closeCh <- err
} }
func NewBroker(opts ...broker.Option) broker.Broker { func NewBroker(opts ...broker.Option) broker.Broker {

View File

@ -805,10 +805,7 @@ func (g *grpcServer) Start() error {
return err return err
} }
baddr := config.Broker.Address() log.Logf("Broker [%s] Connected to %s", config.Broker.String(), config.Broker.Address())
bname := config.Broker.String()
log.Logf("Broker [%s] Connected to %s", bname, baddr)
} }
// announce self to the world // announce self to the world
@ -876,6 +873,7 @@ func (g *grpcServer) Start() error {
// close transport // close transport
ch <- nil ch <- nil
log.Logf("Broker [%s] Disconnected from %s", config.Broker.String(), config.Broker.Address())
// disconnect broker // disconnect broker
config.Broker.Disconnect() config.Broker.Disconnect()
}() }()

View File

@ -879,6 +879,7 @@ func (s *rpcServer) Start() error {
// close transport listener // close transport listener
ch <- ts.Close() ch <- ts.Close()
log.Logf("Broker [%s] Disconnected from %s", bname, config.Broker.Address())
// disconnect the broker // disconnect the broker
config.Broker.Disconnect() config.Broker.Disconnect()