fix overflow in peer selection

This commit is contained in:
Andrew Gerrand 2013-08-12 12:08:11 +10:00
parent b3105e2311
commit 3f89d3eb33

View File

@ -90,7 +90,11 @@ func (p *HTTPPool) PickPeer(key string) (ProtoGetter, bool) {
if len(p.peers) == 0 {
return nil, false
}
if peer := p.peers[int(h)%len(p.peers)]; peer != p.self {
n := int(h)
if n < 0 {
n *= -1
}
if peer := p.peers[n%len(p.peers)]; peer != p.self {
// TODO: pre-build a slice of *httpGetter when Set()
// is called to avoid these two allocations.
return &httpGetter{p.Transport, peer + p.basePath}, true