From 3f7232facaebc69fccdbc773c2cccb4a668089cd Mon Sep 17 00:00:00 2001 From: Tommy PAGEARD Date: Wed, 8 Jul 2020 14:21:49 +0200 Subject: [PATCH] feat(peer): add GetURL to be able to retrieve peer url --- groupcache.go | 2 +- groupcache_test.go | 4 ++++ http.go | 14 ++++++++++++++ peers.go | 6 ++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/groupcache.go b/groupcache.go index e841ef9..c583d80 100644 --- a/groupcache.go +++ b/groupcache.go @@ -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) diff --git a/groupcache_test.go b/groupcache_test.go index a3d8369..58f8c81 100644 --- a/groupcache_test.go +++ b/groupcache_test.go @@ -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) diff --git a/http.go b/http.go index a792357..badc84c 100644 --- a/http.go +++ b/http.go @@ -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) { diff --git a/peers.go b/peers.go index dbb8ae1..9f36e6b 100644 --- a/peers.go +++ b/peers.go @@ -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