feat(peer): add GetURL to be able to retrieve peer url

This commit is contained in:
Tommy PAGEARD 2020-07-08 14:21:49 +02:00
parent b543958475
commit 3f7232faca
4 changed files with 25 additions and 1 deletions

View File

@ -353,7 +353,7 @@ func (g *Group) load(ctx context.Context, key string, dest Sink) (value ByteView
"err": err,
"key": key,
"category": "groupcache",
}).Error("error retrieving key from peers")
}).Errorf("error retrieving key from peer '%s'", g.peers.GetURL(key))
}
g.Stats.PeerErrors.Add(1)

View File

@ -285,6 +285,10 @@ func (p fakePeers) GetAll() []ProtoGetter {
return p
}
func (p fakePeers) GetURL(key string) string {
return "fakePeer"
}
// tests that peers (virtual, in-process) are hit, and how much.
func TestPeers(t *testing.T) {
once.Do(testSetup)

14
http.go
View File

@ -157,6 +157,20 @@ func (p *HTTPPool) PickPeer(key string) (ProtoGetter, bool) {
return nil, false
}
// GetURL
func (p *HTTPPool) GetURL(key string) string {
p.mu.Lock()
defer p.mu.Unlock()
if p.peers.IsEmpty() {
return ""
}
if peer := p.peers.Get(key); peer != p.self {
return peer
}
return p.self
}
func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Parse request.
if !strings.HasPrefix(r.URL.Path, p.opts.BasePath) {

View File

@ -20,6 +20,7 @@ package groupcache
import (
"context"
pb "github.com/mailgun/groupcache/v2/groupcachepb"
)
@ -36,7 +37,11 @@ type PeerPicker interface {
// and true to indicate that a remote peer was nominated.
// It returns nil, false if the key owner is the current peer.
PickPeer(key string) (peer ProtoGetter, ok bool)
// GetAll returns all the peers in the group
GetAll() []ProtoGetter
// GetURL returns the peer URL that own the specific key
// It returns empty string if there is no peers
GetURL(key string) string
}
// NoPeers is an implementation of PeerPicker that never finds a peer.
@ -44,6 +49,7 @@ type NoPeers struct{}
func (NoPeers) PickPeer(key string) (peer ProtoGetter, ok bool) { return }
func (NoPeers) GetAll() []ProtoGetter { return []ProtoGetter{} }
func (NoPeers) GetURL(key string) string { return "" }
var (
portPicker func(groupName string) PeerPicker