Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(discovery):Fix the protected data race #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discovery

import (
"context"
"sync"
"sync/atomic"
"time"

Expand All @@ -17,6 +18,7 @@ type Discovery struct {
client *http.Client
registry *registry.Registry
nodes atomic.Value
lock sync.RWMutex
}

// New get a discovery.
Expand All @@ -38,5 +40,7 @@ func New(c *conf.Config) (d *Discovery, cancel context.CancelFunc) {
func (d *Discovery) exitProtect() {
// exist protect mode after two renew cycle
time.Sleep(time.Second * 60)
d.lock.Lock()
d.protected = false
d.lock.Unlock()
}
7 changes: 6 additions & 1 deletion discovery/syncup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var (
// if service in init protect mode,only support write,
// read operator isn't supported.
func (d *Discovery) Protected() bool {
return d.protected
d.lock.RLock()
protected := d.protected
d.lock.RUnlock()
return protected
}

// syncUp populates the registry information from a peer eureka node.
Expand All @@ -45,7 +48,9 @@ func (d *Discovery) syncUp() {
continue
}
// sync success from other node,exit protected mode
d.lock.Lock()
d.protected = false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious about shouldn't exiting protected mode after all instances finish register?

d.lock.Unlock()
for _, is := range res.Data {
for _, i := range is {
_ = d.registry.Register(i, i.LatestTimestamp)
Expand Down