Add Servers method

This commit is contained in:
Alexander Klizhentas
2014-12-14 18:28:53 -08:00
parent adce58c6a6
commit 0e2c16a681
3 changed files with 20 additions and 0 deletions
+7
View File
@@ -110,6 +110,13 @@ func NewRebalancer(handler balancerHandler, opts ...rbOptSetter) (*Rebalancer, e
return rb, nil
}
func (rb *Rebalancer) Servers() []*url.URL {
rb.mtx.Lock()
defer rb.mtx.Unlock()
return rb.next.Servers()
}
func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
pw := &utils.ProxyWriter{W: w}
start := rb.clock.UtcNow()
+1
View File
@@ -44,6 +44,7 @@ func (s *RBSuite) TestRebalancerNormalOperation(c *C) {
c.Assert(err, IsNil)
rb.UpsertServer(testutils.ParseURI(a.URL))
c.Assert(rb.Servers()[0].String(), Equals, a.URL)
proxy := httptest.NewServer(rb)
defer proxy.Close()
+12
View File
@@ -117,6 +117,17 @@ func (r *RoundRobin) RemoveServer(u *url.URL) error {
return nil
}
func (rr *RoundRobin) Servers() []*url.URL {
rr.mutex.Lock()
defer rr.mutex.Unlock()
out := make([]*url.URL, len(rr.servers))
for i, srv := range rr.servers {
out[i] = srv.url
}
return out
}
func (rr *RoundRobin) ServerWeight(u *url.URL) (int, bool) {
rr.mutex.Lock()
defer rr.mutex.Unlock()
@@ -223,6 +234,7 @@ func sameURL(a, b *url.URL) bool {
}
type balancerHandler interface {
Servers() []*url.URL
ServeHTTP(w http.ResponseWriter, req *http.Request)
ServerWeight(u *url.URL) (int, bool)
RemoveServer(u *url.URL) error