Fix: fake ip pool offset calculate (#2281)
This commit is contained in:
parent
425b6e0dc0
commit
22b9befbda
2 changed files with 15 additions and 18 deletions
|
@ -21,7 +21,7 @@ type store interface {
|
||||||
CloneTo(store)
|
CloneTo(store)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pool is a implementation about fake ip generator without storage
|
// Pool is an implementation about fake ip generator without storage
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
max uint32
|
max uint32
|
||||||
min uint32
|
min uint32
|
||||||
|
@ -99,21 +99,21 @@ func (p *Pool) CloneFrom(o *Pool) {
|
||||||
func (p *Pool) get(host string) net.IP {
|
func (p *Pool) get(host string) net.IP {
|
||||||
current := p.offset
|
current := p.offset
|
||||||
for {
|
for {
|
||||||
|
ip := uintToIP(p.min + p.offset)
|
||||||
|
if !p.store.Exist(ip) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
p.offset = (p.offset + 1) % (p.max - p.min)
|
p.offset = (p.offset + 1) % (p.max - p.min)
|
||||||
// Avoid infinite loops
|
// Avoid infinite loops
|
||||||
if p.offset == current {
|
if p.offset == current {
|
||||||
p.offset = (p.offset + 1) % (p.max - p.min)
|
p.offset = (p.offset + 1) % (p.max - p.min)
|
||||||
ip := uintToIP(p.min + p.offset - 1)
|
ip := uintToIP(p.min + p.offset)
|
||||||
p.store.DelByIP(ip)
|
p.store.DelByIP(ip)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
ip := uintToIP(p.min + p.offset - 1)
|
|
||||||
if !p.store.Exist(ip) {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
ip := uintToIP(p.min + p.offset)
|
||||||
ip := uintToIP(p.min + p.offset - 1)
|
|
||||||
p.store.PutByIP(ip, host)
|
p.store.PutByIP(ip, host)
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package fakeip
|
package fakeip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -106,15 +105,13 @@ func TestPool_CycleUsed(t *testing.T) {
|
||||||
defer os.Remove(tempfile)
|
defer os.Remove(tempfile)
|
||||||
|
|
||||||
for _, pool := range pools {
|
for _, pool := range pools {
|
||||||
foo := pool.Lookup("foo.com")
|
assert.Equal(t, net.IP{192, 168, 0, 2}, pool.Lookup("2.com"))
|
||||||
bar := pool.Lookup("bar.com")
|
assert.Equal(t, net.IP{192, 168, 0, 3}, pool.Lookup("3.com"))
|
||||||
for i := 0; i < 3; i++ {
|
assert.Equal(t, net.IP{192, 168, 0, 4}, pool.Lookup("4.com"))
|
||||||
pool.Lookup(fmt.Sprintf("%d.com", i))
|
assert.Equal(t, net.IP{192, 168, 0, 5}, pool.Lookup("5.com"))
|
||||||
}
|
assert.Equal(t, net.IP{192, 168, 0, 6}, pool.Lookup("6.com"))
|
||||||
baz := pool.Lookup("baz.com")
|
assert.Equal(t, net.IP{192, 168, 0, 2}, pool.Lookup("12.com"))
|
||||||
next := pool.Lookup("foo.com")
|
assert.Equal(t, net.IP{192, 168, 0, 3}, pool.Lookup("3.com"))
|
||||||
assert.True(t, foo.Equal(baz))
|
|
||||||
assert.True(t, next.Equal(bar))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue