Skip to content

Commit

Permalink
Update selector race, rename cache selector
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Dec 29, 2018
1 parent ff982b5 commit 5cae330
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 500 deletions.
4 changes: 1 addition & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

// selectors
"github.com/micro/go-micro/selector"
"github.com/micro/go-micro/selector/cache"

// transports
"github.com/micro/go-micro/transport"
Expand Down Expand Up @@ -149,7 +148,6 @@ var (
Name: "selector",
EnvVar: "MICRO_SELECTOR",
Usage: "Selector used to pick nodes for querying",
Value: "cache",
},
cli.StringFlag{
Name: "transport",
Expand Down Expand Up @@ -179,7 +177,7 @@ var (

DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
"default": selector.NewSelector,
"cache": cache.NewSelector,
"cache": selector.NewSelector,
}

DefaultServers = map[string]func(...server.Option) server.Server{
Expand Down
47 changes: 35 additions & 12 deletions registry/gossip/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,35 @@ func cp(current []*registry.Service) []*registry.Service {
}

func addNodes(old, neu []*registry.Node) []*registry.Node {
var nodes []*registry.Node

// add all new nodes
for _, n := range neu {
var seen bool
for i, o := range old {
node := *n
nodes = append(nodes, &node)
}

// look at old nodes
for _, o := range old {
var exists bool

// check against new nodes
for _, n := range nodes {
// ids match then skip
if o.Id == n.Id {
seen = true
old[i] = n
exists = true
break
}
}
if !seen {
old = append(old, n)

// keep old node
if !exists {
node := *o
nodes = append(nodes, &node)
}
}
return old

return nodes
}

func addServices(old, neu []*registry.Service) []*registry.Service {
Expand Down Expand Up @@ -91,19 +106,27 @@ func delNodes(old, del []*registry.Node) []*registry.Node {

func delServices(old, del []*registry.Service) []*registry.Service {
var services []*registry.Service
for i, o := range old {

for _, o := range old {
srv := new(registry.Service)
*srv = *o

var rem bool

for _, s := range del {
if o.Version == s.Version {
old[i].Nodes = delNodes(o.Nodes, s.Nodes)
if len(old[i].Nodes) == 0 {
if srv.Version == s.Version {
srv.Nodes = delNodes(srv.Nodes, s.Nodes)

if len(srv.Nodes) == 0 {
rem = true
}
}
}

if !rem {
services = append(services, o)
services = append(services, srv)
}
}

return services
}
Loading

0 comments on commit 5cae330

Please sign in to comment.