Retry IP allocator claims

It may be that we cannot talk to a peer straight away, so allow claims
to be retried a few times before giving up.
This commit is contained in:
Bryan Boreham committed 2019-10-25 17:13:11 +00:00
1 parent 6a48da60ec
commit e8fcd21baa
1 file changed
+9 -2
+9 -2
View File
@@ -12,6 +12,7 @@ import (
type claim struct {
resultChan chan<- error
tryCount int
ident string // a container ID, something like "weave:expose", or api.NoContainerID
cidr address.CIDR // single address being claimed
isContainer bool // true if ident is a container ID
@@ -19,6 +20,8 @@ type claim struct {
hasBeenCancelled func() bool
}
const maxTryCount = 5
// Send an error (or nil for success) back to caller listening on resultChan
func (c *claim) sendResult(result error) {
// Make sure we only send a result once, since listener stops listening after that
@@ -40,6 +43,8 @@ func (c *claim) Try(alloc *Allocator) bool {
return true
}
c.tryCount++
addOwned := func() {
if c.ident == api.NoContainerID {
alloc.addOwned(c.cidr.Addr.String(), c.cidr, c.isContainer)
@@ -90,8 +95,10 @@ func (c *claim) Try(alloc *Allocator) bool {
if c.noErrorOnUnknown {
alloc.infof("Claim %s for %s: %s; will try later.", c.cidr, c.ident, err)
c.sendResult(nil)
} else { // just tell the user they can't do this.
c.deniedBy(alloc, owner)
} else if c.tryCount > maxTryCount {
// give up, tell the user they can't do this
c.deniedBy(alloc, owner, err)
return true
}
}
return false