fix(): get URL from httpGetter instead of httpPool

This commit is contained in:
Tommy PAGEARD 2020-07-08 18:59:59 +02:00
parent ae2e156f19
commit fb2028f2ff
4 changed files with 12 additions and 19 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",
}).Errorf("error retrieving key from peer '%s'", g.peers.GetURL(key))
}).Errorf("error retrieving key from peer '%s'", peer.GetURL())
}
g.Stats.PeerErrors.Add(1)

View File

@ -271,6 +271,10 @@ func (p *fakePeer) Remove(_ context.Context, in *pb.GetRequest) error {
return nil
}
func (p *fakePeer) GetURL() string {
return "fakePeer"
}
type fakePeers []ProtoGetter
func (p fakePeers) PickPeer(key string) (peer ProtoGetter, ok bool) {

19
http.go
View File

@ -157,20 +157,6 @@ 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) {
@ -239,6 +225,11 @@ type httpGetter struct {
baseURL string
}
// GetURL
func (p *httpGetter) GetURL() string {
return p.baseURL
}
var bufferPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}

View File

@ -28,6 +28,8 @@ import (
type ProtoGetter interface {
Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error
Remove(context context.Context, in *pb.GetRequest) error
// GetURL returns the peer URL
GetURL() string
}
// PeerPicker is the interface that must be implemented to locate
@ -39,9 +41,6 @@ type PeerPicker interface {
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.
@ -49,7 +48,6 @@ 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