fix registry check

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-09-10 09:52:09 +03:00
parent 6372b7b3fa
commit e55535a53c

26
tcp.go
View File

@ -280,6 +280,8 @@ func (h *tcpServer) Start() error {
return err
}
config := h.Options()
// register
if err = h.Register(); err != nil {
return err
@ -308,8 +310,30 @@ func (h *tcpServer) Start() error {
select {
// register self on interval
case <-t.C:
h.RLock()
registered := h.registered
h.RUnlock()
rerr := h.opts.RegisterCheck(h.opts.Context)
if rerr != nil && registered {
if logger.V(logger.ErrorLevel) {
logger.Errorf("Server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr)
}
// deregister self in case of error
if err := h.Deregister(); err != nil {
if logger.V(logger.ErrorLevel) {
logger.Errorf("Server %s-%s deregister error: %s", config.Name, config.Id, err)
}
}
} else if rerr != nil && !registered {
if logger.V(logger.ErrorLevel) {
logger.Errorf("Server %s-%s register check error: %s", config.Name, config.Id, rerr)
}
continue
}
if err := h.Register(); err != nil {
logger.Error("Server register error: ", err)
if logger.V(logger.ErrorLevel) {
logger.Errorf("Server %s-%s register error: %s", config.Name, config.Id, err)
}
}
// wait for exit
case ch = <-h.exit: