Skip to content

Commit

Permalink
strip strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
coyzeng committed Dec 7, 2022
1 parent e714b00 commit feffebb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 2 additions & 3 deletions roundrobin/rr.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// NextServer gets the next server.
func (r *RoundRobin) NextServer(w http.ResponseWriter, req *http.Request, neq *http.Request) (*url.URL, error) {
// Use extension balance server, if extension return multiple servers, choose anyone.
if ss := Strategy().Next(w, req, neq, r.servers); len(ss) > 0 {
srv := ss[rand.Intn(len(ss))]
return utils.CopyURL(srv.URL()), nil
if ss := Strategy().Next(w, req, neq, r.servers); len(ss) > 0 && len(ss) < len(r.servers) {
return Strategy().Strip(utils.CopyURL(ss[rand.Intn(len(ss))].URL())), nil
}
srv, err := r.nextServer(w, req)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions roundrobin/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type LBStrategy interface {
// Next servers
// Load balancer extension for custom rules filter.
Next(w http.ResponseWriter, req *http.Request, neq *http.Request, servers []Server) []Server

// Strip filter the server URL
Strip(uri *url.URL) *url.URL
}

type CompositeStrategy struct {
Expand All @@ -68,3 +71,10 @@ func (that *CompositeStrategy) Next(w http.ResponseWriter, req *http.Request, ne
}
return servers
}

func (that *CompositeStrategy) Strip(uri *url.URL) *url.URL {
for _, strategy := range that.strategies {
uri = strategy.Strip(uri)
}
return uri
}

0 comments on commit feffebb

Please sign in to comment.