Skip to content

Commit

Permalink
Merge 72d872f into 5a67a24
Browse files Browse the repository at this point in the history
  • Loading branch information
huahuiyang committed Jul 27, 2018
2 parents 5a67a24 + 72d872f commit 7603ab3
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions api/v1/lib/httpcli/httpsched/httpsched.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ type (

client struct {
*httpcli.Client
redirect RedirectSettings
allowReconnect bool // feature flag
listener func(Notification)
redirect RedirectSettings
allowReconnect bool // feature flag
listener func(Notification)
candidateSelector CandidateSelector
}

// Caller is the public interface a framework scheduler's should consume
Expand Down Expand Up @@ -74,6 +75,10 @@ type (
requestOpts []httpcli.RequestOpt // requestOpts are temporary per-request options
opt httpcli.Opt // opt is a temporary client option
}

// CandidateSelector returns the next endpoint to try if there are errors reaching the mesos master,
// or else an empty string if there are no such candidates.
CandidateSelector func() string
)

const (
Expand Down Expand Up @@ -134,6 +139,14 @@ func AllowReconnection(v bool) Option {
}
}

func EndpointCandidates(cs CandidateSelector) Option {
return func(c *client) Option {
old := c.candidateSelector
c.candidateSelector = cs
return EndpointCandidates(old)
}
}

// NewCaller returns a scheduler API Client in the form of a Caller. Concurrent invocations
// of Call upon the returned caller are safely executed in a serial fashion. It is expected that
// there are no other users of the given Client since its state may be modified by this impl.
Expand Down Expand Up @@ -173,15 +186,35 @@ func (cli *client) httpDo(ctx context.Context, m encoding.Marshaler, opt ...http
opt = append(opt, httpcli.Context(ctx))
for attempt := 0; ; attempt++ {
resp, err = cli.Client.Do(m, opt...)
redirectErr, ok := err.(*mesosRedirectionError)
if !ok {
return resp, err
if err == nil {
return
}
redirectErr, ok := err.(*mesosRedirectionError)

if attempt < cli.redirect.MaxAttempts {
var candidate string
if !ok {
if cli.candidateSelector == nil {
if debug {
log.Printf("not found candidate selector, using url when initilize framework")
}
candidate = cli.Endpoint()
} else {
candidate = cli.candidateSelector()
}
if candidate == "" {
if debug {
log.Printf("not found candidate url, return directly")
}
return
}
} else {
candidate = redirectErr.newURL
}
if debug {
log.Println("redirecting to " + redirectErr.newURL)
log.Printf("redirecting to %v", candidate)
}
cli.With(httpcli.Endpoint(redirectErr.newURL))
cli.With(httpcli.Endpoint(candidate))
select {
case <-getBackoff():
case <-ctx.Done():
Expand Down

0 comments on commit 7603ab3

Please sign in to comment.