Registration Retry / Interval (#1651)

* Change the default ttl to 90 seconds

* add retries to registration

* Add retry to web register
This commit is contained in:
Asim Aslam 2020-05-20 11:49:09 +01:00 committed by Vasiliy Tolstov
parent 95ffd9ca4a
commit 8c937eabb9

33
grpc.go
View File

@ -22,6 +22,7 @@ import (
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/util/addr" "github.com/micro/go-micro/v2/util/addr"
"github.com/micro/go-micro/v2/util/backoff"
mgrpc "github.com/micro/go-micro/v2/util/grpc" mgrpc "github.com/micro/go-micro/v2/util/grpc"
mnet "github.com/micro/go-micro/v2/util/net" mnet "github.com/micro/go-micro/v2/util/net"
"golang.org/x/net/netutil" "golang.org/x/net/netutil"
@ -566,16 +567,36 @@ func (g *grpcServer) Subscribe(sb server.Subscriber) error {
} }
func (g *grpcServer) Register() error { func (g *grpcServer) Register() error {
g.RLock() g.RLock()
rsvc := g.rsvc rsvc := g.rsvc
config := g.opts config := g.opts
g.RUnlock() g.RUnlock()
regFunc := func(service *registry.Service) error {
var regErr error
for i := 0; i < 3; i++ {
// set the ttl
rOpts := []registry.RegisterOption{registry.RegisterTTL(config.RegisterTTL)}
// 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
}
return regErr
}
// if service already filled, reuse it and return early // if service already filled, reuse it and return early
if rsvc != nil { if rsvc != nil {
rOpts := []registry.RegisterOption{registry.RegisterTTL(config.RegisterTTL)} if err := regFunc(rsvc); err != nil {
if err := config.Registry.Register(rsvc, rOpts...); err != nil {
return err return err
} }
return nil return nil
@ -677,10 +698,8 @@ func (g *grpcServer) Register() error {
} }
} }
// create registry options // register the service
rOpts := []registry.RegisterOption{registry.RegisterTTL(config.RegisterTTL)} if err := regFunc(service); err != nil {
if err := config.Registry.Register(service, rOpts...); err != nil {
return err return err
} }