Skip to content

Commit

Permalink
cluster: retry if register to cluster failed
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng committed Jun 30, 2019
1 parent 9138e2a commit dce2405
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os/signal"
"sync/atomic"
"syscall"
"time"

"github.com/lonng/nano/cluster"
"github.com/lonng/nano/component"
Expand Down Expand Up @@ -56,10 +57,16 @@ func run(addr string, isWs bool, certificate string, key string, opts ...Option)
opt.clientAddr = addr
}

// Set the retry interval to 3 secondes if doesn't set by user
if opt.retryInterval == 0 {
opt.retryInterval = time.Second * 3
}

node := &cluster.Node{
Label: opt.label,
IsMaster: opt.isMaster,
AdvertiseAddr: opt.advertiseAddr,
RetryInterval: opt.retryInterval,
ClientAddr: opt.clientAddr,
ServiceAddr: addr,
Components: opt.components,
Expand Down
15 changes: 11 additions & 4 deletions cluster/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"net/http"
"strings"
"sync"
"time"

"github.com/gorilla/websocket"
"github.com/lonng/nano/cluster/clusterpb"
Expand All @@ -49,6 +50,7 @@ type Node struct {
IsMaster bool // indicate if the current node is master
IsGate bool // indicate if the current node is gate
AdvertiseAddr string // master server service address
RetryInterval time.Duration
ClientAddr string
ServiceAddr string
Components *component.Components
Expand Down Expand Up @@ -164,11 +166,16 @@ func (n *Node) initNode() error {
Services: n.handler.LocalService(),
},
}
resp, err := client.Register(context.Background(), request)
if err != nil {
return err
for {
resp, err := client.Register(context.Background(), request)
if err == nil {
n.handler.initRemoteService(resp.Members)
break
}
log.Println("Register current node to cluster failed", err, "and will retry in", n.RetryInterval.String())
time.Sleep(n.RetryInterval)
}
n.handler.initRemoteService(resp.Members)

}

return nil
Expand Down
6 changes: 5 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type (
pipeline pipeline.Pipeline
isMaster bool
advertiseAddr string
retryInterval time.Duration
clientAddr string
components *component.Components
label string
Expand All @@ -33,9 +34,12 @@ func WithPipeline(pipeline pipeline.Pipeline) Option {

// WithAdvertiseAddr sets the advertise address option, it will be the listen address in
// master node and an advertise address which cluster member to connect
func WithAdvertiseAddr(addr string) Option {
func WithAdvertiseAddr(addr string, retryInterval ...time.Duration) Option {
return func(opt *options) {
opt.advertiseAddr = addr
if len(retryInterval) > 0 {
opt.retryInterval = retryInterval[0]
}
}
}

Expand Down

0 comments on commit dce2405

Please sign in to comment.