From c27cd5b4abaf6fb4cf6fa8709add74dc4399d5a4 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 10 Jun 2015 14:20:58 +0100 Subject: [PATCH] Extract lookup function for owned addresses --- ipam/allocate.go | 11 +++-------- ipam/allocator.go | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ipam/allocate.go b/ipam/allocate.go index f4ffa2e9..130380e9 100644 --- a/ipam/allocate.go +++ b/ipam/allocate.go @@ -24,14 +24,9 @@ func (g *allocate) Try(alloc *Allocator) bool { return true } - // If we have previously stored an address for this container in this subnet, return it. - if addrs, found := alloc.owned[g.ident]; found { - for _, addr := range addrs { - if g.r.Contains(addr) { - g.resultChan <- allocateResult{addr, nil} - return true - } - } + if addr, found := alloc.lookupOwned(g.ident, g.r); found { + g.resultChan <- allocateResult{addr, nil} + return true } if !alloc.universe.Overlaps(g.r) { diff --git a/ipam/allocator.go b/ipam/allocator.go index 7ab81b31..e3edfcc7 100644 --- a/ipam/allocator.go +++ b/ipam/allocator.go @@ -48,7 +48,7 @@ type Allocator struct { universe address.Range // superset of all ranges ring *ring.Ring // information on ranges owned by all peers space space.Space // more detail on ranges owned by us - owned map[string][]address.Address // who owns what address, indexed by container-ID + owned map[string][]address.Address // who owns what addresses, indexed by container-ID nicknames map[router.PeerName]string // so we can map nicknames for rmpeer pendingAllocates []operation // held until we get some free space pendingClaims []operation // held until we know who owns the space @@ -191,13 +191,11 @@ func (alloc *Allocator) Allocate(ident string, r address.Range, cancelChan <-cha func (alloc *Allocator) Lookup(ident string, r address.Range) (address.Address, error) { resultChan := make(chan allocateResult) alloc.actionChan <- func() { - for _, addr := range alloc.owned[ident] { - if r.Contains(addr) { - resultChan <- allocateResult{addr: addr} - return - } + if addr, found := alloc.lookupOwned(ident, r); found { + resultChan <- allocateResult{addr: addr} + return } - resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found")} + resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found for %s in range %s", ident, r)} } result := <-resultChan return result.addr, result.err @@ -689,10 +687,20 @@ func (alloc *Allocator) reportFreeSpace() { // Owned addresses +// NB: addr must not be owned by ident already func (alloc *Allocator) addOwned(ident string, addr address.Address) { alloc.owned[ident] = append(alloc.owned[ident], addr) } +func (alloc *Allocator) lookupOwned(ident string, r address.Range) (address.Address, bool) { + for _, addr := range alloc.owned[ident] { + if r.Contains(addr) { + return addr, true + } + } + return 0, false +} + func (alloc *Allocator) findOwner(addr address.Address) string { for ident, addrs := range alloc.owned { for _, candidate := range addrs {