Skip to content

Commit feffebb

Browse files
author
coyzeng
committed
strip strategy
1 parent e714b00 commit feffebb

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

roundrobin/rr.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
106106
// NextServer gets the next server.
107107
func (r *RoundRobin) NextServer(w http.ResponseWriter, req *http.Request, neq *http.Request) (*url.URL, error) {
108108
// Use extension balance server, if extension return multiple servers, choose anyone.
109-
if ss := Strategy().Next(w, req, neq, r.servers); len(ss) > 0 {
110-
srv := ss[rand.Intn(len(ss))]
111-
return utils.CopyURL(srv.URL()), nil
109+
if ss := Strategy().Next(w, req, neq, r.servers); len(ss) > 0 && len(ss) < len(r.servers) {
110+
return Strategy().Strip(utils.CopyURL(ss[rand.Intn(len(ss))].URL())), nil
112111
}
113112
srv, err := r.nextServer(w, req)
114113
if err != nil {

roundrobin/strategy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type LBStrategy interface {
4242
// Next servers
4343
// Load balancer extension for custom rules filter.
4444
Next(w http.ResponseWriter, req *http.Request, neq *http.Request, servers []Server) []Server
45+
46+
// Strip filter the server URL
47+
Strip(uri *url.URL) *url.URL
4548
}
4649

4750
type CompositeStrategy struct {
@@ -68,3 +71,10 @@ func (that *CompositeStrategy) Next(w http.ResponseWriter, req *http.Request, ne
6871
}
6972
return servers
7073
}
74+
75+
func (that *CompositeStrategy) Strip(uri *url.URL) *url.URL {
76+
for _, strategy := range that.strategies {
77+
uri = strategy.Strip(uri)
78+
}
79+
return uri
80+
}

0 commit comments

Comments
 (0)