Merge pull request #70 from skyportsystems/perGroupPeerPicker4.1

portPicker function now takes group name
This commit is contained in:
Brad Fitzpatrick 2016-08-03 13:04:08 -07:00 committed by GitHub
commit a6b377e340
2 changed files with 18 additions and 4 deletions

View File

@ -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)
}
}

View File

@ -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{}
}