refactor: simplify ipam claim try

This commit is contained in:
Matthias Radestock committed 2015-07-11 02:28:14 +01:00
1 parent aaba681db1
commit c4f516655d
1 file changed
+19 -20
+19 -20
View File
@@ -38,13 +38,14 @@ func (c *claim) Try(alloc *Allocator) bool {
// If our ring doesn't know, it must be empty. We will have initiated the // If our ring doesn't know, it must be empty. We will have initiated the
// bootstrap of the ring, so wait until we find some owner for this // bootstrap of the ring, so wait until we find some owner for this
// range (might be us). // range (might be us).
owner := alloc.ring.Owner(c.addr) switch owner := alloc.ring.Owner(c.addr); owner {
if owner == router.UnknownPeerName { case alloc.ourName:
// success
case router.UnknownPeerName:
alloc.infof("Ring is empty; will try later.\n", c.addr, owner) alloc.infof("Ring is empty; will try later.\n", c.addr, owner)
c.sendResult(nil) // don't make the caller wait c.sendResult(nil) // don't make the caller wait
return false return false
} default:
if owner != alloc.ourName {
name, found := alloc.nicknames[owner] name, found := alloc.nicknames[owner]
if found { if found {
name = " (" + name + ")" name = " (" + name + ")"
@@ -52,26 +53,24 @@ func (c *claim) Try(alloc *Allocator) bool {
c.sendResult(fmt.Errorf("address %s is owned by other peer %s%s", c.addr.String(), owner, name)) c.sendResult(fmt.Errorf("address %s is owned by other peer %s%s", c.addr.String(), owner, name))
return true return true
} }
// We are the owner, check we haven't given it to another container // We are the owner, check we haven't given it to another container
existingIdent := alloc.findOwner(c.addr) switch existingIdent := alloc.findOwner(c.addr); existingIdent {
if existingIdent == c.ident { case "":
if err := alloc.space.Claim(c.addr); err != nil {
c.sendResult(err)
} else {
alloc.debugln("Claimed", c.addr, "for", c.ident)
alloc.addOwned(c.ident, c.addr)
c.sendResult(nil)
}
case c.ident:
// same identifier is claiming same address; that's OK // same identifier is claiming same address; that's OK
c.sendResult(nil) c.sendResult(nil)
return true default:
// Addr already owned by container on this machine
c.sendResult(fmt.Errorf("address %s is already owned by %s", c.addr.String(), existingIdent))
} }
if existingIdent == "" {
err := alloc.space.Claim(c.addr)
if err != nil {
c.sendResult(err)
return true
}
alloc.debugln("Claimed", c.addr, "for", c.ident)
alloc.addOwned(c.ident, c.addr)
c.sendResult(nil)
return true
}
// Addr already owned by container on this machine
c.sendResult(fmt.Errorf("address %s is already owned by %s", c.addr.String(), existingIdent))
return true return true
} }