Skip to content

Commit

Permalink
Merge pull request #70 from skyportsystems/perGroupPeerPicker4.1
Browse files Browse the repository at this point in the history
portPicker function now takes group name
  • Loading branch information
bradfitz committed Aug 3, 2016
2 parents 02826c3 + b3c0d09 commit a6b377e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion groupcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (g *Group) Name() string {

func (g *Group) initPeers() {
if g.peers == nil {
g.peers = getPeers()
g.peers = getPeers(g.name)
}
}

Expand Down
20 changes: 17 additions & 3 deletions peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,37 @@ type NoPeers struct{}
func (NoPeers) PickPeer(key string) (peer ProtoGetter, ok bool) { return }

var (
portPicker func() PeerPicker
portPicker func(groupName string) PeerPicker
)

// RegisterPeerPicker registers the peer initialization function.
// It is called once, when the first group is created.
// Either RegisterPeerPicker or RegisterPerGroupPeerPicker should be
// called exactly once, but not both.
func RegisterPeerPicker(fn func() PeerPicker) {
if portPicker != nil {
panic("RegisterPeerPicker called more than once")
}
portPicker = func(_ string) PeerPicker { return fn() }
}

// RegisterPerGroupPeerPicker registers the peer initialization function,
// which takes the groupName, to be used in choosing a PeerPicker.
// It is called once, when the first group is created.
// Either RegisterPeerPicker or RegisterPerGroupPeerPicker should be
// called exactly once, but not both.
func RegisterPerGroupPeerPicker(fn func(groupName string) PeerPicker) {
if portPicker != nil {
panic("RegisterPeerPicker called more than once")
}
portPicker = fn
}

func getPeers() PeerPicker {
func getPeers(groupName string) PeerPicker {
if portPicker == nil {
return NoPeers{}
}
pk := portPicker()
pk := portPicker(groupName)
if pk == nil {
pk = NoPeers{}
}
Expand Down

0 comments on commit a6b377e

Please sign in to comment.